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

前端 未结 23 857
别跟我提以往
别跟我提以往 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:50

    A possible solution:

        /#include/!{p;d;}
        i\
        #include "newfile.h"
        :a
        n
        ba
    

    Explanation:

    • read lines until we find the #include, print these lines then start new cycle
    • insert the new include line
    • enter a loop that just reads lines (by default sed will also print these lines), we won't get back to the first part of the script from here

提交回复
热议问题