Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 262
  • Last Modified:

extracting a path from a string


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
akohan
Asked:
akohan
  • 3
  • 2
  • 2
  • +3
6 Solutions
 
ozoCommented:
awk -F: '{ print $6}'
0
 
MysidiaCommented:
perl -e 'while(split(":",<>)) { print $_[5];}'
0
 
MysidiaCommented:
Err rather

perl -e 'while(split(":",<>)) { print $_[5]."\n";}'
0
Build your data science skills into a career

Are you ready to take your data science career to the next step, or break into data science? With Springboard’s Data Science Career Track, you’ll master data science topics, have personalized career guidance, weekly calls with a data science expert, and a job guarantee.

 
ozoCommented:
perl -ne 'print ((split":")[5],"\n")'
perl -F: -lane 'print $F[5]'
0
 
ygouthamCommented:
cat /etc/passwd | cut -s -d':' -f6
0
 
TintinCommented:
awk -F: '/^user_A:/ {print $6}' /etc/passwd
0
 
ygouthamCommented:
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
 
ozoCommented:
perl -F: -lane 'print $F[5] if /^user_A:/' /etc/passwd
0
 
ltarc3Commented:
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
 
ctwaleyCommented:
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
 
TintinCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

A proven path to a career in data science

At Springboard, we know how to get you a job in data science. With Springboard’s Data Science Career Track, you’ll master data science  with a curriculum built by industry experts. You’ll work on real projects, and get 1-on-1 mentorship from a data scientist.

  • 3
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now