シェルコマンドを使用してパターンに一致する行の行番号を印刷するにはどうすればよいですか? [コピー]

シェルコマンドを使用してパターンに一致する行の行番号を印刷するにはどうすればよいですか? [コピー]

文書、

.false alarm is bla no. 11
.no alarm generated is bla
.application will be killed is bla wall command
.doc file is document file with .doc extension
.no authority for this selection bla

blaここで、単語(または数字も含む)のみを含む出力ファイルの行を印刷したいと思います。

出力は次のとおりです。

1 .false alarm is bla no. 11
2 .no alarm generated is bla
3 .application will be killed is bla wall command
5 .no authority for this selection bla

ベストアンサー1

多くのツールが便利です。

  • -nofがgrepあなたが探しているものです。

    grep -n 'bla' file
    
  • またはawk:

    awk '/bla/{print NR":"$0}' file
    
  • またはperl:

    perl -ne 'print $.,":",$_ if /bla/' file
    
  • またはsed:

    sed '/bla/!d;=' file |sed 'N;s/\n/:/'
    

おすすめ記事