1

I'm trying to manipulate a text file with sed looking for a pattern of .mkv and I would like it to append this to the beginning of the matching line

pipe:///usr/bin/ffmpeg -loglevel 0 -re -i

but I am having issues working though the delimiter issue. I tried changing the / delimited to another such as ! or | but I get unknown command errors.

this code without // being appended works

sed '/.mkv/s/^/test /' test
kiyah
  • 1,502
  • 2
  • 18
  • 27
Jeff Leiss
  • 13
  • 3

1 Answers1

1

First of all, you need to escape a dot in the regex pattern to match a literal dot. Then, use braces to enclose the s command triggered after the .mkv pattern is found and use the delimiters of your choice:

sed '/\.mkv/{s,^,pipe:///usr/bin/ffmpeg -loglevel 0 -re -i,}' file > newfile

Here, I used commas with the s command.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • sorry need additional help, I would like to also append text but if I run 2 seperate commands then it skews the data applied by the first SED, at the end of the same line I would like to append -c copy -flags +global_headers -f mpegts pipe:1. so the output would look like such pipe:///usr/bin/ffmpeg -loglevel 0 -re -i http://hosting.ddns.net:826/Jleiss/dKUAE6VBJ/95976.mkv -c copy -flags +global_headers -f mpegts pipe:1 This will be a query I would run against multiple lines. – Jeff Leiss Jun 03 '19 at 00:00
  • could also be line breaks causing an issue also, and if that is the case how could I avoid that. All of this is being done on a linux machine. – Jeff Leiss Jun 03 '19 at 00:22
  • 1
    @JeffLeiss It's highly inappropriate to add requirements like this. Either edit your question or ask a new one, but if Wiktor's answer helped you, please upvote and/or accept his answer. – vintnes Jun 03 '19 at 00:42
  • ok sorry, first time asking here. I will accept that answer and ask another. – Jeff Leiss Jun 03 '19 at 00:44
  • @JeffLeiss If you want to append text at booth at the start and end of the line that contains `.mkv` you may use `sed 's,.*\.mkv.*,text_at_start&text_at_end,' file > newfile`. Also, if linebreaks are causing issues, [normalize them to LF](https://stackoverflow.com/questions/2613800/how-to-convert-dos-windows-newline-crlf-to-unix-newline-lf-in-a-bash-script). – Wiktor Stribiżew Jun 03 '19 at 08:18
  • thank you, this worked with a little bit of tweaking.. I added the bracket because I was workign with slashes that would cause issues, and I had to remove.* after mkv because it would then take the second part and append over the first. this is what the working command came out to be: sed '{s,.*\mkv,pipe:///usr/bin/ffmpeg -loglevel 0 -re -i & -c copy -flags +global_headers -f mpegts pipe:1,}' test > file – Jeff Leiss Jun 03 '19 at 14:43