0

I'm trying to use a line of code that will find and replace text with a long string of prefix and suffix code.

Original file:

1. 'text that will go here'
2. 'more text that will be here'
3. 'even more text'

After replace:

youtube-dl -f 'bestvideo[height<=360]+bestaudio/best[height<=360]' --postprocessor-args "-ss 0:0:55 -to 0:1:05" "ytsearch1:text that will go here" -o "text that will go here";
youtube-dl -f 'bestvideo[height<=360]+bestaudio/best[height<=360]' --postprocessor-args "-ss 0:0:55 -to 0:1:05" "ytsearch1:more text that will be here" -o "more text that will be here";
youtube-dl -f 'bestvideo[height<=360]+bestaudio/best[height<=360]' --postprocessor-args "-ss 0:0:55 -to 0:1:05" "ytsearch1:even more text" -o "even more text";

I have the following regex which matches #. ' and replaces it but when I try to replace it with the code, sed won't accept it and shoots out an error. This is what I was trying to use:

sed -e "s/^.*\d. '/youtube-dl -f 'bestvideo[height<=360]+bestaudio/best[height<=360]' --postprocessor-args "-ss 0:0:55 -to 0:1:05" "ytsearch1:/g" original.txt > new.txt

I know why it doesn't work, because the replace with has a bunch of slashes and quotes in it, but I have no idea how to get sed to accept those characters to replace with so it doesn't break the entire command.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

2

With extended regular expression:

sed -E "s|^.*'(.*)'.*|youtube-dl -f 'bestvideo[height<=360]+bestaudio/best[height<=360]' --postprocessor-args \"-ss 0:0:55 -to 0:1:05\" \"ytsearch1:\1\" -o \"\1\";|" file

or with regex:

sed "s|^.*'\(.*\)'.*|youtube-dl -f 'bestvideo[height<=360]+bestaudio/best[height<=360]' --postprocessor-args \"-ss 0:0:55 -to 0:1:05\" \"ytsearch1:\1\" -o \"\1\";|" file

See: Escaping forward slashes in sed command

Cyrus
  • 84,225
  • 14
  • 89
  • 153
  • It almost works, only problem is it isn't matching my regex exactly. It's doing ^.*instead of ^.*\d. ' and missing the \d part and looks like what also follows it. – user2406786 Apr 26 '20 at 02:06
  • @user2406786 but this gives the output as mentioned in the question.. sometimes there are different ways to solve the same problem.. also note that `sed` doesn't support `\d`, you need to use `[0-9]` – Sundeep Apr 26 '20 at 03:35