Awk

使用 grep 搜尋文件,顯示目前的“cat”行

  • November 12, 2017

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]

完成**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]

grep -n ball file.txt會輸出

1:football
4:basketball

引用自:https://unix.stackexchange.com/questions/403950