Replacing lines which match certain criteria


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing lines which match certain criteria
# 1  
Old 09-22-2011
Replacing lines which match certain criteria

Hi,

I have code which is like this
Code:
<TABLE name="UsageDetail_24>
<ROW>
<Date24><![CDATA[25 May 11]]></Date24>
<Time24><![CDATA[12:56:19]]></Time24>
<Destination24><![CDATA[07800852666]]></Destination24>
<Rate24><![CDATA[Peak]]></Rate24>
<Duration24><![CDATA[00:06:53]]></Duration24>
<Cost24><![CDATA[0.000]]></Cost24>
<Allowance24><![CDATA[F]]></Allowance24>
</ROW>
<ROW>
<Date24><![CDATA[25 May 11]]></Date24>
<Time24><![CDATA[13:06:38]]></Time24>
<Destination24><![CDATA[07785776658]]></Destination24>
<Rate24><![CDATA[Peak]]></Rate24>
<Duration24><![CDATA[00:00:05]]></Duration24>
<Cost24><![CDATA[0.000]]></Cost24>
<Allowance24><![CDATA[F]]></Allowance24>
</ROW>
</TABLE>
<TABLE name="Headers_43>
<ROW>
<Date_43><![CDATA[date]]></Date_43>
<Time_43><![CDATA[time]]></Time_43>
<Source_43><![CDATA[source]]></Source_43>
<Volume_43><![CDATA[volume (KB)]]></Volume_43>
<Cost_43><![CDATA[cost]]></Cost_43>
<Allowance_43><![CDATA[allowance]]></Allowance_43>
</ROW>
</TABLE>

I need to change it so that the table name gets inserted into the ROW and followed by -row for every time it says that up until the table ends and also remove the table name=

Code:
<UsageDetail_24>
<UsageDetail_24-row>
<Date24><![CDATA[25 May 11]]></Date24>
<Time24><![CDATA[12:56:19]]></Time24>
<Destination24><![CDATA[07800852666]]></Destination24>
<Rate24><![CDATA[Peak]]></Rate24>
<Duration24><![CDATA[00:06:53]]></Duration24>
<Cost24><![CDATA[0.000]]></Cost24>
<Allowance24><![CDATA[F]]></Allowance24>
</UsageDetail_24-row>
<UsageDetail_24-row>
<Date24><![CDATA[25 May 11]]></Date24>
<Time24><![CDATA[13:06:38]]></Time24>
<Destination24><![CDATA[07785776658]]></Destination24>
<Rate24><![CDATA[Peak]]></Rate24>
<Duration24><![CDATA[00:00:05]]></Duration24>
<Cost24><![CDATA[0.000]]></Cost24>
<Allowance24><![CDATA[F]]></Allowance24>
</UsageDetail_24-row>
</UsageDetail_24>
<Headers_43>
<Headers_43-row>
<Date_43><![CDATA[date]]></Date_43>
<Time_43><![CDATA[time]]></Time_43>
<Source_43><![CDATA[source]]></Source_43>
<Volume_43><![CDATA[volume (KB)]]></Volume_43>
<Cost_43><![CDATA[cost]]></Cost_43>
<Allowance_43><![CDATA[allowance]]></Allowance_43>
</Headers_43-row>
</Headers_43>

This file is long with many different table names in it. Is there a code to do this?
# 2  
Old 09-22-2011
Try this:
Code:
awk -F\" '/<TABLE/{t=$2;sub(">","",t);$0="<"t">"}/ROW>/{r=t"-row";sub("ROW",r)}/<\/TABLE/{$0="</"t">"}1' file

# 3  
Old 09-22-2011
your a genius great work thank you!
# 4  
Old 09-22-2011
Code:
sed -ne ':x;/TABLE name/{s/.*="//;s/.$//;h;s/\(.*\)/<\1>/}; ' -e '/<ROW>/{s///;g;s/.*/<&-row>/};/<\/ROW/{s///;g;s|.*|<\/&-row>|};/<\/TABLE/{s///;g;s/.*/<\/&>/};' -e 'tx;p' file

regards
ygemici
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replacing match lines from one file with another file

Please help me in this scenario i have two text files as below file1.txt record_1 conversion of a file; code change; data types; lenght; end; record_2 field name; string; new fileline; end; record_3 (7 Replies)
Discussion started by: velraj9
7 Replies

2. Shell Programming and Scripting

Match based on criteria to file

Trying to match $1 of target.txt to $5 of file.txt. If there is a match then in an output.txt file $1,$1 (row underneath),$6,$4,$7 from file.txt are printed on the same line as $1 of target.txt. The input is from excel and the output should be tab-deliminated. Thank you :). target.txt... (2 Replies)
Discussion started by: cmccabe
2 Replies

3. Shell Programming and Scripting

Rows to Columns with match criteria

Hello Friends, I have a input file having hundreds of rows. I want them to translate in to columns if column 1 is same. Input data: zp06 xxx zp06 rrr zp06 hhh zp06 aaa zp06 ggg zp06 qwer zp06 ser zl11 old3 zl11 old4 zl11 old5 zl11 old6 zl11 old7 zm14 luri zm14 body zm14 ucp (9 Replies)
Discussion started by: suresh3566
9 Replies

4. Shell Programming and Scripting

awk not working for calculating no of lines with criteria

I have tar.gz file and i want to count the lines which are matching the criteria as well as which are not matching the criteria. Following is the code Output Requirement: Match the input from zcat with 26th filed having 02 value, in case it matches then print the output in a file & increase... (12 Replies)
Discussion started by: siramitsharma
12 Replies

5. Shell Programming and Scripting

Need To Delete Lines Based On Search Criteria

Hi All, I have following input file. I wish to retain those lines which match multiple search criteria. The search criteria is stored in a variable seperated from each other by comma(,). SEARCH_CRITERIA = "REJECT, DUPLICATE" Input File: ERROR,MYFILE_20130214_11387,9,37.75... (3 Replies)
Discussion started by: angshuman
3 Replies

6. Shell Programming and Scripting

Select lines from a file based on a criteria

Hi I need to select lines from a txt file, I have got a line starting with ZMIO:MSISDN= and after a few line I have another line starting with 'MOBILE STATION ISDN NUMBER' and another one starting with 'VLR-ADDRESS' I need to copy these three lines as three different columns in a separate... (3 Replies)
Discussion started by: Tlcm sam
3 Replies

7. Shell Programming and Scripting

Merging Lines based on criteria

Hello, Need help with following scenario. A file contains following text: {beginning of file} New: This is a new record and it is not on same line. Since I have lost touch with script take this challenge and bring all this in one line. New: Hello losttouch. You seem to be struggling... (4 Replies)
Discussion started by: losttouch
4 Replies

8. Shell Programming and Scripting

Print lines that match certain criteria

Hi all I have a text file with the following format: id col1 col2 col3 col4 col5 col6 col7 ... row1 0 0 0 0 0 0 0 row2 0 0 0 0 0 0 0 row3 0 0 0 0 0 0.2 0 row4 0 0 0 0 0 0 0 row5 0 0 0 0 0 0 0 row6 0 0 0 0.1 0 0 0 row7 0 0 0 0 0 0 0 row8 0 0 0 0 0 0 0 row9 0 0 0 0 0 0 0 ... The file... (2 Replies)
Discussion started by: gautig
2 Replies

9. Shell Programming and Scripting

Need help in adding the column to set of lines that match the certain criteria

Hi, I need help to add a certain values to end of each line in the same file. Here is my input file name:aaa id :123 salary job qulification name:bbb id :124 salary job qulification name:ccc id :345 salary job qulification (2 Replies)
Discussion started by: jpkumar10
2 Replies

10. Shell Programming and Scripting

remove lines based on score criteria

Hi guys, Please guide for Solution. PART-I INPUT FILE (has 2 columns ID and score) TC5584_1 93.9 DV161411_2 79.5 BP132435_5 46.8 EB682112_1 34.7 BP132435_4 29.5 TC13860_2 10.1 OUTPUT FILE (It shudn't contain the line ' BP132435_4 29.5 ' as BP132435 is repeated... (2 Replies)
Discussion started by: smriti_shridhar
2 Replies
Login or Register to Ask a Question