我嘗試了很多變體,但似乎都不起作用,我不知道我不明白什么。
我試過的最后一個是(\<[[:digit:]] \>.*){4,}. 但它甚至找不到像123 123 123 123. 輸入行可以是任何東西(甚至像hello 123 my 1 name 2 is 3)。
抱歉,我沒有具體說明,但我的意思是:“123 a2”行有 1 個數字,“1 2 3 45”行有 4 個數字。
uj5u.com熱心網友回復:
使用您顯示的示例,請嘗試以下awk代碼。我們不需要在這里使用回圈。此awk代碼是用 GNU 撰寫和測驗的awk。
awk -v FPAT='(^|[[:space:]] )[0-9] ([[:space:]] |$)' 'NF>3' Input_file
說明:為上述添加詳細說明。
- 使用 GNU
awk的選項命名FPAT以允許正則運算式制作欄位分隔符。 - 使用正則運算式
(^|[[:space:]] )[0-9] ([[:space:]] |$)匹配起始空格后跟數字或數字后跟空格或行尾。 - 在主程式
awk檢查條件NF>3中,這意味著如果欄位數大于 3,則列印該行。
uj5u.com熱心網友回復:
量化命令中的模式似乎存在一些問題grep。如果第一個數字有多個數字,而其他數字只包含一個數字,則您的正則運算式有效。否則不會,看測驗結果:
#!/bin/bash
s="123 1 2 3 - works, the first number is two digits, the rest are one digits
1 2 3 45 - does not work, the first number is one-digit
14 2 3 4 - works
123 a2 - not output as expected as there is just one number
12 and one 1 more and 23 and 6 and some more 3546
hello 123 my 1 name 2 is 3 - output fine since the first number is three-digit and the rest are one digit"
grep -E '(\<[[:digit:]] \>.*){4,}' <<< "$s"
輸出:
123 1 2 3 - works, the first number is two digits, the rest are one digits
14 2 3 4 - works
hello 123 my 1 name 2 is 3 - output fine since the first number is three-digit and the rest are one digit
如果你在沒有它的情況下重寫它{4,}:
grep -E '\<[0-9] \>.*\<[0-9] \>.*\<[0-9] \>.*\<[0-9] \>' file
請參閱此在線演示。
您也可以awk在任何環境中使用它:
awk '{cnt=0; for (i=1; i<=NF; i) { if ($i ~ /^[0-9] $/) { cnt } } }cnt>3' file
請參閱在線演示。詳情:
cnt=0- 將cnt計數器變數設定為0for (i=1; i<=NF; i) {...}- 遍歷當前記錄中的所有欄位(=line)if ($i ~ /^[0-9] $/) { cnt } }- 如果欄位由數字增量組成cntcnt>3- 如果cnt大于3,列印找到的記錄。
查看在線演示:
#!/bin/bash
s="123 123 123 123
1 2 3 45
1 3 5
hello 123 my 1 name 2 is 3"
awk '{cnt=0; for (i=1; i<=NF; i) { if ($i ~ /^[0-9] $/) { cnt } } }cnt>3' <<< "$s"
輸出:
123 123 123 123
1 2 3 45
hello 123 my 1 name 2 is 3
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/425667.html
