|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Awk script, print lines with one equivalent field
Hi,
I have files in which some lines have identical fields. 4696 1 741.30345 M1-D6 5 4793 1 741.30345 M1-D6 5 5472 1 740.37771 M1-D6 5 I'd like awk to compare only $3 and printout all and only full lines ($0) with the same $3. awk '$3 = prev { print; prev = $3 }' <file> gives removes duplicates fine but I can't get duplicates only, '$3 != prev ...' fails to give anything. Thanks for any tips. |
|
#2
|
||||
|
||||
|
I think this should do the trick
Code:
sort file.txt -nk 3 | awk '
BEGIN { prev = $3 }
($3 == prev) {print $0}
{prev = $3}
'
If the file's already sorted, I guess you can leave out the first part of that Code:
sort file.txt -nk 3 | |
|
#3
|
|||
|
|||
|
You can get awk to print duplicated lines
Code:
awk '{ arr[$3]++; (arr[$3]>0) {print $0} }' file.txt > dupfile.txt
|
![]() |
| Viewing: Dev Shed Forums > Operating Systems > UNIX Help > Awk script, print lines with one equivalent field |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|