Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Picking the Right Records 1

Status
Not open for further replies.

thunderkid

Technical User
Oct 26, 2000
54
US
I am trying to modify a current awk script that PHV provided on an earlier thread (271-801668). I have provided my current task and PHV’s script for earlier task.

Here is sample of input file:

123 <time1_data> xxxx bbb1 cccc1 dddd1
123 <time2_data> xxxx bbb1 cccc1 dddd1
234 <time3_data> xxxx bbb2 cccc2 dddd2
234 <time4_data> xxxx bbb2 cccc2 dddd2
234 <time5_data> xxxx bbb2 cccc2 dddd2
345 <time6_data> xxxx bbb3 cccc3 dddd3
345 <time7_data> xxxx bbb3 cccc3 dddd3
345 <time8_data> xxxx bbb3 cccc3 dddd3
123 <time1_data> yyyy bbb1 cccc1 dddd1
123 <time2_data> yyyy bbb1 cccc1 dddd1
234 <time3_data> yyyy bbb2 cccc2 dddd2
234 <time4_data> yyyy bbb2 cccc2 dddd2
234 <time5_data> yyyy bbb2 cccc2 dddd2
345 <time6_data> yyyy bbb3 cccc3 dddd3
345 <time7_data> yyyy bbb3 cccc3 dddd3
345 <time8_data> yyyy bbb3 cccc3 dddd3
345 <time9_data> yyyy bbb3 cccc3 dddd3
.
.

desired output:

The first extraction should pick the first record when fields 1,3,4,5 & 6 are the same:
123 <time1_data> xxxx bbb1 cccc1 dddd1
234 <time3_data> xxxx bbb2 cccc2 dddd2
345 <time6_data> xxxx bbb3 cccc3 dddd3
123 <time1_data> yyyy bbb1 cccc1 dddd1
234 <time3_data> yyyy bbb2 cccc2 dddd2
345 <time6_data> yyyy bbb3 cccc3 dddd3

In another extraction I need to pick the second record when fields 1,3,4,5 & 6 are the same:
123 <time2_data> xxxx bbb2 cccc2 dddd2
234 <time4_data> xxxx bbb2 cccc2 dddd2
345 <time7_data> xxxx bbb3 cccc3 dddd3
123 <time2_data> yyyy bbb2 cccc2 dddd2
234 <time4_data> yyyy bbb2 cccc2 dddd2
345 <time7_data> yyyy bbb3 cccc3 dddd3


The following awk script worked great when I was only used fields 1 & 2

awk -v ind=1or2 '++a[$1]==ind' </path/to/file

Now I would like to build script to compare fields as described above. I have tried to modify this script but have not had any success. Thanks for your inputs.

thunderkid
 
Try something like this:
awk -v ind=1or2 '{
b=$1""$3""$4""$5""$6;if(++a==ind)print
}' </path/to/file

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
You are the man! The script worked great. I was able to apply the script and reduce original script (482 lines) down to 14 lines! Thanks PHV. Have a star!

thunderkid
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top