skip to Main Content

I want to prefix the line containing the string echo ‘"xxx.yyy" with "# " (ash+space) string in file.txt using sed in linux

output should look like this: # echo ‘"xxx.yyy"

I tried this:

find file.txt -type f -exec sed -i ‘s|"echo ‘"xxx.yyy""|"# echo ‘"xxx.yyy""|g’ {} ;

But i am getting the error: sed: -e expression #1, char 23: unterminated `s’ command

Should i escape any character here?
What am I doing wrong here?

2

Answers


  1. Your single quotes in the search and replacement are causing the error that the s option is unterminated as sed does not see the complete command.

    To fix it, you can try this sed

    sed -i 's|"echo '"'"'"xxx.yyy"|"# echo '"'"'"xxx.yyy""|g' file.txt
    
    Login or Signup to reply.
  2. I will show the sed commands without -i. Add -i when it works like you want.
    When you are not sure how much quotes you have, you can try

    sed 's/echo [^[:alnum:]]*xxx.yyy[^[:alnum:]]/# &/' file.txt
    

    or when you are already happy when a line has both echo and xxx.yyy:

    sed 's/echo.*xxx.yyy/# &/' file.txt
    

    In both commands, the & stands for the part that is matched.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search