Reputation: 167
I need to use sed to replace a sequence of characters in a text only if that particular sequence of characters belongs to a string contained within quotes.
e.g. the following text:
This is a YouTube video referenced by the 'movies.YouTube_id' column.
should be transformed like this:
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 substring is part of a string enclosed within single quotes ('), no matter of the leading and/or trailing characters enclosed within quotes.
Obviously
sed -r "s/YouTube/you_tube/g"
doesn't work because it replaces "YouTube" with "you_tube" everywhere regardless of the quotes. Which regular expression can I use to accomplish the task?
Thank you in advance.
Upvotes: 6
Views: 898
Reputation: 88583
With GNU sed:
sed -E "s/('[^']*)YouTube([^']*')/\1you_tube\2/g" file
Output:
This is a YouTube video referenced by the 'movies.you_tube_id' column.
Upvotes: 4
Reputation: 784998
Here is an awk
solution for this:
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 we are using single quote as field delimiter, each even numbered field will give us quoted string surrounded by single quote.
Upvotes: 2
Reputation: 133458
Following awk
may help you on same:
awk '{sub(/\047movies.YouTube_id\047/,"\047movies.you_tube_id\047")} 1' Input_file
Output will be as follows:
This is a YouTube video referenced by the 'movies.you_tube_id' column.
Upvotes: 0
Reputation: 185025
If the string is not fixed (not already the same), then using look around advanced regexes and perl :
perl -pe "s/(?<=')(:?\w+\.)?YouTube(?=_id')/you_tube/"
Upvotes: 0