Appending a line to a file only if it does not already exist

后端 未结 10 1236
深忆病人
深忆病人 2020-11-30 16:44

I need to add the following line to the end of a config file:

include \"/configs/projectname.conf\"

to a file called lighttpd.conf

10条回答
  •  囚心锁ツ
    2020-11-30 17:23

    Here's a sed version:

    sed -e '\|include "/configs/projectname.conf"|h; ${x;s/incl//;{g;t};a\' -e 'include "/configs/projectname.conf"' -e '}' file
    

    If your string is in a variable:

    string='include "/configs/projectname.conf"'
    sed -e "\|$string|h; \${x;s|$string||;{g;t};a\\" -e "$string" -e "}" file
    

提交回复
热议问题