用grep和wc同时过滤和统计结果
Filter and count the results at the same time with grep and wc
我的项目是过滤列表中的 phone 个数字,里面有很多东西。
所以我想显示我已经过滤的 phone 数字,行数对应于 phone 数字的数量。
我只过滤美国 phone 号码美国 phone 号码。
规则是使用管道 |。
grep "^([0-9][0-9][0-9]) [0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]$" | wc -l > result-phonenumber-filter.txt
data.txt中包含我们需要过滤的数字:
(512) 258-6589
(205) 251-6584
(480) 589-9856
(303) 548-9874
(808) 547-3215
(270) 987-6547
(225) 258-9887
(314) 225-2543
(979) 547-6854
(276) 225-6985
les numeros suivants ne sont pas valables pour ce programme :
+512 325
+512 251 2545654654
+512 6546 6464646
+512546546646564646463313
(314) sgf225-2543
(314) 225-2543fsgaf
(314afd) 225-2543
FSd(314) 225-2543
我要得到的结果是:
(512) 258-6589
(205) 251-6584
(480) 589-9856
(303) 548-9874
(808) 547-3215
(270) 987-6547
(225) 258-9887
(314) 225-2543
(979) 547-6854
(276) 225-6985
The number of line is :10
grep -E '^[ ]{0,9}\([0-9]{3}\) [0-9]{3}-[0-9]{4}[ ]{0,9}$' data.txt | sed 's/^[ \t]*//' > result-phonenumber-filter.txt
count=$(wc -l result-phonenumber-filter.txt)
echo "The number of line is :$count" >> result-phonenumber-filter.txt
$ cat result-phonenumber-filter.txt
(512) 258-6589
(205) 251-6584
(480) 589-9856
(303) 548-9874
(808) 547-3215
(270) 987-6547
(225) 258-9887
(314) 225-2543
(979) 547-6854
(276) 225-6985
The number of line is :10
这是不必要的技巧,但它可以在不创建任何临时文件的情况下工作。进程替换需要 bash
grep -oP '^\s*\(\d{3}\) \d{3}-\d{4}\s*$' file | tee >(echo "there are $(wc -l) matches")
我会选择 awk 或 perl
perl -ne '
if (/^\s*\(\d{3}\) \d{3}-\d{4}\s*$/) {print; $count++}
END {print "there are $count matches\n"}
' file
awk '
/^[[:blank:]]*\([[:digit:]]{3}\) [[:digit:]]{3}-[[:digit:]]{4}[[:blank:]]*$/ {print; count++}
END {print "there are", count, "matches"}
' file
我的项目是过滤列表中的 phone 个数字,里面有很多东西。 所以我想显示我已经过滤的 phone 数字,行数对应于 phone 数字的数量。 我只过滤美国 phone 号码美国 phone 号码。 规则是使用管道 |。
grep "^([0-9][0-9][0-9]) [0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]$" | wc -l > result-phonenumber-filter.txt
data.txt中包含我们需要过滤的数字:
(512) 258-6589
(205) 251-6584
(480) 589-9856
(303) 548-9874
(808) 547-3215
(270) 987-6547
(225) 258-9887
(314) 225-2543
(979) 547-6854
(276) 225-6985
les numeros suivants ne sont pas valables pour ce programme :
+512 325
+512 251 2545654654
+512 6546 6464646
+512546546646564646463313
(314) sgf225-2543
(314) 225-2543fsgaf
(314afd) 225-2543
FSd(314) 225-2543
我要得到的结果是:
(512) 258-6589
(205) 251-6584
(480) 589-9856
(303) 548-9874
(808) 547-3215
(270) 987-6547
(225) 258-9887
(314) 225-2543
(979) 547-6854
(276) 225-6985
The number of line is :10
grep -E '^[ ]{0,9}\([0-9]{3}\) [0-9]{3}-[0-9]{4}[ ]{0,9}$' data.txt | sed 's/^[ \t]*//' > result-phonenumber-filter.txt
count=$(wc -l result-phonenumber-filter.txt)
echo "The number of line is :$count" >> result-phonenumber-filter.txt
$ cat result-phonenumber-filter.txt
(512) 258-6589
(205) 251-6584
(480) 589-9856
(303) 548-9874
(808) 547-3215
(270) 987-6547
(225) 258-9887
(314) 225-2543
(979) 547-6854
(276) 225-6985
The number of line is :10
这是不必要的技巧,但它可以在不创建任何临时文件的情况下工作。进程替换需要 bash
grep -oP '^\s*\(\d{3}\) \d{3}-\d{4}\s*$' file | tee >(echo "there are $(wc -l) matches")
我会选择 awk 或 perl
perl -ne '
if (/^\s*\(\d{3}\) \d{3}-\d{4}\s*$/) {print; $count++}
END {print "there are $count matches\n"}
' file
awk '
/^[[:blank:]]*\([[:digit:]]{3}\) [[:digit:]]{3}-[[:digit:]]{4}[[:blank:]]*$/ {print; count++}
END {print "there are", count, "matches"}
' file