Skip to main content
Use only the `y` command
Source Link
xhienne
  • 18.2k
  • 2
  • 58
  • 71

Changing characters

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 only, it will be best done by sed indeedwhich has a y command similar to tr:

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

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

Following the line selector is sed's substitution command sy/xxxset1/yyyset2/g which replaces every occurrence of patterna character in xxxset1 with stringthe character which has the same position in yyyset2.

Swapping characters

Now, if on the same line you want to replace each " with ' and at the same time each ' with ", you mustcan use the y sedonly one command instead of s:

sed -e "/^KEYWORD_1\|^KEYWORD_2/  y/\"'/'\"/" file

Changing characters

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/\"/'/g" \
    -e "/^KEYWORD_2/ s/'/\"/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's substitution command s/xxx/yyy/g which replaces every occurrence of pattern xxx with string yyy.

Swapping characters

Now, if on the same line you want to replace each " with ' and at the same time each ' with ", you must use the y sed command instead of s:

sed -e "/^KEYWORD_1\|^KEYWORD_2/ y/\"'/'\"/" file

Changing characters

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 on certain lines only, it will be best done by sed which has a y command similar to tr:

sed -e "/^KEYWORD_1/  y/\"/'/" \
    -e "/^KEYWORD_2/  y/'/\"/" \
    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 they are to be found at the beginning of the line.

Following the line selector is sed's substitution command y/set1/set2/g which replaces every occurrence of a character in set1 with the character which has the same position in set2.

Swapping characters

Now, if on the same line you want to replace each " with ' and at the same time each ' with ", you can use only one command:

sed -e "/^KEYWORD_1\|^KEYWORD_2/  y/\"'/'\"/" file
Headers
Source Link
xhienne
  • 18.2k
  • 2
  • 58
  • 71

Changing characters

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/\"/'/g" \
    -e "/^KEYWORD_2/ s/'/\"/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's substitution command s/xxx/yyy/g which replaces every occurrence of pattern xxx with string yyy.

Swapping characters

Now, if on the same line you want to replace each " with ' and at the same time each ' with ", you must use the y sed command instead of s:

sed -e "/^KEYWORD_1\|^KEYWORD_2/ y/\"'/'\"/" file

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/\"/'/g" \
    -e "/^KEYWORD_2/ s/'/\"/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.

Now, if on the same line you want to replace each " with ' and at the same time each ' with ", you must use the y sed command instead of s:

sed -e "/^KEYWORD_1\|^KEYWORD_2/ y/\"'/'\"/" file

Changing characters

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/\"/'/g" \
    -e "/^KEYWORD_2/ s/'/\"/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's substitution command s/xxx/yyy/g which replaces every occurrence of pattern xxx with string yyy.

Swapping characters

Now, if on the same line you want to replace each " with ' and at the same time each ' with ", you must use the y sed command instead of s:

sed -e "/^KEYWORD_1\|^KEYWORD_2/ y/\"'/'\"/" file
Changed quotes escaping
Source Link
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'g" \
    -e '"/^KEYWORD_2/ s/\x27'/"\"/g'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 ').

Now, if on the same line you want to replace each " with ' and at the same time each ' with ", you must use the y sed command instead of s:

sed -e '"/^KEYWORD_1|^KEYWORD_2^KEYWORD_1\|^KEYWORD_2/ y/"\x27|\x27"\"'/''\"/" file

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 ').

Now, if on the same line you want to replace each " with ' and at the same time each ' with ", you must use the y sed command instead of s:

sed -e '/^KEYWORD_1|^KEYWORD_2/ y/"\x27|\x27"/' file

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/\"/'/g" \
    -e "/^KEYWORD_2/ s/'/\"/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.

Now, if on the same line you want to replace each " with ' and at the same time each ' with ", you must use the y sed command instead of s:

sed -e "/^KEYWORD_1\|^KEYWORD_2/ y/\"'/'\"/" file
y sed command
Source Link
xhienne
  • 18.2k
  • 2
  • 58
  • 71
Loading
added 98 characters in body
Source Link
xhienne
  • 18.2k
  • 2
  • 58
  • 71
Loading
Source Link
xhienne
  • 18.2k
  • 2
  • 58
  • 71
Loading