Skip to main content
2 of 6
added 98 characters in body
xhienne
  • 18.2k
  • 2
  • 58
  • 71

Changing a set of characters to another set of characters is generally a task for the tr command but since you want to do it only on certain lines, it will be best done by sed indeed:

sed -e '/^KEYWORD_1/ s/"/\x27/g' \
    -e '/^KEYWORD_2/ s/\x27/"/g' file

Each sed command here starts with a line selector /KEYWORD/ which instructs sed to only operate on the line matching the pattern between /. Here the patterns start with the character ^ to indicate it is to be found at the beginning of the line.

Following the line selector is sed substitution command s/xxx/yyy/g which replaces every occurrence of pattern xxx with string yyy. Here, \x27 represents the single quote (I used this alternate notation because I couldn't use a ' in a string already enclosed in ').

xhienne
  • 18.2k
  • 2
  • 58
  • 71