[英]Make awk print only if matches are greater than 3 and not print if less than 3
如何僅在匹配 3 個或更多時才進行 awk 打印,如果小於 3 則不打印。
我不擅長 awk 編程。 但這是我嘗試過的。
awk '/DEF/ {count++;print} { if ( count > 2 ) print count } ' file
DEF
DEF是上述命令的結果。 不應該打印,因為文件中只有一個匹配項,而不是 3 個或更多。
模式 DEF 曾經在文件中,但即使我在 if 語句中寫了僅當它匹配 3 個或更多時才打印,awk 仍然打印它。 任何想法我做錯了什么?
這將打印在文件中找到的帶有DEF
的行數超過 2 次(3 次或更多):
awk '/DEF/ {++c} END {if (c>2) print c}' file
如果一行有兩個DEF
,算作一行還是兩個DEF
呢?
當找到兩個以上的DEF
行時打印找到的行。
awk '/DEF/ {++c;a[NR]=$0} END {if (c>2) {for (i in a) print a[i]}}' file
這將打印一行,其中包含找到的DEF
行數以及該行。
awk '/DEF/ {++c;a[NR]=$0} END {if (c>2) {print "DEF found "c" times";for (i in a) print a[i]}}' file
這將打印命中數和找到的行號:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.