如何对模式后的内容进行grep?例如,给定一个文件:potato: 1234
apple: 5678
potato: 5432
grape: 4567
banana: 5432
sushi: 56789我想为所有以potato:但只需列出下面的数字potato:..所以在上面的例子中,输出是:1234
5432我怎么能这么做?
3 回答

收到一只叮咚
TA贡献1821条经验 获得超4个赞
grep 'potato:' file.txt | sed 's/^.*: //'
grep 'potato:' file.txt | cut -d\ -f2
grep 'potato:' file.txt | awk '{print $2}'
grep 'potato:' file.txt | perl -e 'for(<>){s/^.*: //;print}'
awk '{if(/potato:/) print $2}' < file.txt
perl -e 'for(<>){/potato:/ && s/^.*: // && print}' < file.txt

一只斗牛犬
TA贡献1784条经验 获得超2个赞
sed -n 's/^potato:[[:space:]]*//p' file.txt

吃鸡游戏
TA贡献1829条经验 获得超7个赞
perl -lne 'print $1 if /^potato:\s*(.*)/' file.txt
perl -lne 'if ($found){print} elsif (/^potato:\s*(.*)/){print $1; $found++}' file.txt
-n
在输入文件的每一行周围循环。 -l
在处理之前移除换行符,然后将它们添加回 -e
执行perl代码
添加回答
举报
0/150
提交
取消