Command to insert lines before first match

前端 未结 3 1286
伪装坚强ぢ
伪装坚强ぢ 2021-01-05 05:46

I have file with the below info

testing
testing
testing

I want to insert a word(tested) before the first testing word using sed or any linu

3条回答
  •  失恋的感觉
    2021-01-05 06:04

    Exactly:

    sed '0,/testing/s/testing/tested\n&/' file
    

    For lines containing "testing":

    sed '0,/.*testing.*/s/.*testing.*/tested\n&/' file
    

    For Lines Starting with "testing"

    sed '0,/^testing.*/s/^testing.*/tested\n&/' file
    

    For lines ending with "testing":

    sed '0,/.*testing$/s/.*testing$/tested\n&/' file
    

    To update the content of the file with the result add "-i", example:

    sed -i '0,/testing/s/testing/tested\n&/' file
    

提交回复
热议问题