Print values within groups of lines with awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print values within groups of lines with awk
# 1  
Old 10-23-2015
Print values within groups of lines with awk

Hello to all,

I'm trying to print the value corresponding to the words A, B, C, D, E. These words could appear sometimes and sometimes not inside each group of lines. Each group of lines begins with "ZYX".

My issue with current code is that should print values for 3 groups and only is printing for 2 groups (group 1 and group 3) and I'm not sure why.

What is missing in my current code to fix this?

The current output is:
Code:
123|3|22|56|881
711||988||444

and desired ouput:
Code:
123|3|22|56|881
332|453|11||
711||988||444

My input file is:
Code:
ZYX
A = 123
B = 3
C = 22
D = 56
E = 881
ZYX
A = 332
B = 453
C = 11
ZYX
A = 711
C = 988
E = 444

My current code is:
Code:
awk '/ZYX/{a="";b="";c="";d="";e=""} 
         /A/ {a=$3}
         /B/ {b=$3}
         /C/ {c=$3} 
         /D/ {d=$3}
         /E/ {e=$3; print a"|"b"|"c"|"d"|"e}' file


Thaaks for any help.

Regards
# 2  
Old 10-23-2015
It prints when E is met. If there is no E nothing is printed.
Instead it must print at the end of each block, that is either when ZYX is met or at the END, and if it's not line 1.
Consider a
Code:
function prt() {if (NR>1) print a"|"b"|"c"|"d"|"e}

These 2 Users Gave Thanks to MadeInGermany For This Post:
# 3  
Old 10-23-2015
Thanks MadeInGermany, your're right.

I've added the END and print when each block begins and at the END and works now and adding the function as you sugested.

Code:
awk '
 function prt(){if (NR>1) {print a"|"b"|"c"|"d"|"e; a="";b="";c="";d="";e=""}}
 /ZYX/{prt()}
         /A/ {a=$3}
         /B/ {b=$3}
         /C/ {c=$3}
         /D/ {d=$3}
         /E/ {e=$3}
 END{prt()}' file.txt

Thanks again.

Best regards
This User Gave Thanks to Ophiuchus For This Post:
# 4  
Old 10-24-2015
Thank you for sharing your results. It will help other people reading this thread understand how you solved your problem.

For future reference, note that as long as a list of variables are all being set to the same value in awk, you can simplify the construct:
Code:
a="";b="";c="";d="";e=""

to just:
Code:
a=b=c=d=e=""

These 2 Users Gave Thanks to Don Cragun For This Post:
# 5  
Old 10-24-2015
Hi could you please explain how your code is working , specially
PHP Code:
a="";b="";c="";d="";e="" 
Thanks,
# 6  
Old 10-24-2015
Quote:
Originally Posted by looney
Hi could you please explain how your code is working , specially
PHP Code:
a="";b="";c="";d="";e="" 
Thanks,
When a header is found, all of the data fields are cleared (i.e., set to empty strings) by the above statement.
The individual data fields are filled in as they are found by the statements on the following lines in the script. All of the fields have to be cleared so data from an earlier header isn't printed as data from a later header that did not contain entries for some fields.
# 7  
Old 10-25-2015
Quote:
Originally Posted by Don Cragun
Thank you for sharing your results. It will help other people reading this thread understand how you solved your problem.

For future reference, note that as long as a list of variables are all being set to the same value in awk, you can simplify the construct:
Code:
a="";b="";c="";d="";e=""

to just:
Code:
a=b=c=d=e=""

Hi Don,

Excellent advice. Thanks so much for comment and add extra information even when the question was solvedSmilie.

With your advice the code looks like this:
Code:
awk '
 function prt(){if (NR>1) {print a"|"b"|"c"|"d"|"e; a=b=c=d=e=""}}
 /ZYX/{prt()}
         /A/ {a=$3}
         /B/ {b=$3}
         /C/ {c=$3}
         /D/ {d=$3}
         /E/ {e=$3}
 END{prt()}' file.txt

Best regards
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Print lines based upon unique values in Nth field

For some reason I am having difficulty performing what should be a fairly easy task. I would like to print lines of a file that have a unique value in the first field. For example, I have a large data-set with the following excerpt: PS003,001 MZMWR/ L-DWD// * PS003,001... (4 Replies)
Discussion started by: jvoot
4 Replies

2. Shell Programming and Scripting

How to print lines that have values in certain columns ?

Hi, everyone I have a dataset like this: A B C D A C C D E F G H F D K Y X A K K C Gsome of columns have no values in each line. I want to print all lines that have 1/2/3/4 values, export separately to four files. What I expected is like this: file1 Y file 2 A C X Afile 3... (3 Replies)
Discussion started by: nengcheng
3 Replies

3. UNIX for Dummies Questions & Answers

awk - (URGENT!) Print lines sort and move lines if match found

URGENT HELP IS NEEDED!! I am looking to move matching lines (01 - 07) from File1 and 77 tab the matching string from File2, to File3.txt. I am almost done but - Currently, script is not printing lines to File3.txt in order. - Also the matching lines are not moving out of File1.txt ... (1 Reply)
Discussion started by: High-T
1 Replies

4. Shell Programming and Scripting

awk print values between consecutive lines

I have a file in below format: file01.txt TERM TERM TERM ABC 12315 68.53 12042013 165144 ABC 12315 62.12 12042013 165145 ABC 12315 122.36 12052013 165146 ABC 12315 582.18 12052013 165147 ABC 12316 2.36 12052013 165141 ABC 12316 ... (8 Replies)
Discussion started by: alex2005
8 Replies

5. Shell Programming and Scripting

awk Print New Column For Every Two Lines and Match On Multiple Column Values to print another column

Hi, My input files is like this axis1 0 1 10 axis2 0 1 5 axis1 1 2 -4 axis2 2 3 -3 axis1 3 4 5 axis2 3 4 -1 axis1 4 5 -6 axis2 4 5 1 Now, these are my following tasks 1. Print a first column for every two rows that has the same value followed by a string. 2. Match on the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

6. Shell Programming and Scripting

How to print in awk matching $1 values ,to $1,$4 example given.?

Hi Experts, I am trying to get the output from a matching pattern but unable to construct the awk command: file : aa bb cc 11 dd aa cc 33 cc 22 45 68 aa 33 44 44 dd aa cc 37 aa 33 44 67 I want the output to be : ( if $1 match to "aa" start of the line,then print $4 of that line, and... (3 Replies)
Discussion started by: rveri
3 Replies

7. Shell Programming and Scripting

Print lines containing same values in a group

Hi, I have a table like this: Name A1 A2 A3 B1 B2 B3 111 abc abc abc cbc cbc cbc 222 acv acv n_n bbc bbc bbc 333 bvv bvv bvv cBx ccx ccx 444 ttk ttk ttk kke kke kke 555 mcr mcr mcr mcr mcr mcr The 1st column is just names... (3 Replies)
Discussion started by: polsum
3 Replies

8. UNIX for Dummies Questions & Answers

Only print lines with 3 numeric values

Hey guys & gals, I am hoping for some advice on a sed or awk command that will allow to only print lines from a file that contain 3 numeric values. From previous searches here I saw that ygemici used the sed command to remove lines containing more than 3 numeric values ; however how... (3 Replies)
Discussion started by: TAPE
3 Replies

9. Shell Programming and Scripting

awk- looping through groups of lines

Hello, I'm working with a file that has three columns. The first one represents a certain channel and the third one a timestamp (second one is not important). Example input is as follows: 2513 12 10.771 2513 13 10.771 2513 14 10.771 2513 15 10.771 2644 8 10.771 ... (6 Replies)
Discussion started by: acsg
6 Replies

10. Shell Programming and Scripting

Print a key with its all values using awk/others

input COL1 a1 b1 c1 d1 e1 f1 C1 10 10 10 100 100 1000 C2 20 20 200 200 200 2000 output C1 a1 10 1 C1 b1 10 1 C1 c1 10 1 C1 d1 100 2 C1 e1 100 2 C1 f1 1000 3 C2 ... (12 Replies)
Discussion started by: ruby_sgp
12 Replies
Login or Register to Ask a Question