How to use sed to replace only the first occurrence in a file?

前端 未结 23 896
别跟我提以往
别跟我提以往 2020-11-22 04:27

I would like to update a large number of C++ source files with an extra include directive before any existing #includes. For this sort of task, I normally use a small bash s

23条回答
  •  攒了一身酷
    2020-11-22 04:54

    This might work for you (GNU sed):

    sed -si '/#include/{s//& "newfile.h\n&/;:a;$!{n;ba}}' file1 file2 file....
    

    or if memory is not a problem:

    sed -si ':a;$!{N;ba};s/#include/& "newfile.h\n&/' file1 file2 file...
    

提交回复
热议问题