sed replace string in a first line

前端 未结 3 1632
自闭症患者
自闭症患者 2021-01-17 11:42

How can I replace a string but only in the first line of the file using the program \"sed\"?

The commands s/test/blah/1 and 1s/test/blah/ d

3条回答
  •  一个人的身影
    2021-01-17 12:11

    This might work for you (GNU sed):

    sed -i '1!b;s/test/blah/' file
    

    will only substitute the first test for blah on the first line only.

    Or if you just want to change the first line:

    sed -i '1c\replacement' file 
    

提交回复
热议问题