How can I make awk print only if it matches 3 or more and NOT print if it's less than 3.
I'm not strong in awk programming. But here is what I tried.
awk '/DEF/ {count++;print} { if ( count > 2 ) print count } ' file
DEF
DEF is the result from the command above. It is not suppose to print because there is only one match in the file, not 3 or more.
The pattern DEF is in the file once, but awk still prints it even though I wrote in the if statement to print only if it matches 3 or more. Any ideas what I am doing wrong?
This will print number of line with DEF
found in the file more than 2 times (3 or more):
awk '/DEF/ {++c} END {if (c>2) print c}' file
What if there are two DEF
on one line, count as one line or two DEF
?
Print the lines found when there are more than two DEF
lines found.
awk '/DEF/ {++c;a[NR]=$0} END {if (c>2) {for (i in a) print a[i]}}' file
This will print a line with number of DEF
lines found as well as the line.
awk '/DEF/ {++c;a[NR]=$0} END {if (c>2) {print "DEF found "c" times";for (i in a) print a[i]}}' file
This will print number of hits and line number its found in as well:
awk '/DEF/ {++c;a[NR]=$0} END {if (c>2) {print "DEF found "c" times\n-----";for (i in a) {print ++t". line "i": "a[i]}}}' file
DEF found 3 times
-----
1. line 4: DEF first time
2. line 8: red DEF second time
3. line 16: DEF more
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.