I have a .csv file with two "columns" of data like this:
test1.ts.meta,Before Sunrise (1995)
test2.ts.meta,A Beautiful Mind (2001)
test3.ts.meta,Departures (2008)
test4.ts.meta,Love & Other Drugs (2010)
I am trying to use this command to replace line 2 in each .ts.meta file with the corresponding movie name...
cat 1TBMovie2_dotTSdotMeta.txt | while IFS=, read file moviename; do sed "2 s/^.*$/$moviename/" "$file"; done
It works fine except for movie names that have the ampersand in them (&).
For example the movie name: Love & Other Drugs (2010) in this case line two of the .ts.meta file gets this movie name:
Love Love Love & Other Drugs Other Drugs (2010) Other Drugs (2010)
Likewise the movie name: Love & Mercy (2015) appears in the .ts.meta file like: Love Love Love & Mercy Mercy (2015) Mercy (2015).
Confusingly... If I open the .ts.meta file for a movie called Love & Mercy (2015) and manually delete line 2 and save then run the command above again I get this in line 2... Love Mercy (2015) with two spaces between the words "Love" and "Mercy".
I think I need to enclose the $moviename variable in double quotes like I did with the $file variable? I guess the ampersand character is being read by sed as having special meaning?
Here is some more information to clarify the problem
My csv file (I actually call it: updatemeta.txt
test1.ts.meta,Carols from King's (2013)
test2.ts.meta,Before Sunrise (1995)
test3.ts.meta,Love & Other Drugs (2010)
test4.ts.meta,Departures (2008)
test1.ts.meta
1:0:19:1B1C:802:2:11A0000:0:0:0:
Carols from King's
The traditional Christmas carol concert from King's College Chapel, Cambridge. Stephen Cleobury conducts the famous chapel choir in carols old and new. [HD] [S]
1387969020
448066800
2913369072
f:0,c:00157c,c:01157e,c:02157f,c:03157c,c:050001
188
0
test2.ts.meta
1:0:1:189E:7FD:2:11A0000:0:0:0:
Before Sunrise
Romance starring Julie Delpy and Ethan Hawke. Two twentysomethings meet on a train and decide to spend a few hours together. Contains some strong language. Also in HD. [1995] [AD,S]
1392418980
550744512
2637755808
f:0,c:0013ec,c:0113ed,c:0213ef,c:0313ec
188
0
test3.ts.meta
1:0:1:2404:7F9:2:11A0000:0:0:0:
Love & Other Drugs
(2010) Fact-based adult comedy. Jake Gyllenhaal stars as a successful Viagra salesman who falls for a woman with Parkinson's (Anne Hathaway). Strong language/sexual scenes. [AD,S]
1472775840
712401799
2824257448
f:0,c:000931,c:010932,c:020934,c:030931
188
0
test4.ts.meta
1:0:1:2404:7F9:2:11A0000:0:0:0:
Departures
(2008) An Oscar-winning, whimsical look at the Japanese undertaking profession. Masahiro Motoki stars as a musician starting a new career preparing the dead for burial. Japanese/subs.
1400111580
863881200
3699150040
f:0,c:000931,c:010932,c:020934,c:030931
188
0
I will have the .csv file in the same directory as many .ts.meta files. There will be a row in the .csv file for each .ts.meta file in the directory and a corresponding movie name.
How do I use sed or awk or gawk to create a script that loops through each row in the .csv file and replaces line two in the named .ts.meta file with the corresponding movie name specified in the .csv file?
I tried the examples given in the solutions below but don't understand what is going on!
Thank you,
Flex
&
is a special character on the replacement side of a sed substitution – steeldriver Jul 08 '20 at 00:31