LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-13-2014, 07:02 AM   #1
mdesh
LQ Newbie
 
Registered: Aug 2014
Posts: 4

Rep: Reputation: Disabled
Grep Multiple same words from multiple paragraphs


Hi,

I want to grep multiple same words from different paragraphs and copy only those words to different file. I am trying with grep command but not getting the output which I want.

As an example, please find below paragraph where I want to grep keywords such as user=<user name>,queue=<queue name> ctime=<value>, start=<value>

Code:
08/12/2014 23:52:18;E;956.sb;user=user1 group=user1 jobname=STDIN queue=largeq ctime=1407865741 qtime=1407865741 etime=1407865741 start=1407865742 owner=user1@sb exec_host=node231/0+node231/1+node231/2+node231/3+node231/4+node231/5+node231/6+node231/7+node231/8+node231/9+node231/10+node231/11+node230/0+node230/1+node230/2+node230/3+node230/4+node230/5+node230/6+node230/7+node230/8+node230/9+node230/10+node230/11 Resource_List.neednodes=2:ppn=12 Resource_List.nodect=2 Resource_List.nodes=2:ppn=12 session=24628 end=1407867738 Exit_status=0 resources_used.cput=00:00:00 resources_used.mem=4304kb resources_used.vmem=376848kb resources_used.walltime=00:33:16

So can someone please suggest on how to get this output as there are so many paragraphs from which I want to grep these keywords.

Regards
MDesh

Last edited by colucix; 08-14-2014 at 12:23 AM. Reason: Disabled smiles in text and added CODE tags to improve readability
 
Old 08-13-2014, 07:54 AM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 5,388

Rep: Reputation: 398Reputation: 398Reputation: 398Reputation: 398
You need other program than grep to extract strings from text. Grep returns complete lines.

Btw your example text is in one line.
If your example matches reality, I would do (say your text is in myfile.txt:
Code:
perl -F';' -lane 'print join ",",grep {/(user|queue|ctime|start)=/} split / /,$F[3]' myfile.txt
If you want to save output in another file, use ">" redirecting operator
Eg: command > out.txt

Last edited by keefaz; 08-13-2014 at 07:56 AM.
 
2 members found this post helpful.
Old 08-13-2014, 08:34 AM   #3
mdesh
LQ Newbie
 
Registered: Aug 2014
Posts: 4

Original Poster
Rep: Reputation: Disabled
Thanks Keefaz, this is what exactly I want.

In addition to the keywords that I wanted to grep, I also want to grep <number>.sb from the paragraph.
So the number of keywords that I want to grep are

<number>.sb user=<user name> queue=<queue name> ctime=<value>, start=<value>

So do you any suggestion on this?
 
Old 08-13-2014, 08:55 AM   #4
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 5,388

Rep: Reputation: 398Reputation: 398Reputation: 398Reputation: 398
I see, it's the 3rd field ($F[2])
Code:
perl -F';' -lane 'print $F[2].",".join ",",grep {/(user|queue|ctime|start)=/} split / /,$F[3]' myfile.txt
Note that join "," will format output with words separated by ","
If you want them separated with another character (space?), just change the "," value after join and $F[2]
 
Old 08-14-2014, 04:47 AM   #5
mdesh
LQ Newbie
 
Registered: Aug 2014
Posts: 4

Original Poster
Rep: Reputation: Disabled
Thanks Keefaz for your help.

I am able to get the required output based on the command that you have suggested.
 
  


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
grep a file and find multiple words and take action accordingly. hbchok Linux - Newbie 5 11-04-2012 07:50 PM
grep multiple words and linking with other file zonah12 Linux - Newbie 11 11-17-2011 11:29 PM
Searching multiple words? ritika_sharma Linux - Newbie 7 12-17-2010 03:25 AM
grep multiple words any order (AND, not OR) single line, from many files cedardoc Linux - Newbie 7 07-29-2010 11:23 AM
Grep an entire file but must contain multiple words wakeboarder3780 Linux - Newbie 10 02-19-2009 05:46 PM


All times are GMT -5. The time now is 02:02 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration