Solved

I need a parsing script...or a command with awk, not sure

Posted on 2014-04-27
5
289 Views
Last Modified: 2014-04-28
This is an XML file exported from my text. I need a script to parse it...as an example below this XML file

 <sms protocol="0" address="4053234432" date="1388706261627" type="2" subject="null" body="I'm heading home" toa="null" sc_toa="null" service_center="null" read="1" status="-1" locked="0" date_sent="null" readable_date="Jan 2, 2014 5:44:21 PM" contact_name="Mom" />
  <sms protocol="0" address="9726768860" date="1388786728946" type="1" subject="null" body="Tony_new_number - How are you?" toa="null" sc_toa="null" service_center="null" read="1" status="-1" locked="0" date_sent="null" readable_date="Jan 3, 2014 4:05:28 PM" contact_name="Tony Comp" />
  <sms protocol="0" address="9726768860" date="1388786847009" type="1" subject="null" body="Tony_new_number -  Fine.just anticipating getting back. I'm ready. Although I'll miss her." toa="null" sc_toa="null" service_center="null" read="1" status="-1" locked="0" date_sent="null" readable_date="Jan 3, 2014 4:07:27 PM" contact_name="Tony Comp" />

I would like it to read like the following

4053234432 "I'm heading home" Jan 2, 2014 5:44:21 "Mom"
9726768860 "Tony_new_number - How are you?" "Jan 3, 2014 4:05:28 PM" ""Tony Comp"
"9726768860" "Tony_new_number -  Fine.just anticipating getting back. I'm ready. Although I'll miss her." "Jan 3, 2014 4:07:27 PM" "Tony Comp"

So, I basically just need the:
The fields: address="xxx" body="xxxx" readable_date="xxxxx" and contact_name="xxx"

Thanks for any help
0
Comment
Question by:Viclyn
  • 2
  • 2
5 Comments
 
LVL 61

Expert Comment

by:gheist
ID: 40027037
lex/bison seems more appropriate to dig randomly ordered fields you have.
0
 

Author Comment

by:Viclyn
ID: 40027140
I don't think it's very random. There are hundreds of entries, and they take on the format:

<sms

protocol=""
address=""
date=""
type=""
subject=""
body=""
toa=""
sc_toa=""
service_center=""
read=""
status=""
locked=""
date_sent=""
readable_date=""
contact_name=""

/>

I just want these four field:

address=""
body=""
readable_date=""
 contact_name=""
0
 
LVL 19

Accepted Solution

by:
simon3270 earned 500 total points
ID: 40027248
sed 's/^.* address="\([0-9]*\). .* body=\("[^"]*"\) .* readable_date=\("[^"]*"\) contact_name=\("[^"]*"\) .*/\1 \2 \3 \4/' input_file

Open in new window


I've assumed you don't want the double quotes round the phone number (1 of your output lines has the quotes, the other 2 don't!)
0
 
LVL 19

Expert Comment

by:simon3270
ID: 40027255
My answer does assume a couple of things:
- that each entry is on one line, as in your example text
- that none of the fields contain double-quote characters.  What happens if they do?
0
 

Author Closing Comment

by:Viclyn
ID: 40027639
Very nice. Thanks
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now