LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 06-11-2014, 08:00 AM   #1
divya0102
LQ Newbie
 
Registered: Jun 2014
Posts: 2

Rep: Reputation: Disabled
Question sed command to display only the matched pattern and not the whole line


Hi,

I have a file with content as

2014 H_DSF_ABL.txt.gz
Dec 11 09:29 H_DSF_CDL.txt.gz
18 08:09 H_DSF_HCL.txt.gz
H_DSF_PPM.txt.gz

I need the output as

H_DSF_ABL.txt.gz
H_DSF_CDL.txt.gz
H_DSF_HCL.txt.gz
H_DSF_PPM.txt.gz

Please suggest the unix command to display the exact output as mentioned(only the pattern matched and not the whole line).
Also it should work on solaris OS.

Thanks!!
 
Old 06-11-2014, 08:32 AM   #2
pan64
Senior Member
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 4,478

Rep: Reputation: 1220Reputation: 1220Reputation: 1220Reputation: 1220Reputation: 1220Reputation: 1220Reputation: 1220Reputation: 1220Reputation: 1220
I suggest you to use sed. There are two different approach:
1. construct a regexp to find what you need, group it, and replace the whole line to that group
2. construct a regexp to find what you do not need and replace it with nothing.

what have you made so far?
 
Old 06-11-2014, 08:33 AM   #3
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 748
Blog Entries: 2

Rep: Reputation: 193Reputation: 193
You could match and delete everything up to the last space.
sed 's/.* //'
 
2 members found this post helpful.
Old 06-11-2014, 08:53 AM   #4
mina86
Member
 
Registered: Aug 2008
Distribution: Slackware
Posts: 354

Rep: Reputation: 148Reputation: 148
If this is all you need, “grep -o” might be a better option than using sed.
 
1 members found this post helpful.
Old 06-11-2014, 09:25 AM   #5
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Ubuntu
Posts: 1,061

Rep: Reputation: 284Reputation: 284Reputation: 284
Quote:
Originally Posted by mina86 View Post
If this is all you need, “grep -o” might be a better option than using sed.
Please elaborate. I tried this and couldn't make it work.

Edit: Ahh, now I understand. This works.
Code:
grep -o "H_DSF.*" $InFile >$OutFile
Daniel B. Martin

Last edited by danielbmartin; 06-11-2014 at 09:32 AM. Reason: Enlightenment
 
Old 06-11-2014, 09:33 AM   #6
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 12,094

Rep: Reputation: 978Reputation: 978Reputation: 978Reputation: 978Reputation: 978Reputation: 978Reputation: 978Reputation: 978
Code:
grep -Eo "[^[:space:]]+$"
?
 
Old 06-11-2014, 10:42 AM   #7
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Ubuntu
Posts: 1,061

Rep: Reputation: 284Reputation: 284Reputation: 284
Maybe this is a nitpick but I question this ...
Code:
sed 's/.* //'
... and this ...
Code:
grep -Eo "[^[:space:]]+$"
... because those solutions are based on position rather than pattern.

Daniel B. Martin
 
Old 06-11-2014, 10:45 AM   #8
mina86
Member
 
Registered: Aug 2008
Distribution: Slackware
Posts: 354

Rep: Reputation: 148Reputation: 148
If you want the last element to be printed, even easier is
Code:
awk '{print $NF}'
 
1 members found this post helpful.
Old 06-12-2014, 01:39 AM   #9
divya0102
LQ Newbie
 
Registered: Jun 2014
Posts: 2

Original Poster
Rep: Reputation: Disabled
Smile sed worked perfectly!!

Thank you all for your replies.

sed and awk command worked perfectly in unix and solaris OS.

Code:
sed 's/.* //'
Code:
awk '{print $NF}'
I got the desired output.
Actually grep -o is better option in unix and but it does not work in solaris.

Last edited by divya0102; 06-12-2014 at 01:46 AM.
 
  


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
[SOLVED] sed and how to remove all lines after matched pattern transmutated Programming 5 06-13-2012 07:54 AM
[SOLVED] sed to display the pattern string, the line above it and the first line of that para rockie321 Linux - Newbie 3 04-03-2011 02:48 PM
[SOLVED] Need sed help: s/ command won't replace two occurrences of pattern on same line GrapefruiTgirl Programming 7 12-16-2009 02:08 AM
sed display line after pattern match inonzi_prowler Linux - Software 3 02-19-2007 01:47 PM
Inputting with sed, based on matched pattern mike9287 Programming 5 07-20-2006 07:49 AM


All times are GMT -5. The time now is 12:31 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