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

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

    The use case can perhaps be that your occurences are spread throughout your file, but you know your only concern is in the first 10, 20 or 100 lines.

    Then simply adressing those lines fixes the issue - even if the wording of the OP regards first only.

    sed '1,10s/#include/#include "newfile.h"\n#include/'
    

提交回复
热议问题