Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

4
  • 1
    I think the OP wants to replace for all lines matching KEYWORD but change all ' with " and all " with '. I don't think we can rely in using KEYWORD1 or KEYWORD2 to know which replacement should be made. Commented Jan 13, 2017 at 22:52
  • I realized today there is text in front of my keywords. I tried your solutions without the ^ and the top line worked. When I tried the second line without a carat, I got the error sed: can't read FILENAME.txt: No such file or directory. I know the file exists because I used the first command on it, successfully. Commented Jan 16, 2017 at 18:11
  • This is what I wound up using to change my files: sed -i -e "/^KEYWORD_1/ y/\"/'/" \ -i -e "/^KEYWORD_2/ y/'/\"/" \ file Commented Jan 16, 2017 at 19:37
  • @EmilyShaffer Glad it worked. The altenate command is correct. Your error "can't read FILENAME.txt" was probably due to a quoting problem. If you mistakenly remove the leading or ending double quote, or one of the inner escapes (``) you may end up with a similar error. Commented Jan 16, 2017 at 19:49