Numbering lines matching the pattern using sed

前端 未结 6 1974
一生所求
一生所求 2021-02-01 06:59

I\'m writing the script that searches for lines that match some pattern. I must use sed for this script. This works fine for searching and printing matched lines:



        
6条回答
  •  野的像风
    2021-02-01 07:37

    Using Perl:

    perl -ne 'print "$.: $_" if /regex/' input.file
    

    $. contain line number. If input file contains:

    record foo
    bar baz
    record qux
    

    This one-liner: perl -ne 'print "$.: $_" if /record/' input.file will print:

    1: record foo
    3: record qux
    

    Or if you just want total number of lines matched a pattern use:

    perl -lne '$count++ if /regex/; END { print int $count }' input.file

提交回复
热议问题