In this tutorial, we’ll learn how to delete lines from a file that contain a specific string using the Linux commands awk, grep, and sed. Along the way, we’ll discuss their similarities and differences.
2. Input File
We’ll need to use an input file for most of the commands demonstrated in this tutorial, so let’s create a file named myfile.txt:
Baeldung is focused on the Java ecosystem, helping developers learn to implement better, more secure web applications with Spring. We can find many articles and tutorials on the website. Baeldung offers a great range of Java tutorials. The HttpClient Series contain very useful examples. Please let us know if you have any question regarding the tutorial.
In the next few sections, we’ll run various commands to delete all the lines containing the string “Baeldung” from our sample file.
3. Delete Lines With grep
grep is one of the most famous text-processing commands in the Linux operating system. It allows us to search for patterns in input files, and prints the lines that match.
In this case, we actually want to print non-matching lines, so we’ll use the -v option, which performs an inverted match:
$ grep -v "Baeldung" myfile.txt > tmpfile && mv tmpfile myfile.txt
This gives us only those lines that don’t match the pattern specified:
$ cat myfile.txt
We can find many articles and tutorials on the website. The HttpClient Series contain very useful examples. Please let us know if you have any question regarding the tutorial.
As we can see, we’re omitting the lines that contain the string “Baeldung” and saving the non-matching lines into a temporary file. Then we overwrite our original file with the temporary file.
4. Delete Lines With awk
The awk command is built-in on the Linux operating system. Often described as a scripting language, awk is very powerful, providing us with functions to perform various operations on the input data.
In this example, we can see that by using a simple regular expression, awk will give us the result we’re expecting to get. Using /Baeldung/, we’ll match any line with the string “Baeldung.” With the exclamation mark (!), we negate the condition to get the desired result:
$ awk '!/Baeldung/' myfile.txt > tmpfile && mv tmpfile myfile.txt
Again, as we saw with grep, we had to use a temporary file as an intermediate step.
$ gawk -i inplace '!/Baeldung/' myfile.txt
Thanks to gawk and its inplace extension, we don’t have to handle the file redirection manually.
5. Delete Lines With sed
So far, we’ve learned how to delete lines that contain a specific string with the grep and awk commands.
In addition, we can also solve the problem using the sed command.
The sed command has the -i option, which allows editing files in-place. The -i option takes an optional extension argument in case we want to save the original file as a backup with that extension. If no extension is given, the backup will be skipped:
$ sed -i '/Baeldung/d' myfile.txt
As we can see, we’re matching lines using the regex /Baeldung/ and then deleting the matching lines using the d operator.
If we check our file now, we’ll see that the lines containing “Baeldung” have been deleted.
In this article, we explained three different methods for deleting lines that contain a specific string from input files.
Moreover, if we have gawk (version 4.1.0 or later) or sed available, we can use their “in-place” edit feature, so that we don’t have to handle the temp file redirection manually.