我有一个包含类似
的行的文件A,B,1,2,3,$long,6,"A","",$long,,,,"ABC",,$long,,,,
E,F,2,3,4,$long,$long,$long,$long,,,"A","STRING";123456,,,1,2
我的目标是找到包含n次出现模式的行" $ long"。
有人知道这场比赛的grep正则表达式吗?
答案 0 :(得分:4)
你不需要正则表达式。使用awk
,您可以使用$long
作为字段分隔符,并检查每行有多少字段:
awk -v count=3 'BEGIN {FS="\\$long"} NF==(count+1)' file
$ awk -v count=3 'BEGIN {FS="\\$long"} NF==(count+1)' a
A,B,1,2,3,$long,6,"A","",$long,,,,"ABC",,$long,,,,
$ awk -v count=4 'BEGIN {FS="\\$long"} NF==(count+1)' a
E,F,2,3,4,$long,$long,$long,$long,,,"A","STRING";123456,,,1,2
$ awk -v count=5 'BEGIN {FS="\\$long"} NF==(count+1)' a
$
答案 1 :(得分:2)
awk
solution by Fedorqui应该可以正常工作。您也可以使用grep
:
grep -E '(.*\$long){4}' file
E,F,2,3,4,$long,$long,$long,$long,,,"A","STRING";123456,,,1,2
答案 2 :(得分:2)
$ awk -v n=3 'gsub(/\$long/,"&")==n' file
A,B,1,2,3,$long,6,"A","",$long,,,,"ABC",,$long,,,,
$ awk -v n=4 'gsub(/\$long/,"&")==n' file
E,F,2,3,4,$long,$long,$long,$long,,,"A","STRING";123456,,,1,2
但如果$long
可以出现在除了作为其自己的字段之外的上下文中,例如:
A,B,1,2,3,$long,6,"A","",$long,,,,"ABC$longDEF",,$long,,,,
并且你只想在它自己的领域中计算它然后你需要更多的东西:
$ awk -F, -v n=3 '{c=0; for (i=1;i<=NF;i++) if ($i=="$long") c++} c==n' file
e.g:
$ cat file
A,B,1,2,3,$long,6,"A","",$long,,,,"ABC",,$long,,,,
E,F,2,3,4,$long,$long,$long,$long,,,"A","STRING";123456,,,1,2
A,B,1,2,3,$long,6,"A","",$long,,,,"ABC$longDEF",,$long,,,,
错:
$ awk -v n=3 'gsub(/\$long/,"&")==n' file
A,B,1,2,3,$long,6,"A","",$long,,,,"ABC",,$long,,,,
$ awk -v n=4 'gsub(/\$long/,"&")==n' file
E,F,2,3,4,$long,$long,$long,$long,,,"A","STRING";123456,,,1,2
A,B,1,2,3,$long,6,"A","",$long,,,,"ABC$longDEF",,$long,,,,
右:
$ awk -F, -v n=3 '{c=0; for (i=1;i<=NF;i++) if ($i=="$long") c++} c==n' file
A,B,1,2,3,$long,6,"A","",$long,,,,"ABC",,$long,,,,
A,B,1,2,3,$long,6,"A","",$long,,,,"ABC$longDEF",,$long,,,,
$ awk -F, -v n=4 '{c=0; for (i=1;i<=NF;i++) if ($i=="$long") c++} c==n' file
E,F,2,3,4,$long,$long,$long,$long,,,"A","STRING";123456,,,1,2