How to delete a line containing a pattern using sed.
Contents of test.txt.
$ cat test.txt
one
two
three |
Delete a line matching the string “one.”
sed -i '/one/d' test.txt |
Display test.txt
$ cat test.txt
two
three |
cloud engineer
How to delete a line containing a pattern using sed.
Contents of test.txt.
$ cat test.txt
one
two
three |
$ cat test.txt one two three
Delete a line matching the string “one.”
sed -i '/one/d' test.txt |
sed -i '/one/d' test.txt
Display test.txt
$ cat test.txt
two
three |
$ cat test.txt two three
Here’s how to delete a line from a file using awk.
awk '!/Redhat/' sample.txt > sample.tmp && mv sample.tmp sample.txt |
awk '!/Redhat/' sample.txt > sample.tmp && mv sample.tmp sample.txt
The command above looks for ‘Redhat’ in a file and removes that line from the file.
You can add the line in the crontab to run at reboot if you like.
crontab -e |
crontab -e
@reboot awk '!/Redhat/' sample.txt > sample.tmp && mv sample.tmp sample.txt |
@reboot awk '!/Redhat/' sample.txt > sample.tmp && mv sample.tmp sample.txt
Here’s how to modify a line in a file using sed.
sed -i 's/replace me.*/with this text/' /path/to/a/file.txt |
sed -i 's/replace me.*/with this text/' /path/to/a/file.txt