(n)awk: print regex search output lines in one line

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting (n)awk: print regex search output lines in one line
# 1  
Old 03-24-2016
(n)awk: print regex search output lines in one line

I have been looking high and low for the solution for this. I seems there should be a simple answer, but alas.

I have a big xml file, and I need to extract certain information from specific items. The information I need can be found between a specific set of tags. let's call them <tag></tag>. Doing a regex lookup yields 4 lines of text, which I need to be 1 line, and this has to happen with every snippet that's between those tags (about 700 or so <tag></tag> elements in the xml file).

So, if I do this:
awk -F"tag" '/ref1/;/ref2/;/ref3/;/ref4/ {print}' file.xml

the output I get is:
<ref1="this is ref1"/>
<re2="this is ref2" />
<ref3="this is ref3" />
<ref4="this is ref4 />
<ref1="this is ref1"/>
<re2="this is ref2" />
<ref3="this is ref3" />
<ref4="this is ref4 />
. . .

The output I need:
<ref1="this is ref1"/><re2="this is ref2" /><ref3="this is ref3" /><ref4="this is ref4 />
<ref1="this is ref1"/><re2="this is ref2" /><ref3="this is ref3" /><ref4="this is ref4 />
. . .

Note: I am in a very secure environment, so I cannot install anything, so I only have the regular awk/nawk/gawk and sed that comes with Solaris 11.

Any help would be greatly appreciated.
# 2  
Old 03-24-2016
Assuming the tags are in order as shown in your sample, the following should work:
awk '/ref1/{r1=$0};/ref2/{r2=$0};/ref3/{r3=$0};/ref4/{print r1 r2 r3 $0}' file.xml

Note, however, that your code (and this trivial modification of it) are not just matching tag names, they are matching tag names and text that is not part of the tag name (in the case of ref2, it is only matching text since the tag name is re2 instead of ref2).

Of course, you could also use:
grep 'ref[1-4]' file.xml | paste -d '\0\0\0\n' - - - -

awk '/ref[1-4]/' file.xml | paste -d '\0\0\0\n' - - - -

Last edited by Don Cragun; 03-24-2016 at 10:00 PM.. Reason: Add grep/paste alternative
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 03-25-2016
Piping to paste worked like a charm

Thank you.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk script to search output for a value and print

GOODNUMBERS="1 2 3 4 5 6 3 3 34 34 5 66 12" BADNUMBERS="7 3 12 5 66" for eachnum in `echo ${GOODNUMBERS}` do echo ${BADNUMBERS} | gawk -v threshold=${eachnum} '$1 != threshold' done what im trying to do with the above is, i want to print numbers that are in the GOODNUMBERS... (10 Replies)
Discussion started by: SkySmart
10 Replies

2. Shell Programming and Scripting

String search and print next all lines in one line until blank line

Dear all I want to search special string in file and then print next all line in one line until blank lines come. Help me plz for same. My input file and desire op file is as under. i/p file: A1/EXT "BSCABD1_21233G1" 757 130823 1157 RADIO X-CEIVER ADMINISTRATION BTS EXTERNAL FAULT ... (7 Replies)
Discussion started by: jaydeep_sadaria
7 Replies

3. Shell Programming and Scripting

To print lines between 2 timestamps using awk|sed and regex

Hi, I am using the following code to fetch lines that are generated in last 1 hr . Hence, I am using date function to calculate -last 1 hr & the current hr and then somehow use awk (or sed-if someone could guide me better) with some regex pattern. dt_1=`date +%h" "%d", "%Y\ %l -d "1 hour... (10 Replies)
Discussion started by: sarah-alikhan31
10 Replies

4. Shell Programming and Scripting

Search string and print the above line and below lines?.

if the first string matches then print the previous line and current line and also print the following lines if the other string search matches. Input ------ TranTime 2012 10 12 The Record starts here Accountnumber: 4632473431274 TxnCode 323 TranID 329473242834 ccsdkcnsdncskd... (7 Replies)
Discussion started by: laknar
7 Replies

5. Shell Programming and Scripting

Search words in a line and print next 15 lines.

I have a text file ( basically a log file) and i have 2 words (alpha, beta), Now i want to search these two words in one line and then print next 15 lines in a temp file. there would be many lines with alpha and beta But I need only last occurrence with "alpha" and "beta" and next 15 lines. ... (4 Replies)
Discussion started by: kashif.live
4 Replies

6. Shell Programming and Scripting

Print lines after the search string until blank line is found

All I want is to look for the pattern in the file...If I found it at # places... I want print lines after those pattern(line) until I find a blank line. Log EXAMPLE : MT:Exception caught The following Numbers were affected: 1234 2345 2346 Error java.lang.InternalError:... (3 Replies)
Discussion started by: prash184u
3 Replies

7. Shell Programming and Scripting

awk print second line after search string

I have multiple config files where I need to pull the ip address from loopback3. The format is the same in every file, the ip is the second line after interface loopback3. interface loopback2 loopback description router ID ip address interface loopback3 loopback description... (3 Replies)
Discussion started by: numele
3 Replies

8. Shell Programming and Scripting

awk search column, print line

Hello. I've been banging my head against walls trying to search a comma delimited file, using awk. I'm trying to search a "column" for a specific parameter, if it matches, then I'd like to print the whole line. I've read in multiple texts: awk -F, '{ if ($4 == "string") print $0 }'... (2 Replies)
Discussion started by: Matthias03
2 Replies

9. Shell Programming and Scripting

How to use sed to search for string and Print previous two lines and current line

Hello, Can anybody help me to correct my sed syntax to find the string and print previous two lines and current line and next one line. i am using string as "testing" netstat -v | sed -n -e '/test/{x;2!p;g;$!N;p;D;}' -e h i am able to get the previous line current line next line but... (1 Reply)
Discussion started by: nmadhuhb
1 Replies

10. Shell Programming and Scripting

using awk to search and print output

suppose i have one file file A 18 24 30 35 38 45 55 Another file file B 08_46 A 16 V -0.36 0.23 E : 1.41 08_46 A 17 D -1.04 0.22 E : 0.84 08_46 A 18 Q -0.49 0.12 E : 0.06 08_46 A 19 G 0.50 0.14 E : 0.05 08_46 A 20 V ... (5 Replies)
Discussion started by: cdfd123
5 Replies
Login or Register to Ask a Question

Featured Tech Videos