Formatting Grep有4个管道来格式化输出,更简单吗?

Formatting Grep有4个管道来格式化输出,更简单吗?,formatting,grep,cut,Formatting,Grep,Cut,我认为必须有一个更简单的方法来做到这一点 我有这样的文件(由ls返回): 我目前正在使用: grep -l "string" ./*_file_*.txt | cut -c 3- | cut -d "." -f1 | cut -d "_" -f1,3 | tr -s "_" " " 要获得正确的输出,请执行以下操作: my 0 the 1 my 2 a 3 虽然这很有效,但我是不是很难做到?这看起来很麻烦 谢谢 您可以先执行grep,然后通过管道将grep-l输出到: awk -F'[./]

我认为必须有一个更简单的方法来做到这一点

我有这样的文件(由ls返回):

我目前正在使用:

grep -l "string" ./*_file_*.txt | cut -c 3- | cut -d "." -f1 | cut -d "_" -f1,3 | tr -s "_" " "
要获得正确的输出,请执行以下操作:

my 0
the 1
my 2
a 3
虽然这很有效,但我是不是很难做到?这看起来很麻烦


谢谢

您可以先执行grep,然后通过管道将
grep-l
输出到:

awk -F'[./]|_file_' '{print $3,$4}'

e、 g


所以awk使用了多个分隔符-句号、斜杠和下划线文件下划线?谢谢你的帮助Kent!
awk -F'[./]|_file_' '{print $3,$4}'
sed 's#\.[^.]*$##;s#./##;s#_file_# #'
kent$  echo "./my_file_0.txt
./the_file_1.txt
./my_file_2.txt
./a_file_3.txt"|awk -F'[./]|_file_' '{print $3,$4}'
my 0
the 1
my 2
a 3

kent$  echo "./my_file_0.txt
./the_file_1.txt
./my_file_2.txt
./a_file_3.txt"|sed 's#\.[^.]*$##;s#./##;s#_file_# #'         
my 0
the 1
my 2
a 3