Help With AWK Matching and Re-printing Lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help With AWK Matching and Re-printing Lines
# 1  
Old 05-03-2012
Help With AWK Matching and Re-printing Lines

Hi All,

I'm looking to use AWK to pattern match lines in XML file - Example patten for below sample would be /^<apple>/
The sample I wrote out is very basic compared to what I am actually working with but it will get me started
I would like to keep the matched line(s) unchanged but have them re-printed third line up from the bottom of file
If a previous line was already printed it would just get pushed up one by the next line to be printed.
There will always be at least 1 line to match but any number is possible after that
The newly re-printed line(s) tags need to be unique - a number or anything, really, would work.
I'm using GNU AWK on Windows XP to be put in a batch script. One-liner would be great if possible.
I've ordered a couple AWK books but by the time I figure this out I'll be an older old man.
Thank you in advance for any ideas.

Input:

Code:
<Request>
<Products>
<orange>Florida</orange>
<apple>Macintosh</apple>
<banana>Chiquita</banana>
<pineapple>Dole</pineapple>
<apple>Granny Smith</apple>
<corn>Green Giant</corn>
<peas>Birdseye</peas>
</Products>
</Request>

Output:

Code:
<Request>
<Products>
<orange>Florida</orange>
<apple>Macintosh</apple>
<banana>Chiquita</banana>
<pineapple>Dole</pineapple>
<apple>Granny Smith</apple>
<corn>Green Giant</corn>
<peas>Birdseye</peas>
<apple1>Macintosh</apple1>
<apple2>Granny Smith</apple2>
</Products>
</Request>

# 2  
Old 05-03-2012
Hi, try this:
Code:
awk '$1==s{$1=$1m;m++}1' s=apple RS=\< FS=\> ORS=\< OFS=\> infile

# 3  
Old 05-04-2012
Hi Scrutinizer,

Thanks for the help. This is what I am getting --

Code:
H:\>awk $1==s{$1=$1m;m++}1 s="apple" RS="<" FS=">" ORS="<" OFS=">" C:\test4.txt
<Request>
<Products>
<orange>Florida</orange>
<apple>Macintosh</apple>
<banana>Chiquita</banana>
<pineapple>Dole</pineapple>
<apple1>Granny Smith</apple>
<corn>Green Giant</corn>
<peas>Birdseye</peas>
</Products>
</Request><
H:\>


Last edited by Scrutinizer; 05-04-2012 at 08:56 AM.. Reason: code tags
# 4  
Old 05-04-2012
Perhaps works better:
Code:
awk '$1=="/"s || $1==s{$1=$1m; if(/^\//) m++ }1' s=apple RS=\< FS=\> ORS=\< OFS=\> infile

# 5  
Old 05-04-2012
Yes, this adds the digit to the closing tag now. Thanks.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Continued trouble matching fields in different files and selective field printing ([g]awk)

I apologize in advance, but I continue to have trouble searching for matches between two files and then printing portions of each to output in awk and would very much appreciate some help. I have data as follows: File1 PS012,002 PRQ 0 1 1 17 1 0 -1 3 2 1 2 -1 ... (7 Replies)
Discussion started by: jvoot
7 Replies

2. Shell Programming and Scripting

awk to combine matching lines in file

I am trying to combine all matching lines in the tab-delimited using awk. The below runs but no output results. Thank you :). input chrX 110925349 110925532 ALG13 chrX 110925349 110925532 ALG13 chrX 110925349 110925532 ALG13 chrX 47433390 47433999 SYN1... (3 Replies)
Discussion started by: cmccabe
3 Replies

3. Shell Programming and Scripting

awk - printing new lines based of 2 dates

I have some test data that is seperated out into annual records, each record has a start date (COL7), an end date (COL8) and a maturity date (COL18) - What I need to do is ensure that there is one record to cover each year right up until Maturity date (COL18). In the first group of the below... (10 Replies)
Discussion started by: Ads89
10 Replies

4. Shell Programming and Scripting

UNIX awk pattern matching and printing lines

I have the below plain text file where i have some result, in order to mail that result in html table format I have written the below script and its working well. cat result.txt Page 2015-01-01 2000 Colors 2015-02-01 3000 Landing 2015-03-02 4000 #!/bin/sh LOG=/tmp/maillog.txt... (1 Reply)
Discussion started by: close2jay
1 Replies

5. Shell Programming and Scripting

Sed: printing lines AFTER pattern matching EXCLUDING the line containing the pattern

'Hi I'm using the following code to extract the lines(and redirect them to a txt file) after the pattern match. But the output is inclusive of the line with pattern match. Which option is to be used to exclude the line containing the pattern? sed -n '/Conn.*User/,$p' > consumers.txt (11 Replies)
Discussion started by: essem
11 Replies

6. Shell Programming and Scripting

Matching and printing line with awk

Hi there, I'm trying to use awk to print out the entire line that contains a match to a certain regex and then append some text,plus the match to the end of the line. So far I have: awk -F: '{print "RG:Z:" $2}' file Which prints out the match I want plus the additional text, but I'm stuck... (3 Replies)
Discussion started by: jim_lad
3 Replies

7. Shell Programming and Scripting

fgrep not printing non matching lines

I'm using this: fgrep -f file1.txt file2.txt To find lines in file1 that match patterns found in file2. When I add -v: egrep -v -f file1.txt file2.txt It won't return non matching lines, I just get a blank. Can anyone help? PS. file1.txt contains 3 million lines...each string... (2 Replies)
Discussion started by: Nonito84
2 Replies

8. Shell Programming and Scripting

Printing entire field, if at least one row is matching by AWK

Dear all, I have been trying to print an entire field, if the first line of the field is matching. For example, my input looks something like this. aaa ddd zzz 123 987 126 24 0.650 985 354 9864 0.32 0.333 4324 000 I am looking for a pattern,... (5 Replies)
Discussion started by: Chulamakuri
5 Replies

9. Shell Programming and Scripting

printing two lines in awk as two columns in excel

hi guys, i would like to print two lines from a file as two adjacent columns using excel using awk.. i have this so far: awk '{for(i=1; i<=NF; i++) {printf("%s\n",$i)}}' "$count".ttt > "$count".csv #this to print the first line from the .ttt file as rows of the first column in the .csv... (9 Replies)
Discussion started by: npatwardhan
9 Replies

10. Shell Programming and Scripting

Printing lines with specific awk NF

I have this files: ./frm/lf_mt1_cd.Ic_cell_template.attr ./die/addgen_tb_pumd.Ic_cell_template.attr ./min_m1_n.Ic_cell_template.attr When I use: awk -F\/ '{print NF}' Would result to: 3 3 2 I would like to list the files with 3 fields on it. Any Suggestions? (1 Reply)
Discussion started by: jehrome_rando
1 Replies
Login or Register to Ask a Question