0

What would be the proper way to change the "2" limit to use a variable? I have a file of IP addresses that I want to report only the addresses with a count over the max variable.

awk '{a[$0]++}END{for(i in a){if(a[i] > **2**){print i}}}' a.txt 

Attempt 1:

max=5
awk '{a[$0]++}END{for(i in a){if(a[i] > max){print i}}}' a.txt

Attempt 2:

$max, max {max}

Thanks

Question is based on this link:

Stackoverflow - https://stackoverflow.com/questions/20147878/how-to-sort-uniq-and-display-line-that-appear-more-than-x-times?newreg=67b23722d94743d48e149713f96b10fa

Community
  • 1
  • 1
CA171
  • 51
  • 1
  • 10

3 Answers3

2
maxInShell=5
awk -v maxInAwk="$maxInShell" '{a[$0]++}END{for(i in a){if(a[i] > maxInAwk){print i}}}' a.txt

used lengthy var-name to save explanations.

Kent
  • 189,393
  • 32
  • 233
  • 301
1
awk -v max=5 '{a[$0]++}END{for(i in a){if(a[i] > max){print i}}}' a.txt
jaypal singh
  • 74,723
  • 23
  • 102
  • 147
Etan Reisner
  • 77,877
  • 8
  • 106
  • 148
1

If your awk script is part of another shell script who creates the variable max, then you can use that variable in awk script by using -v option of awk.

For example:

awk -v max="$max" '{a[$0]++}END{for(i in a){if(a[i] > max){print i}}}' a.txt
jaypal singh
  • 74,723
  • 23
  • 102
  • 147