If you have necessary to find and remove a specific line from am file on Linux system use following different command,
For example the below command remove a line which contain a string,
# sed -i '/string-name/d' thelinuxfaq*.txt
The below command remove a line which contain a string, with a same file (thelinuxfaq.txt.backup)
# sed -i ".backup" '/test/d' thelinuxfaq.txt
Follwoing command just clear a line which contain a string,
# sed -i 's/linux//g' filename.txt
This command, find files file names with .txt extension, remove a line which contain a string "linux"
find -name '*.txt' -exec sed -i=.bak '/linux/d' "{}" ';'
-i (or) --in-place edit files in place
GREP command also help us to remove a line, the below command are grep with -v option invert the sense of matching to select non-matching lines.
# cat new_file.txt
Linux
CentOS
RedHat
Fedora
Ubuntu
# grep -v "Ubuntu" new_file.txt > copied_file.txt
# cat copied_file.txt
# cat copied_file.txt
Linux
CentOS
RedHat
Fedora
Remove a file name which contain a string.
Find the files which contain string name "fedora" and remove that files.
# grep -ir 'fedora' *.txt
copied_file.txt:Fedora
new_file.txt:Fedora
testbak.txt:Fedora
# grep -ir 'fedora' *.txt | exec rm -rf *
Comments (0)