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

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

    i would do this with an awk script:

    BEGIN {i=0}
    (i==0) && /#include/ {print "#include \"newfile.h\""; i=1}
    {print $0}    
    END {}
    

    then run it with awk:

    awk -f awkscript headerfile.h > headerfilenew.h
    

    might be sloppy, I'm new to this.

提交回复
热议问题