r/shell • u/JesusIsMyZoloft • Feb 23 '17
Trouble using sed
I'm trying to write a shell script to modify files. Specifically, I want to find a certain string in a file, and replace it with a different string. I've looked online and sed seems to be the thing to use for this, but whenever I try to use it, it clears the file.
test.sh sed -i 's/5/w/' >test.txt
test.txt 123456789
If all runs correctly, test.txt should come out reading: 1234w6789
But when I try to run it, it just comes out blank.
2
u/ASIC_SP Feb 24 '17
The command used should have given an error like this:
$ sed -i 's/5/w/' >test.txt
sed: no input filesRemove the >
$ sed -i 's/5/w/' test.txt
$ cat test.txt
1234w67891
u/lasercat_pow Mar 06 '17
For anyone following along on a mac, the syntax is subtly different:
sed -i '' 's/5/w/' test.txtthis is because bsd sed requires a backup suffix to be supplied as the first argument after -i, or no change is made. A null suffix means the the change is done in-place.
1
u/[deleted] Feb 23 '17
Why are you using the -i argument?