I want to get rid of new lines in Echo then suffix a file with them.

files=$(cat ./Temp/files.txt)
prefixedfiles=$(cat ./Temp/prefixedfiles.txt)
echo "$files" >> generatedscript
sed -e 's/^/ffmpeg -i /' generatedscript >> generatedscript2
sed 's/$/ -vcodec libx264 -acodec copy -threads 10 -crf 18 /' >> 
echo "$prefixedfiles" > generatedscript3

E.g files.txt

./More Testing/Test.mkv

E.g prefixedfiles.txt

../Encoded/More Testing/Test.mkv

Expected output: generatedscript3

ffmpeg -i ./Testing/blank.mp4 -vcodec libx264 -acodec copy -threads 10 -crf 18 ../Encoded/More Testing/blank.mp4
ffmpeg -i ./More Testing/Test.mkv -vcodec libx264 -acodec copy -threads 10 -crf 18 ../Encoded/More Testing/Test.mkv
  • There appear to logic errors in your script (removing new lines can be done with echo -n but I dont think this will help you). Why do you have both files.txt and prefixedfiles.txt, rather then a single file with separation if required? Will the path on prefixedfiles.txt change on a per file bases? What do you think the second line is trying to do? (I ask these questions because I suspect the way you are trying to build the script is impractical - you probably need to iterate over $files on a line-by-line basis. No point in helping write it until I understand the constraints. – davidgo May 2 '17 at 23:38
  • 1. The reason the 2 are seperate is because to be able to add "-vcodec libx264 -acodec copy -threads 10 -crf 18" in the middle of the 2 I would need to take down the amount of characters needed for each line and put it in the middle with sed. – Ninsto May 2 '17 at 23:57
  • 2. Yes except for "../Encoded" – Ninsto May 2 '17 at 23:58
  • 3. I'm assuming you mean "sed -e 's/^/ffmpeg -i /' generatedscript >> generatedscript2" it adds ffmpeg -i to the front of every line to tell ffmpeg that is the input. If you were talking about "prefixedfiles=$(cat ./Temp/prefixedfiles.txt)" it is supposed to be a variable for bash to read out because cat will just output to a new line. – Ninsto May 3 '17 at 0:00
  • bash = echo in 3. – Ninsto May 3 '17 at 2:02

As per my comments, I think the logic of the script you provided is flawed - specifically I don't see how sed is able to combine the 2 input files on a per row basis (I don't think it works the way you think it does)

I've come up with an alternative solution for you -

#! /bin/bash

export IFS=$'\n'

for LINE in `paste Temp/files.txt Temp/prefixedfiles.txt`
    F1=`echo $LINE | cut -f1`
    F2=`echo $LINE | cut -f2`    
    echo "ffmpeg -i $F1 -vcodec libx264 -acodec copy -threads 10 -crf 18 $F2"

) > generatedscript3


The export IFS line changes the file separation character to newline - this means that spaces in the files will be correctly handled.

The magic occurs in the for LINE loop - the "paste" command merges the 2 files into a single tab separated output, which is then processed line-by-line.

F1 and F2 separate the 2 parts of the input into variables, which are then printed using the echo line.

This whole output is captured into a file called generatedscript3 (because the loop and echo is between brackets)


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.