Visit Our UNIX and Linux User Community


awk to print the line that matches and the next if line is wrapped


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk to print the line that matches and the next if line is wrapped
# 1  
Old 07-17-2014
awk to print the line that matches and the next if line is wrapped

I have a file and when I match the word "initiators" in the first column I need to be able to print the rest of the columns in that row. This is fine for the most part but on occasion the "initiators" line gets wrapped to the next line. Here is a sample of the file.

Code:
caw-enabled               true
controller-tag            -
initiators                [NETIK0102_UCS_Boot_a, NETIK0102_UCS_Boot_b]
operational-status        ok
port-name-enabled-status  [P00000000476043B3-A0-FC01,true,ok, P00000000476046CC-A0-FC00,true,ok,
                          P00000000477043B3-B0-FC00,true,ok, P00000000477046CC-B0-FC01,true,ok]
ports                     [P00000000476043B3-A0-FC01, P00000000476046CC-A0-FC00,
                          P00000000477043B3-B0-FC00, P00000000477046CC-B0-FC01]
caw-enabled               true
controller-tag            -
initiators                [NETIK0102_UCS_HBA0_a, NETIK0102_UCS_HBA1_b, NETIK0102_UCS_HBA2_a,
                          NETIK0102_UCS_HBA3_b]
operational-status        ok
port-name-enabled-status  [P00000000476043B8-A0-FC02,true,ok, P0000000047604458-A0-FC03,true,ok,
                          P00000000477043B8-B0-FC03,true,ok, P0000000047704458-B0-FC02,true,ok]
ports                     [P00000000476043B8-A0-FC02, P0000000047604458-A0-FC03,
                          P00000000477043B8-B0-FC03, P0000000047704458-B0-FC02]

How do I print the initiator info when it is matched in the 2nd instance above? I would like the ouput to be like this.

Code:
 
initiators [NETIK0102_UCS_Boot_a, NETIK0102_UCS_Boot_b]
initiators [NETIK0102_UCS_HBA0_a, NETIK0102_UCS_HBA1_b, NETIK0102_UCS_HBA2_a, NETIK0102_UCS_HBA3_b]

---------- Post updated at 11:43 AM ---------- Previous update was at 11:04 AM ----------

I am able to extract the info I need but I want it to print on the same line. I am using

Code:
cat file | awk '/initiators/,/operational-status/ {print $0}' | grep -v operational-status

And this gets me the output in this format.

Code:
initiators                [NETIK0102_UCS_Boot_a, NETIK0102_UCS_Boot_b]
initiators                [NETIK0102_UCS_HBA0_a, NETIK0102_UCS_HBA1_b, NETIK0102_UCS_HBA2_a,
                          NETIK0102_UCS_HBA3_b]

But I want it to output in the following format

Code:
initiators                [NETIK0102_UCS_Boot_a, NETIK0102_UCS_Boot_b]
initiators                [NETIK0102_UCS_HBA0_a, NETIK0102_UCS_HBA1_b, NETIK0102_UCS_HBA2_a,NETIK0102_UCS_HBA3_b]

---------- Post updated at 12:02 PM ---------- Previous update was at 11:43 AM ----------

Seems like a roudabout way to do it but I got it working by using the following command. Alternatives are welcome.

Code:
cat file | awk '/initiators/,/operational-status/ {print $0}' | grep -v operational-status | sed 's/ //g' | sed 's/initiators/initiators    /g' | nawk '/,$/ {printf "%s",substr($0, 1, length-1);next} 1'

# 2  
Old 07-17-2014
Try this one:
Code:
cat file | tr '\n' '$' | sed 's/$                          / /g' | tr '$' '\n' | awk '$1 == "initiators" {print}'

Note that this assumes that "$" does not appear in your text. If it does, find some character that isn't in your text and replace all $'s in that line with that new character.

Last edited by blakeoft; 07-17-2014 at 09:17 AM.. Reason: Added a space in the sed command
# 3  
Old 07-17-2014
Try also
Code:
awk '/initi/ {if (!sub(/]$/,"&")) {getline X; gsub(/^ +/," ", X); $0=$0 X}; print}' file
initiators                [NETIK0102_UCS_Boot_a, NETIK0102_UCS_Boot_b]
initiators                [NETIK0102_UCS_HBA0_a, NETIK0102_UCS_HBA1_b, NETIK0102_UCS_HBA2_a, NETIK0102_UCS_HBA3_b]

This User Gave Thanks to RudiC For This Post:
# 4  
Old 07-18-2014
Thanks. That works great!

Previous Thread | Next Thread
Test Your Knowledge in Computers #488
Difficulty: Medium
Some languages are interpreted, not compiled, and the source code is contained in a file usually referred to as a text-based assembly unit.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print whole line if variables matches

Der colleagues, 4 days I am trying to solve my issue and no success.. Maybe you can give me a clue how to achieve what I need.. So I have two files. file1 example: 1_column1.1 1_column2.1 aaa 1_column4.1 1_column1.2 1_column2.2 ttt 1_column4.2 1_column1.3 1_column2.3 ... (10 Replies)
Discussion started by: nypreH
10 Replies

2. Linux

Print line 1 if line 3 matches of the output

Hi I want to extend following command so that on the basis of "Branch: ****" on the third line I can grep and print name of the file on the first line. cat .labellog.emd | grep DA2458A7962276A7E040E50A0DC06459 | cut -d " " -f2 | grep -v branch_name | xargs -I file <command to describe> file ... (1 Reply)
Discussion started by: ezee
1 Replies

3. Shell Programming and Scripting

How to print line if two lines above it matches patterns.?

Hi, I could only find examples to print line before/after a match, but I'd need to print line after two separate lines matching. E.g.: From the below log entry, I would need to print out the 1234. This is from a huge log file, that has a lot of entries with "CLIENT" and "No" entries (+ other... (3 Replies)
Discussion started by: Juha
3 Replies

4. Shell Programming and Scripting

Using regex's from file1, print line and line after matches in file2

Good day, I have a list of regular expressions in file1. For each match in file2, print the containing line and the line after. file1: file2: Output: I can match a regex and print the line and line after awk '{lines = $0} /Macrosiphum_rosae/ {print lines ; print lines } ' ... (1 Reply)
Discussion started by: pathunkathunk
1 Replies

5. UNIX for Dummies Questions & Answers

print line that matches and next line, too

I'm using sh on hp-ux and want to find / print a line that matches 132.101- and the next line, too. grep -A isn't supported on hp-ux, so I'm trying awk and sed. The code below works but only prints the first occurence. I need all matches from the file. awk... (2 Replies)
Discussion started by: Scottie1954
2 Replies

6. UNIX for Advanced & Expert Users

Print line if subsrt matches array

Hi Folks! im printing all lines where the characters in position 270-271 match 33|H1|HA|KA|26 so i came up with this #!/bin/bash array=(33 H1 HA KA 26 ) for i in "${array}" do #echo $i awk '{ if (substr($0,270,2)~'/$i/') print; }' $1 >> $1.temp done It works fine . but... (2 Replies)
Discussion started by: phpsnook
2 Replies

7. Shell Programming and Scripting

How to print line if field matches?

Hi all, I got several lines line this a b c d e 1 e a 1 c d e 3 f a b c 1 e 8 h a b c d e 1 w a 1 c d e 2 w a b c d e 1 t a b c d e 7 4 How can I print the line if 1 is the field one before the last field? Basicly this 2 field ? a b c d e 1 e a b c d e 1 t The file I got is... (7 Replies)
Discussion started by: stinkefisch
7 Replies

8. Shell Programming and Scripting

Print word 1 in line 1 and word 2 in line 2 if it matches a pattern

i have a file in this pattern MATCH1 word1 IMAGE word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH1 word1 IMAGE word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1... (7 Replies)
Discussion started by: bangaram
7 Replies

9. Shell Programming and Scripting

Print line if first Field matches a pattern

Hi All, I would like my code to be able to print out the whole line if 1st field has a dot in the number. Sample input and expected output given below. My AWK code is below but it can;t work, can any expert help me ? Thanks in advance. {if ($1 ~ /*\.*/) { print $0 }} Input: ... (2 Replies)
Discussion started by: Raynon
2 Replies

10. Shell Programming and Scripting

print next line if matches a particular word..need help

Hi i need a help for making a script whch can print next line if it matches a particular word like file1 have ename Mohan eid 2008 ename Shyam eid 345 if scipt got Mohan it will print next line (eid 2008) pls help me .......:) (2 Replies)
Discussion started by: anish19
2 Replies

Featured Tech Videos