Combine two lists From Multiple Grep commands.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Combine two lists From Multiple Grep commands.
# 1  
Old 03-13-2011
Combine two lists From Multiple Grep commands.

I'm working with a file with an xml structure. I'd like to parse it down to just the bits i want. Here is and example of the file
Code:
<message id="96352877" method="status">
      <date rfc="Sat, 12 Mar 2011 16:13:15 -0600" unix="1299967995" />
      <services>
        <service id="facebook" name="Facebook" />
        <service id="twitter" name="Twitter" />
        <service id="myspace" name="MySpace" />
        <service id="identi.ca" name="Identi.ca" />
        <service id="friendfeed" name="FriendFeed" />
        <service id="gtalk" name="GTalk Status" />
        <service id="yahoo" name="Yahoo Profiles" />
        <service id="buzz" name="Google Buzz" />
      </services>
      <content>
        <body>SSBqdXN0IGRpc21hbnRsZWQgbXkgUnViaWsncyBDdWJlLi4uIFRoZXJlJ3MgTm8gbWFnaWMgaW4gdGhlcmUgISEh</body>
      </content>
      <location />
      <mood />
      <tags />
      <from>API</from>
    </message>
    <message id="96345969" method="status">
      <date rfc="Sat, 12 Mar 2011 15:18:17 -0600" unix="1299964697" />
      <services>
        <service id="facebook" name="Facebook" />
        <service id="twitter" name="Twitter" />
        <service id="myspace" name="MySpace" />
        <service id="identi.ca" name="Identi.ca" />
        <service id="friendfeed" name="FriendFeed" />
        <service id="gtalk" name="GTalk Status" />
        <service id="yahoo" name="Yahoo Profiles" />
        <service id="buzz" name="Google Buzz" />
      </services>
      <content>
        <body>VGhlIEVsZXBoYW50cyBvZiBQb3puYW4gYnkgT3Jzb24gU2NvdHQgQ2FyZCBBIFN1cmVhbCBzaG9ydCBzdG9yeSBodHRwOi8vcGluZy5mbS8yRVpuVw==</body>
      </content>
      <location />
      <mood />
      <tags />
      <from>API</from>
    </message>

I'd like to take the bold bits and Put them in a list ie.
$date $body
so the output would look like this:
Code:
1299967995  SSBqdXN0IGRpc21hbnRsZWQgbXkgUnViaWsncyBDdWJlLi4uIFRoZXJlJ3MgTm8gbWFnaWMgaW4gdGhlcmUgISEh
1299964697  VGhlIEVsZXBoYW50cyBvZiBQb3puYW4gYnkgT3Jzb24gU2NvdHQgQ2FyZCBBIFN1cmVhbCBzaG9ydCBzdG9yeSBodHRwOi8vcGluZy5mbS8yRVpuVw==

I have some code that will get each of these and list them sepereately but I'm having a hard time combining the two lists.

Code:
date=$(grep "unix"| sed 's/.*\(unix="\)\(.*\)\(".*\)/\2/')
body=$(grep "<body>"| sed -e 's/<body>//g' | sed -e 's/<\/body>//g')
echo "$date $body"

Everyway I've tried to make the list has failed so far Smilie

Thankyou for the help, I know this should be simple, yet alludes me so far.
# 2  
Old 03-13-2011
Try this,
Code:
perl -nle 'printf "$1\t" if /unix="(.+?)"/>$/;print $1 if m/<body>(.*)<\/body>/;'  input.xmls

This User Gave Thanks to pravin27 For This Post:
# 3  
Old 03-13-2011
try:
Code:
awk  '/unix=/{v=gensub("unix=|\"","",1,$(NF-1))}/<body>/{print v,gensub("</?body>","","g",$1)}' file

This User Gave Thanks to yinyuemi For This Post:
# 4  
Old 03-13-2011
Thank you both. They both work :-) I can understand the how perl script works pretty easily.

How does the awk line break down?
# 5  
Old 03-13-2011
Hi Pravin,

Code:
perl -nle 'printf "$1\t" if /unix="(.+?)"/>$/;print $1 if m/<body>(.*)<\/body>/;'  input.xmls

I have two queries.

1. Is there any special reason why we are going for non-greddy matching(.+?)
2. In the first if statement , the delimiters are not provided properly. I think its treating ">" as redirection operator .

Thanks in advance
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Combine 2 Commands

Hello, I have the following code. I wonder if it can be combined into 1 command. y=`ls -1| tail -n 1` m=${y%.abc} Thank you. (3 Replies)
Discussion started by: april
3 Replies

2. UNIX for Beginners Questions & Answers

Can I combine below mentioned grep commands using OR (when searching strings having spaces)

Command 1: $script | grep 'Write to ECC( SSID=MARGIN)' Command 2: $script | grep 'is not greater than existing logical processing' The above commands run my script and search the mentioned strings but I do not want to run my script twice. It is increasing run time. Can someone tell me... (3 Replies)
Discussion started by: Tanu
3 Replies

3. Shell Programming and Scripting

Combine multiple commands

I have the following sh-script: konsole -T todo -e vi todo.txt & konsole -T window1 -e ssh user@server & konsole -T window2 -e ssh user@server2 -e cd directory & The first two lines are working fine. The first opens a txt-file, the second opens a ssh-connection. The third line... (6 Replies)
Discussion started by: andre666
6 Replies

4. Shell Programming and Scripting

Csh - how to combine multiple commands in one line

Hey everyone, I am working in an environment where the different users can use ksh or csh. My situation is that I need the same result with one single command line. I am searching for the real path the file is in. My ksh input and output ts2:ts2adm> cd $(dirname $(which sapcontrol)); pwd -P... (2 Replies)
Discussion started by: h1kelds
2 Replies

5. UNIX for Dummies Questions & Answers

Combine two lists of variables

Thanks in advance for any advice and help. I have two lists of variables that I want to put into nested for loops. for x in 1 2 3 do for y in a b c do The output I want is: filepath/1/ command modified by a filepath/2/ command modified by b filepath/3/ command modified by c To... (10 Replies)
Discussion started by: wheyhamp
10 Replies

6. Shell Programming and Scripting

Turning my lists into useable copy commands

Hello everyone! I currently work in the motion picture industry and we constantly receive lists of missing media from our sound department. I use a series of commands in TextWrangler in order to remake the lists into useable copy commands to automate the whole process but if I could make this work... (9 Replies)
Discussion started by: grep_me_please
9 Replies

7. Shell Programming and Scripting

Combine multiple awk commands

Hi Team, I am getting input like below $ ps -ef | grep pmon | grep -v asm | grep -v grep oracle 3246 1 0 00:03 ? 00:00:01 ora_pmon_racora1 oracle 4367 1 0 00:03 ? 00:00:01 ora_pmon_test1 oracle 6893 1 0 00:03 ? 00:00:01 ora_pmon_gipora1... (6 Replies)
Discussion started by: kamauv234
6 Replies

8. Shell Programming and Scripting

Can I combine these two commands into one?

sed -e :a -e 's/<*>//g;/</N;//ba' a2.html -removes html tags and sed -i 's/YOURS TRULY/Joe Bob/' a2.html Replaces a string with another string can i make it into one string? (2 Replies)
Discussion started by: boyboy1212
2 Replies

9. UNIX for Dummies Questions & Answers

Combine commands

Hi, i tried to combine grep with find and it didnt work grep 'find dirname filename" i also would like that the file will be sorted in the way. thanks a lot. (2 Replies)
Discussion started by: Spoiler
2 Replies

10. AIX

grep using lists?

I have a file that contain a list of files. How can I use grep to search the files in the list for a specific pattern? (2 Replies)
Discussion started by: bbbngowc
2 Replies
Login or Register to Ask a Question