Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

extracting a path from a string

Posted on 2006-10-31
14
Medium Priority
?
257 Views
Last Modified: 2010-04-20

Hello,

I want to extract a portion of a string. The string is a path which beloings to an entry in /etc/passwd file

Let's assume the line is:

user_A:x:100:0:ABC User Account:/Dir1/Dir2/Dir3:/bin/sh

I did:

      cat /etc/passed | grep user_A  | awk '{ print $3}'  

this prints:  Account:/Dir1/Dir2/Dir3:/bin/sh

but I need to extract "/Dir1/Dir2/Dir3/" only, neither before it nor after it.

Any suggestions?
Thanks
0
Comment
Question by:akohan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +3
14 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 100 total points
ID: 17846196
awk -F: '{ print $6}'
0
 
LVL 23

Expert Comment

by:Mysidia
ID: 17846514
perl -e 'while(split(":",<>)) { print $_[5];}'
0
 
LVL 23

Assisted Solution

by:Mysidia
Mysidia earned 100 total points
ID: 17846517
Err rather

perl -e 'while(split(":",<>)) { print $_[5]."\n";}'
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 84

Expert Comment

by:ozo
ID: 17846565
perl -ne 'print ((split":")[5],"\n")'
perl -F: -lane 'print $F[5]'
0
 
LVL 14

Assisted Solution

by:ygoutham
ygoutham earned 100 total points
ID: 17847048
cat /etc/passwd | cut -s -d':' -f6
0
 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 100 total points
ID: 17847340
awk -F: '/^user_A:/ {print $6}' /etc/passwd
0
 
LVL 14

Expert Comment

by:ygoutham
ID: 17847688
cat /etc/passwd | grep 'user_A' | cut -s -d':' -f6

the earlier would give all the home directories of all users.  this one gives only specific user. can be put in a script and a parameter can also be given as $1 for any specific user
0
 
LVL 84

Expert Comment

by:ozo
ID: 17847741
perl -F: -lane 'print $F[5] if /^user_A:/' /etc/passwd
0
 
LVL 1

Assisted Solution

by:ltarc3
ltarc3 earned 100 total points
ID: 17858664
Look at all these solutions!  Perl is overkill it seems to me, awk is your solution.  But generally you use what you are most comfortable with.  You almost had, just needed help with the syntax.  Ozo had it right with the first comment and gave you a nice Perl alternative, as well. Also, note that you want the 6th field for the user directory, not the third.

You can grep a file directly, you do not need to cat and then pipe to it:

grep ^user_A /etc/passwd | awk -F: {print $6}

The -F: tells awk that the fields are separated by colons.

And just as you do not need to cat and then pipe to grep, you do not even need to grep and then pipe to awk.  Awk can work directly on the file and do the grepping itself:

awk -F: '/^user_A/ {print $6}' /etc/passwd

This tells awk that the field separator is a colon, match lines beginning with user_A, print the 6th field, operate on /etc/passwd.  One command.   Nice, clean, concise.
0
 
LVL 2

Assisted Solution

by:ctwaley
ctwaley earned 100 total points
ID: 17902752
One more to the mix:

cut -d: -f6 <(grep 'user_A' /etc/passwd)

(basically the same as ygoutham's post above, w/o the 'cat' command)
0
 
LVL 48

Expert Comment

by:Tintin
ID: 17902951
If you are using grep/cut, the more usual way is

grep 'user_A' /etc/passwd|cut -d: -f6

But note that will match *any field in /etc/passwd that *contain* user_A, eg:

Myuser_ABC

The awk solution is the easiest and most sensible.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Fine Tune your automatic Updates for Ubuntu / Debian
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will giveā€¦
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Suggested Courses

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question