如何對模式后的內(nèi)容進行g(shù)rep?例如,給定一個文件:potato: 1234
apple: 5678
potato: 5432
grape: 4567
banana: 5432
sushi: 56789我想為所有以potato:但只需列出下面的數(shù)字potato:..所以在上面的例子中,輸出是:1234
5432我怎么能這么做?
3 回答

HUWWW
TA貢獻1874條經(jīng)驗 獲得超12個贊
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貢獻1786條經(jīng)驗 獲得超13個贊
sed -n 's/^potato:[[:space:]]*//p' file.txt

梵蒂岡之花
TA貢獻1900條經(jīng)驗 獲得超5個贊
perl -lne 'print $1 if /^potato:\s*(.*)/' file.txt
perl -lne 'if ($found){print} elsif (/^potato:\s*(.*)/){print $1; $found++}' file.txt
-n
在輸入文件的每一行周圍循環(huán)。 -l
在處理之前移除換行符,然后將它們添加回 -e
執(zhí)行perl代碼
添加回答
舉報
0/150
提交
取消