Delete blank lines from file using sed




















The below sed command removes the second line in a file. Delete range of lines The sed command can be used to delete a range of lines.

The sed command removes the lines from m to n in the file. Delete lines other than the first line or header line Use the negation! The following sed command removes all the lines except the header line. Delete first and last line You can specify the list of lines you want to remove in sed command with semicolon as a delimiter.

However, this sed do not remove the lines that contain spaces. Sed Command to Delete Lines - Based on Pattern Match In the following examples, the sed command deletes the lines in file which match the given pattern. Above sed command removes all the lines that start with character 'u'. The above command deletes all the lines that end with character 'x'.

Note : In all the above examples, the sed command prints the contents of the file on the unix or linux terminal by removing the lines.

However the sed command does not remove the lines from the source file. To Remove the lines from the source file itself, use the -i option with sed command. Tags Unix. You might like Show more. Unknown 28 April, Vijay Bhaskar 28 April, We will give you a brief on the methods to remove lines.

We will use the sed command without -i in this article because it is a demonstration article. To show the sed command, we create a file called LinuxhintOS. In order to provide better information, we include these line numbers in the following content. The sed command can remove the lines of any range.

As you can see, we removed the third, sixth, eighth, and last lines in this example. Through the sed command, we remove those lines from the files which are not defined in the command, i. In the second part, we will see that with the help of the sed command, how we remove lines of a similar pattern.

We can remove all lines starting with any character through the sed command. The interpretation of the above command is straightforward, only the file lines that do not exist as whitespaces are printed.

The longer version of the above command will look similar to the following:. Through awk non-blank character class support, the above command can also be represented in the following manner:. The -d an option lets awk dump the final file lines on the system terminal. As you can see, the file no longer has whitespaces. The three discussed and implemented solutions to dealing with blank lines from files through grep , sed , and awk commands will take us a long way into implementing stable and efficient read and write file operations on a Linux system.

Have a question or suggestion? Please leave a comment to start the discussion. Please keep in mind that all comments are moderated and your email address will NOT be published.



0コメント

  • 1000 / 1000