cat
我使用和搜索一个巨大的文件grep
。我需要cat
一直知道哪条线路。
如果我想搜索ball
:
cat file.txt | grep ball
文件:
football
tennis
hockey
basketball
...
我需要的:
1. row - football [1/1000]
2. row - [2/1000]
2. row - [3/1000]
3. row - basketball [4/1000]
答案1
完全的awk
+wc
方法:
样本file.txt
:
football
tennis
hockey
basketball
boxing
awk -v t=$(wc -l file.txt | cut -d' ' -f1) '{ printf "%s[%d/%d]\n",(/ball/? $0" ":""),NR,t }' file.txt
输出:
football [1/4]
[2/4]
[3/4]
basketball [4/4]
[5/5]
答案2
执行grep -n ball file.txt
会输出
1:football
4:basketball