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!

Selecting Records from Data 1

Status
Not open for further replies.

thunderkid

Technical User
Oct 26, 2000
54
US
I am a novice at awk. I have searched database for script that I might modify, but have not had any success. I am trying to selectively picked records from data file. Here is a sample of input file:

123 <time1_data> aaaa
123 <time2_data> aaaa
234 <time3_data> aaaa
234 <time4_data> aaaa
234 <time5_data> aaaa
345 <time6_data> aaaa
345 <time7_data> aaaa
345 <time8_data> aaaa
345 <time9_data> aaaa
.
.
.

desired output:

The first extraction should pick the first record when col one fields are the same:
123 <time1_data> aaaa
234 <time3_data> aaaa
345 <time6_data> aaaa


In another extraction I need to pick the second record when col one fields are the same:
123 <time2_data> aaaa
234 <time4_data> aaaa
345 <time7_data> aaaa

These are two separate extractions. Nice to have one script which can be modified to do second extraction.

thanks
thunderkid
 
Try something like this:
awk -v ind=1or2 '++a[$1]==ind' </path/to/file

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Assuming you want to print only when two or more records in sequence have field 1 the same:
[tt]
$1 && $1==prev { count++
if (1==count)
print show2nd ? $0 : prev_rec }
$1!=prev { count=0 }
{prev=$1; prev_rec=$0}
[/tt]
To make it show 2nd line, use [tt]awk -v show2nd=1[/tt].

If you have nawk, use it instead of awk because
on some systems awk is very old and lacks many useful features.

Let me know whether or not this helps.


 
PHV
Your solutions worked great! You guys really make this forum such a valuable resource for sharing knowledge and spreading the usefulness of "awk". Thanks to you and others that make it possible.
thunderkid

Have a star!
 
Feedback for futurelet. As you indicated in your response, your solution works for 2 or more. I really needed it to handle all cases.

I was not able to get second script (writes 2nd record) to work:
awk -v show2nd=1

Thanks.
thunderkid
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top