- How do I delete the first n lines and last line of a file using shell commands?
- 9 Answers 9
- head; head
- OR with a POSIX sed :
- 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 do I delete the first n lines and last line of a file using shell commands?
You’re probably best off fixing this inside SQL*Plus; rather than generating a file and then trying to trim the stuff you don’t want, you can just tell SQL*Plus not to generate that stuff to begin with. One approach is described in the «Creating a Flat File» section at docs.oracle.com/cd/A84870_01/doc/sqlplus.816/a75664/ch44.htm; another approach is described at stackoverflow.com/q/2299375/978917.
9 Answers 9
How it works :
- -i option edit the file itself. You could also remove that option and redirect the output to a new file or another command if you want.
- 1d deletes the first line ( 1 to only act on the first line, d to delete it)
- $d deletes the last line ( $ to only act on the last line, d to delete it)
Going further :
- You can also delete a range. For example, 1,5d would delete the first 5 lines.
- You can also delete every line that begins with SQL> using the statement /^SQL> /d
- You could delete every blank line with /^$/d
- Finally, you can combine any of the statement by separating them with a semi-colon ( statement1;statement2;satement3;. ) or by specifying them separately on the command line ( -e ‘statement1’ -e ‘statement 2’ . )
If its 3rd line to delete. then i have to use 3d in place of 1d? if its 3rd line from last to delete. then what will be the command?
@Nainita You can specify a range ( 1,3d will delete the first three lines) but it’s a little more difficult for the end. Depending on what you want, you could be better off using this : sed -i ‘/^SQL> /d’ Element_query to delete lines that begins with SQL> no matter where it is in the file.
@Nainita — see my answer here for arbitrary tail counts — it offers two solutions for stripping count lines as relative to the end of the file. One is a sed one-liner — which will work for stripping arbitrary line counts from the head and tail of a file, Better though, as long as input is a regular file, is just to group a single input across two head processes — it is the fastest way to do this usually.
head; head
< head -n[num] >/dev/null head -n[num] > outfile
With the above you can specify the first number of lines to strip off of the head of the output w/ the first head command, and the number of lines to write to outfile with the second. It will also typically do this faster than sed — especially when input is large — despite requiring two invocations. Where sed definitely should be preferred though, is in the case that not a regular, lseekable file — because this will typically not work as intended in that case, but sed can handle all output modifications in a single, scripted process.
With a GNU head you can use the — negative form for [num] in the second command as well. In which case the following command will strip first and last lines from input:
OR with a POSIX sed :
Say, for example, I was reading an input of 20 lines and I wanted to strip the first 3 and the last 7. If I resolved to do so w/ sed , I would do it with a tail buffer. I would first add together three and seven for a total strip count of ten and then do:
seq 20 | sed -ne:n -e '3d;N;1,10bn' -eP\;D
That is an example which strips the first 3 and last 7 lines from input. The idea is that you can buffer as many lines as you wish to strip from the tail of input in the pattern space on a stack but only P rint the first of these for every line pulled in.
- On lines 1,10 sed P rints nothing because for each of those it is stacking input in pattern space line-by-line in a b ranch loop.
- On the 3rd line all of sed ‘s stack is d eleted — and so the first 3 lines are stripped from output in one fell swoop.
- When sed reaches the $ last line of input and attempts to pull in the N ext it hits EOF and stops processing entirely. But at that time pattern space contains all of lines 14,20 — none of which have yet been P rinted, and never are.
- On every other line sed P rints only up to the first occurring \n ewline in pattern space, and D eletes same before beginning a new cycle with what remains — or the next 6 lines of input. The 7th line is appended again to the stack with the N ext command in the new cycle.
And so, of seq ‘s output (which is 20 sequentially numbered lines), sed only prints:
This gets to be problematic when the number of lines you desire to strip from the tail of input is large — because sed ‘s performance is directly proportional to the size of its pattern space. Still, though, it is a viable solution in many cases — and POSIX specs a sed pattern space to handle at least 4kb before busting.
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.