Aires69
Aires69

Reputation: 87

searching multiple patterns in awk

I've a text file of thousands of lines

:ABC:xyz:1234:200:some text:xxx:yyyy:11818:AAA:BBB  
:ABC:xyz:6789:200:some text:xxx:yyyy:203450:AAA:BBB  
:EFG:xyz:11818:200:some text:xxx:yyyy:154678:AAA:BBB  
:HIJ:xyz:203450:200:some text:xxx:yyyy:154678:AAA:BBB  
:KLM:xyz:7777:200:some text:xxx:yyyy:11818:AAA:BBB  
.....   
....   
:DEL:xyz:1234:200:some text:xxx:yyyy:203450:AAA:BBB  

I need to find more than one occurrence of the 9th column i.e the o/p should show

:ABC:xyz:1234:200:some text:xxx:yyyy:11818:AAA:BBB  
:KLM:xyz:7777:200:some text:xxx:yyyy:11818:AAA:BBB  

:ABC:xyz:6789:200:some text:xxx:yyyy:203450:AAA:BBB  
:DEL:xyz:1234:200:some text:xxx:yyyy:203450:AAA:BBB

I tried:

awk -F ":" '$9 > 2 {split($0,a,":"); print $0}' 

this prints all the records.

Upvotes: 0

Views: 212

Answers (3)

JTextor
JTextor

Reputation: 71

This should do it in pure awk:

awk -F":" '{if( s[$9] ){ print } else if( f[$9] ){ print f[$9]; s[$9]=1; print }; f[$9]=$0 }'

Explanation:

  • The "f" array stores values of the 9th column that have occurred at least once.
  • The "s" array stores values of the 9th column that have occurred twice or more.
  • If the 9th column has occurred before, print the first occurrence, and this line.
  • If the 9th column has occurred twice or more before, print this line.

Upvotes: 1

Ed Morton
Ed Morton

Reputation: 203577

awk -F':' 'NR==FNR{cnt[$9]++;next} cnt[$9]>1' file file

or if you don't want to parse the file twice:

awk -F':' 'cnt[$9]++{printf "%s", prev[$9]; delete prev[$9]; print; next} {prev[$9]=$0 ORS}' file

Upvotes: 3

Jotne
Jotne

Reputation: 41456

Here is another awk

awk -F: '{++a[$9];b[NR]=$0} END {for (i=1;i<=NR;i++) {split(b[i],c,":");if (a[c[9]]>1) print b[i]}}' file

Upvotes: 0

Related Questions