I want to loop through a file and remove certain lines. Example file:
test.txt
a
b
c
d
What I have:
FILE=/tmp/test.txt
while read FILE
do
# if the line starts with b delete it otherwise leave it there
?
done
This is a job for sed
- the UNIX stream editor:
(sed '/^b/d' yourfile > yourfile~ && mv yourfile~ yourfile) || rm yourfile~
The command will delete all lines which begin with a b
and writes the remaing lines to a backup file. If this succeed the backup file will be renamed to the original file if it fails the backup file will be deleted.
you could do it with a grep oneliner :
grep -v "^b" test.txt > newfile
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With