- How to to delete a line given with a variable in sed?
- 4 Answers 4
- How to Remove Lines from a File Using Sed Command
- How to Delete a Line from a File
- How to Delete First and Last Line from a File
- How to Delete Range of Lines from a File
- How to Remove Blank Lines from a File
- How to Delete Lines Starting with Words in a File
- How to Make Changes Directly into a File
- Sed Command to Delete a Line
- Sed on Linux
- Deleting line using sed
- Delete single line
- Delete a range of line
- Delete multiple lines
- Delete all lines except specified range
- Delete empty lines
- Delete lines based on pattern
- Delete lines starting with a specific character
- Delete lines ending with specific character
- Deleting lines that match the pattern and the next line
- Deleting line from the pattern match to the end
- Final thought
- About the author
- Sidratul Muntaha
How to to delete a line given with a variable in sed?
I am attempting to use sed to delete a line, read from user input, from a file whose name is stored in a variable. Right now all sed does is print the line and nothing else. This is a code snippet of the command I am using:
FILE="/home/devosion/scripts/files/todo.db" read DELETELINE sed -e "$DELETELINE"'d' "$FILE"
Is there something I am missing here? Edit: Switching out the -e option with -i fixed my woes!
Unless delete line is a line number it needs to be delimited. Please show the line in question and what the variable contain.
@123 I am uncertain, but im pretty sure bash’s read takes user input as a string. I thought this might be a hurdle. How would I fix it to make it work? EDIT: Updated with variables.
Whoa bobby tables, you need need to validate your user input: if I enter 1,$ , I’ve just deleted every line.
@glenn jackman That it will. I have a conditional that makes sure the input is a number and within a range. The script is still in it’s infancy, but since it’s just for my own use I’m not too worried.
4 Answers 4
You need to delimit the search.
#!/bin/bash read -r Line sed "/$Line/d" file
Will delete any line containing the typed input.
Bear in mind that sed matches on regex though and any special characters will be seen as such.
For example searching for 1* will actually delete lines containing any number of 1’s not an actual 1 and a star.
Also bear in mind that when the variable expands, it cannot contain the delimiters or the command will break or have unexpexted results.
For example if «$Line» contained «/hello» then the sed command will fail with sed: -e expression #1, char 4: extra characters after command .
You can either escape the / in this case or use different delimiters.
Personally i would use awk for this
awk -vLine="$Line" '!index($0,Line)' file
Which searches for an exact string and has none of the drawbacks of the sed command.
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.
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
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
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.
You can use ! symbol to negate the delete operation. This will delete all lines except the given range(3-5).
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
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.
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.
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.
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:]”.
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.