LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-02-2007, 02:58 AM   #1
ahpin
Member
 
Registered: Oct 2003
Location: Malaysia
Distribution: Redhat, Mandrake 9.1, SuSE Linux
Posts: 32

Rep: Reputation: 15
How to get the pattern using sed or awk?


I would like to know how can i used the sed or awk to help me captured out the user name only from the file pattern as below:

/nfs/disk001/jeff
/nfs/disk002/mery
/nfs/homedisk01/kathie
/nfs/localhome/joseph
/nfs/disk02/catherine


The end result will be:
jeff
mery
kathie
joseph
catherine

Thanks.
 
Old 08-02-2007, 03:08 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,502

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Have you any experience in awk? Here you can define a field separator other than the blank space(s) and get the last field of each line, as in
Code:
gawk -F\/ '{print $NF}' dirlist
where dirlist is a file containing your input lines. Or in alternative you can simply pipe the input from another command to gawk.
 
Old 08-02-2007, 03:13 AM   #3
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
sed 's#^.*/##' file

or

for line in $(cat testfile); do basename $line; done
 
Old 08-02-2007, 03:16 AM   #4
ahpin
Member
 
Registered: Oct 2003
Location: Malaysia
Distribution: Redhat, Mandrake 9.1, SuSE Linux
Posts: 32

Original Poster
Rep: Reputation: 15
Brilliant!!!
Thanks for all your help....it is working now...real great.....
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need ideas on how to grep for pattern using awk rahulbgl Linux - Newbie 4 03-05-2007 03:07 PM
AWK/SED Multiple pattern matching over multiple lines issue GigerMalmensteen Programming 15 12-03-2006 05:08 PM
Sed pattern matching digitalbrutus Programming 1 08-20-2006 01:37 PM
pattern matching problem in sed digitalbrutus Programming 4 08-20-2006 04:40 AM
awk print lines that doesn't have a pattern huynguye Programming 5 05-04-2006 11:08 AM


All times are GMT -5. The time now is 07:41 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration