I'm writing bash script where I need to store some files as a .bak
files and change it content at the beginning(using sed
for that).
I'm looking for better way to write it down for my bash script.
cp file.txt file.txt.bak | sed -i '1i#Backup file' file.txt.bak
Maybe someone knew more efficient way to do this or how to do this only by sed or just without pipes.
The pipe isn't doing anything at all there. cp
has no output, and therefore you cannot pipe its output to another program. I'm guessing you wanted ;
or &&
instead:
## copy the file and then run sed
cp file.txt file.txt.bak; sed -i '1i#Backup file' file.txt.bak
Or
## copy the file and then run sed BUT only if the copy was successfull
cp file.txt file.txt.bak && sed -i '1i#Backup file' file.txt.bak
However, if all you want is a copy of the original file with the first line changed, then sed can indeed do it for you:
sed '1i#Backup file' file.txt > file.txt.bak
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments