Numbering lines matching the pattern using sed

前端 未结 6 1985
一生所求
一生所求 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条回答
  •  旧时难觅i
    2021-02-01 07:38

    You can use grep:

    grep -n pattern file
    

    If you use = in sed the line number will be printed on a separate line and is not available in the pattern space for manipulation. However, you can pipe the output into another instance of sed to merge the line number and the line it applies to.

    sed -n '/pattern/{=;p}' file | sed '{N;s/\n/ /}'
    

提交回复
热议问题