Lexicon Lexicon - 5 months ago 10
Linux Question

grep for contents AFTER pattern

Thanks in advance for the help.

Given a file, example:

potato: 1234
apple: 5678
potato: 5432
grape: 4567
banana: 5432
sushi: 56789


I'd like to grep for all lines that start with "potato:" but only pipe the numbers that follow "Potato". So in the above example, the output would be:

1234
5432


Thanks

rid rid
Answer
grep 'potato:' file.txt | sed 's/^.*: //'

or

grep 'potato:' file.txt | cut -d\   -f2

or

grep 'potato:' file.txt | awk '{print $2}'

or

grep 'potato:' file.txt | perl -e 'for(<>){s/^.*: //;print}'

or

awk '{if(/potato:/) print $2}' < file.txt

or

perl -e 'for(<>){/potato:/ && s/^.*: // && print}' < file.txt