I keep text files with definitions in a folder. I like to convert them to spoken word so I can listen to them. I already do this manually by running a few commands to insert some pre-processing codes into the text files and then convert the text to spoken word like so:
sed 's/\..*$/[[slnc 2000]]/' input.txt
inserts a control code after first period
sed 's/$/[[slnc 2000]]/' input.txt"
inserts a control code at end of each line
cat input.txt | say -v Alex -o input.aiff
Instead of having to retype these each time, I would like to create a Bash script that pipes the output of these commands to the final product. I want to call the script with the script name, followed by an input file argument for the text file. I want to preserve the original text file so that if I open it again, none of the control codes are actually inserted, as the only purpose of the control codes is to insert pauses in the audio file.
I've tried writing
#!/bin/bash
FILE=$1
sed 's/$/ [[slnc 2000]]/' FILE -o FILE
But I get hung up immediately as it says sed: -o: No such file or directory. Can anyone help out?
sed
versions do not take kindly to reading from and writing to the same file. It might be better to either (a) precede this withcat "${FILE}" |
, or (b) use a temp file and move it into place. The latter has an advantage that if the replacement fails for any reason, it'll likely fail before the original file is overwritten (and lost). The first method can overwrite the file. A third option is to dosed -i -e "s/..." "${FILE}"
, which does the changes in-place in the file ... though this does not always work, depending on the OS version,sed
, etc. – r2evans-i
.) – r2evans