I want to delete a line containing a specific string from the file. How can I do this without using awk
? I tried to use sed
but I could not achieve it.
The replaceAll() method accepts a regular expression and a String as parameters and, matches the contents of the current string with the given regular expression, in case of match, replaces the matched elements with the String. Retrieve the contents of the file as a String.
@uchuugaka you can do rm -fv and it will output a message if the file was deleted, and it will output nothing if nothing was deleted.
To delete a line, we'll use the sed “d” command. Note that you have to declare which line to delete. Otherwise, sed will delete all the lines.
This should do it:
sed -e s/deletethis//g -i * sed -e "s/deletethis//g" -i.backup * sed -e "s/deletethis//g" -i .backup *
it will replace all occurrences of "deletethis" with "" (nothing) in all files (*
), editing them in place.
In the second form the pattern can be edited a little safer, and it makes backups of any modified files, by suffixing them with ".backup".
The third form is the way some versions of sed
like it. (e.g. Mac OS X)
man sed
for more information.
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