Skip to main content
added 1 character in body
Source Link
terdon
  • 252.2k
  • 69
  • 480
  • 718

The complication here is that if you replace all ' with " and then all " with ', you will be left with only '. So, you will first need to replace ' with something else—for example, the NULL character (\0) which you can safely assume won't be in your input file—then " with ' and then replace that something else with " again. For example, in perl:

$ perl -pe "if(/KEYWORD/){s/'/\0/g; s/\"/'/g; s/\0/\"/g}" file
KEYWORD_1 table name column = 'string' AND column = 'string'
Additional text
      .
      .
      .
KEYWORD_2 text "text in quote" etc. 

Explanation

  • -pe : print each input line after applying the script given by -e.
  • if(/KEYWORD/){something} : do something only if this line matches KEYWORD.
  • s/foo/bar/g : replace all occurrences of foo in the line with bar. The g means "global". Without it, only the 1st occurrence on each line would be replaced.

Note that since the script itself is inside double quotes, the double quotes inside the script need to be escaped (\").


As pointed out in the comments, there's a more direct way I should have thought of in the first place:

perl -pe "tr/'\"/\"'/ if /^KEYWORD/" file

The tr operator transliterates lists of characters. The general format is tr/searchlist/replacementlist/. So this will replace all ' with " and all " with ' only on lines matching KEYWORD.

The complication here is that if you replace all ' with " and then all " with ', you will be left with only '. So, you will first need to replace ' with something else—for example, the NULL character (\0) which you can safely assume won't be in your input file—then " with ' and then replace that something else with " again. For example, in perl:

$ perl -pe "if(/KEYWORD){s/'/\0/g; s/\"/'/g; s/\0/\"/g}" file
KEYWORD_1 table name column = 'string' AND column = 'string'
Additional text
      .
      .
      .
KEYWORD_2 text "text in quote" etc. 

Explanation

  • -pe : print each input line after applying the script given by -e.
  • if(/KEYWORD/){something} : do something only if this line matches KEYWORD.
  • s/foo/bar/g : replace all occurrences of foo in the line with bar. The g means "global". Without it, only the 1st occurrence on each line would be replaced.

Note that since the script itself is inside double quotes, the double quotes inside the script need to be escaped (\").


As pointed out in the comments, there's a more direct way I should have thought of in the first place:

perl -pe "tr/'\"/\"'/ if /^KEYWORD/" file

The tr operator transliterates lists of characters. The general format is tr/searchlist/replacementlist/. So this will replace all ' with " and all " with ' only on lines matching KEYWORD.

The complication here is that if you replace all ' with " and then all " with ', you will be left with only '. So, you will first need to replace ' with something else—for example, the NULL character (\0) which you can safely assume won't be in your input file—then " with ' and then replace that something else with " again. For example, in perl:

$ perl -pe "if(/KEYWORD/){s/'/\0/g; s/\"/'/g; s/\0/\"/g}" file
KEYWORD_1 table name column = 'string' AND column = 'string'
Additional text
      .
      .
      .
KEYWORD_2 text "text in quote" etc. 

Explanation

  • -pe : print each input line after applying the script given by -e.
  • if(/KEYWORD/){something} : do something only if this line matches KEYWORD.
  • s/foo/bar/g : replace all occurrences of foo in the line with bar. The g means "global". Without it, only the 1st occurrence on each line would be replaced.

Note that since the script itself is inside double quotes, the double quotes inside the script need to be escaped (\").


As pointed out in the comments, there's a more direct way I should have thought of in the first place:

perl -pe "tr/'\"/\"'/ if /^KEYWORD/" file

The tr operator transliterates lists of characters. The general format is tr/searchlist/replacementlist/. So this will replace all ' with " and all " with ' only on lines matching KEYWORD.

added 370 characters in body
Source Link
terdon
  • 252.2k
  • 69
  • 480
  • 718

The complication here is that if you replace all ' with " and then all " with ', you will be left with only '. So, you will first need to replace ' with something else—for example, the NULL character (\0) which you can safely assume won't be in your input file—then " with ' and then replace that something else with " again. For example, in perl:

$ perl -pe "if(/KEYWORD){s/'/\0/g; s/\"/'/g; s/\0/\"/g}" file
KEYWORD_1 table name column = 'string' AND column = 'string'
Additional text
      .
      .
      .
KEYWORD_2 text "text in quote" etc. 

Explanation

  • -pe : print each input line after applying the script given by -e.
  • if(/KEYWORD/){something} : do something only if this line matches KEYWORD.
  • s/foo/bar/g : replace all occurrences of foo in the line with bar. The g means "global". Without it, only the 1st occurrence on each line would be replaced.

Note that since the script itself is inside double quotes, the double quotes inside the script need to be escaped (\").


As pointed out in the comments, there's a more direct way I should have thought of in the first place:

perl -pe "tr/'\"/\"'/ if /^KEYWORD/" file

The tr operator transliterates lists of characters. The general format is tr/searchlist/replacementlist/. So this will replace all ' with " and all " with ' only on lines matching KEYWORD.

The complication here is that if you replace all ' with " and then all " with ', you will be left with only '. So, you will first need to replace ' with something else—for example, the NULL character (\0) which you can safely assume won't be in your input file—then " with ' and then replace that something else with " again. For example, in perl:

$ perl -pe "if(/KEYWORD){s/'/\0/g; s/\"/'/g; s/\0/\"/g}" file
KEYWORD_1 table name column = 'string' AND column = 'string'
Additional text
      .
      .
      .
KEYWORD_2 text "text in quote" etc. 

Explanation

  • -pe : print each input line after applying the script given by -e.
  • if(/KEYWORD/){something} : do something only if this line matches KEYWORD.
  • s/foo/bar/g : replace all occurrences of foo in the line with bar. The g means "global". Without it, only the 1st occurrence on each line would be replaced.

Note that since the script itself is inside double quotes, the double quotes inside the script need to be escaped (\").

The complication here is that if you replace all ' with " and then all " with ', you will be left with only '. So, you will first need to replace ' with something else—for example, the NULL character (\0) which you can safely assume won't be in your input file—then " with ' and then replace that something else with " again. For example, in perl:

$ perl -pe "if(/KEYWORD){s/'/\0/g; s/\"/'/g; s/\0/\"/g}" file
KEYWORD_1 table name column = 'string' AND column = 'string'
Additional text
      .
      .
      .
KEYWORD_2 text "text in quote" etc. 

Explanation

  • -pe : print each input line after applying the script given by -e.
  • if(/KEYWORD/){something} : do something only if this line matches KEYWORD.
  • s/foo/bar/g : replace all occurrences of foo in the line with bar. The g means "global". Without it, only the 1st occurrence on each line would be replaced.

Note that since the script itself is inside double quotes, the double quotes inside the script need to be escaped (\").


As pointed out in the comments, there's a more direct way I should have thought of in the first place:

perl -pe "tr/'\"/\"'/ if /^KEYWORD/" file

The tr operator transliterates lists of characters. The general format is tr/searchlist/replacementlist/. So this will replace all ' with " and all " with ' only on lines matching KEYWORD.

Source Link
terdon
  • 252.2k
  • 69
  • 480
  • 718

The complication here is that if you replace all ' with " and then all " with ', you will be left with only '. So, you will first need to replace ' with something else—for example, the NULL character (\0) which you can safely assume won't be in your input file—then " with ' and then replace that something else with " again. For example, in perl:

$ perl -pe "if(/KEYWORD){s/'/\0/g; s/\"/'/g; s/\0/\"/g}" file
KEYWORD_1 table name column = 'string' AND column = 'string'
Additional text
      .
      .
      .
KEYWORD_2 text "text in quote" etc. 

Explanation

  • -pe : print each input line after applying the script given by -e.
  • if(/KEYWORD/){something} : do something only if this line matches KEYWORD.
  • s/foo/bar/g : replace all occurrences of foo in the line with bar. The g means "global". Without it, only the 1st occurrence on each line would be replaced.

Note that since the script itself is inside double quotes, the double quotes inside the script need to be escaped (\").