Sed: replace a string in the text only if it is enclosed in quotation marks

I need to use sed to replace a sequence of characters in the text only if that sequence of characters belongs to the quoted string.

eg. following text:

This is a YouTube video referenced by the 'movies.YouTube_id' column.

should be converted as follows:

This is a YouTube video referenced by the 'movies.you_tube_id' column.

i.e. replacing the substring "YouTube" with "you_tube" only if such a substring is part of a string enclosed in single quotes ('), regardless of leading and / or trailing characters enclosed in quotation marks.

Obviously

sed -r "s/YouTube/you_tube/g"

, "YouTube" "you_tube" , . ?

.

+4
4

GNU sed:

sed -E "s/('[^']*)YouTube([^']*')/\1you_tube\2/g" file

:

This is a YouTube video referenced by the 'movies.you_tube_id' column.
+2

awk :

awk '{sub(/\047movies.YouTube_id\047/,"\047movies.you_tube_id\047")} 1'   Input_file

:

This is a YouTube video referenced by the 'movies.you_tube_id' column.
+1

awk :

awk 'BEGIN{FS=OFS="\047"} {
for (i=2; i<=NF; i+=2) gsub(/YouTube/, "you_tube", $i)} 1' file

This is a YouTube video referenced by the 'movies.you_tube_id' column.

Sinec , , .

+1

( ), :

perl -pe "s/(?<=')(:?\w+\.)?YouTube(?=_id')/you_tube/"
0

Source: https://habr.com/ru/post/1693593/


All Articles