|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !! |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
keeping last record among group of records with common fields (awk)
input: Code:
ref.1;rack.1;1 #group1 ref.1;rack.1;2 #group1 ref.1;rack.2;1 #group2 ref.2;rack.3;1 #group3 ref.2;rack.3;2 #group3 ref.2;rack.3;3 #group3 Among records from same group (i.e. with same 1st and 2nd field - separated by ";"), I would need to keep the last record (or the record with the highest number in the last field, which is the same here). in order to get: Code:
ref.1;rack.1;2 ref.1;rack.2;1 ref.2;rack.3;3 I think I managed to isolate records in groups by doing Code:
BEGIN{FS=OFS=";"}
{array[$1$2] = $0
for(a in array)
if(<$0 is last record>) print array[a]}but I don't know how to say "the last record". I tried with NR but it didn't really help... |
| Sponsored Links | ||
|
|
#2
|
||||
|
||||
|
Quote:
And, setting OFS is useless in this case. Code:
BEGIN{FS=";"}
{array[$1,$2]=$0}
END{for(a in array) print array[a]} |
| The Following User Says Thank You to elixir_sinari For This Useful Post: | ||
beca123456 (10-07-2012) | ||
| Sponsored Links | ||
|
|
#3
|
|||
|
|||
|
Thanks elixir_sinari. I got it now for this way !
And what about if I wanted to keep the highest value in the last field instead of the last line (which is the same here but just for me to know)? |
|
#4
|
||||
|
||||
|
Assuming only positive values in the third field, you may try: Code:
BEGIN{FS=";"}
$3>=highest[$1,$2]{maxrec[$1,$2]=$0;highest[$1,$2]=$3}
END{
for(i in maxrec)
print maxrec[i]
}Last edited by elixir_sinari; 10-07-2012 at 03:11 AM.. |
| The Following User Says Thank You to elixir_sinari For This Useful Post: | ||
beca123456 (10-07-2012) | ||
| Sponsored Links | |
|
|
#5
|
|||
|
|||
|
Here are some options for you. This script provides several ways of processing the input file giving different results depending on whether you want the highest value for $3 or the last value for $3, all entries with matching field 1 and field 2 values adjacent or spread throughout the input file, and whether or not you care if the output order is the same as the input file order: Code:
#!/bin/ksh
printf "Following assumes all entries with matching field 1 & field 2 are
adjacent and prints the last entry found.\n"
awk 'BEGIN {FS = OFS = ";"}
last != $1 FS $2 {
if(last != "") print last, hi3
last = $1 FS $2
hi3 = $3
next
}
{hi3 = $3}
END { if(last != "") print last, hi3}' input
printf "\nFollowing assumes all entries with matching field 1 & field 2 are
adjacent and prints the entry with highest value in field 3.\n"
awk 'BEGIN {FS = OFS = ";"}
last != $1 FS $2 {
if(last != "") print last, hi3
last = $1 FS $2
hi3 = $3
next
}
{if($3 > hi3) hi3 = $3}
END { if(last != "") print last, hi3}' input
printf "\nFollowing assumes entries with matching field 1 & field 2 might not be
adjacent and prints the last entry found. Output order is not guaranteed to
match the order of first appearance in the input file.\n"
awk 'BEGIN {FS = OFS = ";"}
{ k3[$1 FS $2] = $3}
END { for (k in k3) print k, k3[k]}' input
printf "\nFollowing assumes entries with matching field 1 & field 2 might not be
adjacent and prints the highest entry found. Output order is not guaranteed to
match the order of first appearance in the input file.\n"
awk 'BEGIN {FS = OFS = ";"}
{ if(kc[$1 FS $2]++ == 0) k3[$1 FS $2] = $3
else if($3 > k3[$1 FS $2]) k3[$1 FS $2] = $3
}
END { for (k in kc) print k, k3[k]}' input
printf "\nFollowing assumes entries with matching field 1 & field 2 might not be
adjacent and prints the highest entry found. Output order is guaranteed to
match the order of first appearance in the input file.\n"
awk 'BEGIN {FS = OFS = ";"}
{ if(kc[$1 FS $2]++ == 0) {
k3[$1 FS $2] = $3
order[++cnt] = $1 FS $2
} else if($3 > k3[$1 FS $2]) k3[$1 FS $2] = $3
}
END { for(i = 1; i <= cnt; i++) print order[i], k3[order[i]]}' inputWhen the file input contains: Code:
split;test;2 ref.1;rack.1;2 ref.1;rack.1;1 ref.1;rack.2;1 split;test;3 ref.2;rack.3;1 ref.2;rack.3;2 ref.2;rack.3;3 split;test;1 the output from the above script is: Code:
Following assumes all entries with matching field 1 & field 2 are adjacent and prints the last entry found. split;test;2 ref.1;rack.1;1 ref.1;rack.2;1 split;test;3 ref.2;rack.3;3 split;test;1 Following assumes all entries with matching field 1 & field 2 are adjacent and prints the entry with highest value in field 3. split;test;2 ref.1;rack.1;2 ref.1;rack.2;1 split;test;3 ref.2;rack.3;3 split;test;1 Following assumes entries with matching field 1 & field 2 might not be adjacent and prints the last entry found. Output order is not guaranteed to match the order of first appearance in the input file. split;test;1 ref.2;rack.3;3 ref.1;rack.1;1 ref.1;rack.2;1 Following assumes entries with matching field 1 & field 2 might not be adjacent and prints the highest entry found. Output order is not guaranteed to match the order of first appearance in the input file. split;test;3 ref.2;rack.3;3 ref.1;rack.1;2 ref.1;rack.2;1 Following assumes entries with matching field 1 & field 2 might not be adjacent and prints the highest entry found. Output order is guaranteed to match the order of first appearance in the input file. split;test;3 ref.1;rack.1;2 ref.1;rack.2;1 ref.2;rack.3;3 |
| The Following User Says Thank You to Don Cragun For This Useful Post: | ||
beca123456 (10-07-2012) | ||
| Sponsored Links | |
|
|
#6
|
|||
|
|||
|
Waoww !!! This is a very complete answer !
Thanks a lot ! |
| Sponsored Links | ||
|
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Common records | jacobs.smith | Shell Programming and Scripting | 7 | 05-15-2012 02:38 PM |
| Matching and Merging csv data fields based on a common field | landossa | Shell Programming and Scripting | 3 | 04-24-2012 02:35 AM |
| Common records after matching on different columns | jacobs.smith | Shell Programming and Scripting | 10 | 02-17-2012 03:45 PM |
| Merging CSV fields based on a common field | landossa | Shell Programming and Scripting | 1 | 02-09-2012 12:02 AM |
| Common records using AWK | jacobs.smith | Shell Programming and Scripting | 9 | 02-02-2012 02:39 AM |
|
|