Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > macOS > Can anyone generate a script for this

Can anyone generate a script for this
Thread Tools
Sudarshan_Kanna
Fresh-Faced Recruit
Join Date: Sep 2007
Status: Offline
Reply With Quote
Sep 20, 2007, 09:39 AM
 
<ARS>

<tag1>one</tag1>

<tag2>two</tag2>

<tag3>three<AltError Code=123 Description=456789/></tag3>

<tag4>four</tag4>

<ARS>

<ARS>

<tag1>ABCD</tag1>

<tag2>ABCD</tag2>

<tag3>ddsdsds<AltError Code=123 Description=456789/></tag3>

<tag4>EFGH<AltError Code=abc Description=defg/></tag4>

<ARS>



Expected Output:



tag1|tag2|tag3|tag4|code|Description

one|two|three|four|123|456789

ABCD|ABCD|ddsdsds|123|456789

ABCD|ABCD|ddsdsds|abc|defg

The script should read this file and create a pipe delimited file. Each ARS tag record in the XML file should create one line in the target. Get all the values enclosed within the tags, which are the column values.



Also if suppose an <AtlError> encounters within an ARS tag, then get the Code and Description values of the AltError tag and append it to the last of that particular ARS record. If suppose 2 AltError encounters within an ARS tag, the target should be having 2 records
     
Simon
Posting Junkie
Join Date: Nov 2000
Location: in front of my Mac
Status: Offline
Reply With Quote
Sep 20, 2007, 09:56 AM
 
You should get acquainted with sed.
     
Gee4orce
Professional Poster
Join Date: Dec 2000
Location: Staffs, UK
Status: Offline
Reply With Quote
Sep 20, 2007, 11:27 AM
 
Sure can.

My normal rate is $70/hour
     
rehoot
Dedicated MacNNer
Join Date: Nov 2005
Status: Offline
Reply With Quote
Sep 22, 2007, 10:39 AM
 
I have a script that is close to what you want, but it lease the newlines in the output. I think the special characters will cause a problem when I post the script: the slanted smiley faces are equals signs.

This is all one line:

cat ~/input.txt|sed -e s'/\(<tag[0-9]>\)\([^<]*\)<\/tag[0-9]>/\2|/g' -e s'/ \{1,30\}//g' -e s'/<tag[0-9]>\([^<]*\)<AltError[ ]*Code=\([^ ]*\)[ ]*Description=\([^ ]*\)/\1|Code1|Description2/g' -e s'/<ARS>//g'

Some notes:
1) the 'cat' program just prints the file
2) a block in a sed command that is found using a string that is surrounded with \( and \) can later be displayed using \1 (or \2 if it is the second occurrence).
3) the code that says \([^ ]*\) means to find a bunch of characters until you reac a space (the carret means "NOT" and it is follwed by a space).
4) I added the strings: "Code:" and "Description:" to the output
5) tag[0-9] finds the string "tag" followed by a number from 0 to 9. Add a star after this if there are more digits.
Mac Pro Quad: 2.66GHz; 4 GB Ram; 4x500GB drives; Radeon X1900, 23" Cinema Screen, APC UPS
PowerBook G4: 1.33GHz; 768MB Ram; 60GB drive
     
   
 
Forum Links
Forum 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
Top
Privacy Policy
All times are GMT -4. The time now is 11:23 PM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,