extracting a path from a string

Posted on 2006-10-31
Last Modified: 2010-04-20


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?
Question by:akohan
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
LVL 84

Accepted Solution

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

Expert Comment

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

Assisted Solution

Mysidia earned 25 total points
ID: 17846517
Err rather

perl -e 'while(split(":",<>)) { print $_[5]."\n";}'
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

LVL 84

Expert Comment

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

Assisted Solution

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

Assisted Solution

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

Expert Comment

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
LVL 84

Expert Comment

ID: 17847741
perl -F: -lane 'print $F[5] if /^user_A:/' /etc/passwd

Assisted Solution

ltarc3 earned 25 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.

Assisted Solution

ctwaley earned 25 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)
LVL 48

Expert Comment

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:


The awk solution is the easiest and most sensible.

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Running linux commands into linux system remotely using powershell 6 105
Unix Help with:  mailx -s 17 65
check the file dates in unix 14 76
sed replace command 12 26
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension ( This reminded me of questions tha…
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Suggested Courses

732 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