Linux delete all lines in file

Delete all lines beginning with a # from a file

All of the lines with comments in a file begin with # . How can I delete all of the lines (and only those lines) which begin with # ? Other lines containing # , but not at the beginning of the line should be ignored.

Does it have to work with the common convention that #blah \blah counts as a single «logical line» because the backslash escapes the newline?

@sarnold: apart from make , which utilities use the ‘backslash splices lines before ending a comment’? The shells (bash and ksh tested) don’t. C and C++ do handle newline splicing before other processing of preprocessor directives, but they’re directives rather than comments.

@Jonathan: Awesome. I had assumed that the common \ escaping would also work on comments. But wow I was wrong. I haven’t been able to find another example yet. 🙂 Thanks!

10 Answers 10

This says, «find all lines that start with # and delete them, leaving everything else.»

I’m a little surprised nobody has suggested the most obvious solution:

This solves the problem as stated.

But note that a common convention is for everything from a # to the end of a line to be treated as a comment:

though that treats, for example, a # character within a string literal as the beginning of a comment (which may or may not be relevant for your case) (and it leaves empty lines).

A line starting with arbitrary whitespace followed by # might also be treated as a comment:

if whitespace is only spaces, or

where the two spaces are actually a space followed by a literal tab character (type «control-v tab»).

For all these commands, omit the filename argument to read from standard input (e.g., as part of a pipe).

Источник

How to Remove Lines from a File Using Sed Command

streaming editor (sed) is an important tool when you work with parsing and transforming text in your nix-based systems. It is used for finding, filtering, text substitution, and text manipulations such as insertion, deletion, replace, and search in the text files.

In most Linux distributions, the sed command comes pre-installed and you can verify it using the following commands, which will show the binary location of the command and version.

Читайте также:  Linux copy full folder

Here in this article, I am going to show you how to remove lines from a file using the sed command with the help of a sample file that contains 7 lines. I am going to use this file for demonstration purposes.

$ cat testfile.txt First line second line Third line Fourth line Fifth line Sixth line SIXTH LINE

How to Delete a Line from a File

To delete the line from a file you can use the below command. You have to substitute ‘N’ with the line number and ‘d’ is to delete the line.

If you have to delete the fourth line from the file then you have to substitute N=4 .

$ sed '4d' testfile.txt

Delete Line from File

How to Delete First and Last Line from a File

You can delete the first line from a file using the same syntax as described in the previous example. You have to put N=1 which will remove the first line.

$ sed '1d' testfile.txt

To delete the last line from a file using the below command with ($) sign that denotes the last line of a file.

$ sed '$d' testfile.txt

Delete First and Last Lines from File

How to Delete Range of Lines from a File

You can delete a range of lines from a file. Let’s say you want to delete lines from 3 to 5, you can use the below syntax.

To actually delete, use the following command to do it.

Delete Range of Lines from-File

You can use ! symbol to negate the delete operation. This will delete all lines except the given range(3-5).

Negate Operation

How to Remove Blank Lines from a File

To delete all blank lines from a file run the following command. An important point to note is using this command, empty lines with spaces will not be deleted. I have added empty lines and empty lines with spaces in my test file.

$ cat testfile.txt First line second line Third line Fourth line Fifth line Sixth line SIXTH LINE

Lines with Spaces Not Removed

From the above image, you can see empty lines are deleted but lines that have spaces are not deleted. To delete all lines including spaces you can run the following command.

Lines with Spaces Removed

How to Delete Lines Starting with Words in a File

To delete a line that starts with a certain word run the following command with ^ symbol represents the start of the word followed by the actual word.

To delete a line that ends with a certain word run the following command. The word to be deleted followed by the $ symbol will delete lines.

Delete Line Start with Words in File

How to Make Changes Directly into a File

To make the changes directly in the file using sed you have to pass -i flag which will make the changes directly in the file.

We have come to the end of the article. The sed command will play a major part when you are working on manipulating any files. When combined with other Linux utilities like awk, grep you can do more things with sed.

Источник

Sed Command to Delete a Line

Sed is a built-in Linux tool for text manipulation. The term sed stands for stream editor. Despite the name, sed isn’t a text editor by itself. Rather, it takes text as input, performs various text modifications according to instructions, and prints the output.

Читайте также:  Linux cpu speed test

This guide will demonstrate how to use sed to delete a line from a text.

Sed on Linux

The full name of sed gives us a hint at its working method. Sed takes the input text as a stream. The text can come from anywhere – a text file or standard output (STDOUT). After taking the input, sed operates on it line by line.

For demonstration, here’s a simple text file I’ve generated.

Deleting line using sed

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.

Delete single line

The following sed command will delete the first line of the text.

Basically, to delete a line, you need the line number of the target line. Let’s remove line 5.

To delete the last line of the text file, instead of manually calculating the line number, use “$” instead.

Delete a range of line

Sed can delete a range of lines. Here, the minimum line value is 1, and the maximum line value is 5. To declare range, we’re using comma (,).

Delete multiple lines

What if the lines you desire to remove are not in a fixed range? Have a look at the following sed command. Note that we’re using a semicolon (;) as the delimiter. Essentially, each delimited option is a separate sed command.

Delete all lines except specified range

In the next example, sed will only keep the lines described by the range. Here, “!” is the negation operator, telling sed to keep the specific lines.

Delete empty lines

If there are multiple empty or blank lines in the text, the following sed command will remove all of them.

Delete lines based on pattern

Sed can search for a particular pattern and perform the specified actions on the line. We can use this feature to delete specific lines that match a pattern.

Let’s have a look at the following demonstration. Sed will remove any line that contains the string “the”.

We can also describe multiple strings to search for. Each string is delimited using the symbol “\|”.

Delete lines starting with a specific character

To denote the starting of a line, we’ll use the caret (^) symbol.

The following sed command will remove all the lines starting with a digit. Here, the character group “[:digit:]” describes all digits (0-9).

We can also describe multiple characters for a valid match. The following example will match all the lines that start with “t” and “b”.

In the next example, check out how to remove all the lines that start with an uppercase character. Here, we’re using the uppercase character group “[:upper:]”.

If the target lines have lowercase characters at the start, use the lowercase character group “[:lower:]”.

Читайте также:  Лучший usb дистрибутив linux

Delete lines ending with specific character

To denote the end of a line, we can use the symbol “$”. It describes the match with the last occurrence of the pattern.

In the next example, sed will delete lines ending with “e”.

Let’s try with a multiple-character search.

Deleting lines that match the pattern and the next line

We’ve already demonstrated how to delete a line if a pattern matches. We can further extend and delete the subsequent line as well.

Check out the following sed command.

Sed will match the line that contains “the” and delete the subsequent line as well.

Deleting line from the pattern match to the end

We can further extend the previous example to order sed to delete all the lines, starting from the first match of the pattern.

Here, sed will delete the line that matches the pattern “the” first and all lines afterward.

Final thought

Sed is a simple tool. However, it can do wonders, thanks to the support for regular expression. Sed also integrates seamlessly in various scripts.

This was just a short guide demonstrating one of the sed functions – deleting lines. There are tons of other things you can do with sed. For example, check out this mega guide on 50 sed examples. It’s a fantastic guide covering all the basics and many advanced sed implementations.

About the author

Sidratul Muntaha

Student of CSE. I love Linux and playing with tech and gadgets. I use both Ubuntu and Linux Mint.

Источник

How to Delete all Text in a File Using Vi/Vim Editor

Vim is a great tool for editing text or configuration files in Linux. One of the lesser-known Vim tricks is clearing or deleting all text or lines in a file. Although, this is not a frequently used operation, its a good practice to know or learn it.

In this article, we will describe steps on how to delete, remove or clear all text in a file using a Vim editor in different vim modes.

The first option is to remove, clear or delete the all lines in a file in the normal mode (note that Vim starts in “normal” mode by default). Immediately after opening a file, type “gg” to move the cursor to the first line of the file, assuming it is not already there. Then type dG to delete all the lines or text in it.

Delete Complete Text in Vi Editor

If Vim is in another mode, for example, insert mode, you can access normal mode by pressing Esc or C-[> .

Alternatively, you can also clear all lines or text in Vi/Vim in command mode by running the following command.

Delete Text in Vi Editor

Last but not least, here is a list of Vim articles that you will find useful:

In this article, we have explained how to clear or delete all lines or text in a file using Vi/Vim editor. Remember to share your thoughts with us or ask questions using the comment form below.

Источник

Оцените статью
Adblock
detector