How to grep for contents after pattern?

前端 未结 7 833
礼貌的吻别
礼貌的吻别 2020-11-22 07:37

Given a file, for example:

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

I\'d like to grep for all lines that

相关标签:
7条回答
  • 2020-11-22 08:20

    This will print everything after each match, on that same line only:

    perl -lne 'print $1 if /^potato:\s*(.*)/' file.txt
    

    This will do the same, except it will also print all subsequent lines:

    perl -lne 'if ($found){print} elsif (/^potato:\s*(.*)/){print $1; $found++}' file.txt
    

    These command-line options are used:

    • -n loop around each line of the input file
    • -l removes newlines before processing, and adds them back in afterwards
    • -e execute the perl code
    0 讨论(0)
提交回复
热议问题