LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 09-08-2008, 09:54 AM   #1
Hondro
Member
 
Registered: Mar 2006
Location: Lithuania
Distribution: Slackware-12.2
Posts: 102

Rep: Reputation: 15
grep two strings


Hi, for example i have a line:
sasa 2008 fdsfds fdfsd fsdfds 2009 nbvnvb bnvnvb

i want to make a grep command that would output such string(from line above):
2008 2009

How to do that?

Thanks
 
Old 09-08-2008, 10:12 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
It is not clear what is the pattern you want to look for. Are "2008" and "2009" fixed strings? Or do you want to extract any number? From you example I will end up with:
Code:
echo sasa 2008 fdsfds fdfsd fsdfds 2009 nbvnvb bnvnvb | echo $(grep -E -o '2008|2009')
the -o option prints only the matching part. The echo $(...) syntax prevents the two matching string to be printed on different lines.
 
Old 09-08-2008, 10:16 AM   #3
CRC123
Member
 
Registered: Aug 2008
Distribution: opensuse, RHEL
Posts: 374
Blog Entries: 1

Rep: Reputation: 32
Do you have to use grep? I don't think there is a way to do it with grep. 'sed' command would be the way to do this.
Is this homework?

Code:
<input> | sed -r 's/.*(2008).*(2009).*/\1 \2/'
note: '\1' corresponds to capture group 1 (aka first parenthesized text) and '\2' corresponds to second capture group.

man sed would be good.

EDIT: colucix's '-o' option is much easier than mine
 
Old 09-08-2008, 10:55 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 7.7 (?), Centos 8.1
Posts: 17,817

Rep: Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553
If that's really one space between fields and you want those fields...

echo "sasa 2008 fdsfds fdfsd fsdfds 2009 nbvnvb bnvnvb"|cut -d' ' -f2,6
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
grep multiple strings GEEXTER Linux - General 7 12-06-2013 10:56 PM
Trying to understand pipes - Can't pipe output from tail -f to grep then grep again lostjohnny Linux - Newbie 15 03-12-2009 11:31 PM
Grep search & replace strings with backslashes? ziphem Linux - Newbie 4 10-21-2007 05:36 PM
how to find duplicate strings in vertical column of strings markhod Programming 7 11-02-2005 05:04 AM
Problem matching strings with grep/egrep Seb74 Linux - Newbie 5 05-26-2005 02:40 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 06:34 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration