I am trying to delete first two lines and last four lines from my text files. How can I do this with Bash?
Asked
Active
Viewed 8.4k times
67
-
The following may help you with deleting lines at the end of the file: [http://stackoverflow.com/questions/4881930/bash-remove-the-last-line-from-a-file](http://stackoverflow.com/questions/4881930/bash-remove-the-last-line-from-a-file) – Linger May 06 '12 at 03:31
-
I am looking how to do this in place (without using a second file) – Alexander Mills Jul 08 '19 at 05:23
4 Answers
90
You can combine tail and head:
$ tail -n +3 file.txt | head -n -4 > file.txt.new && mv file.txt.new file.txt

Frédéric Hamidi
- 258,201
- 41
- 486
- 479
-
2Note that from Git Bash under Windows, `tail -n +3` works but `head -n -4` tells `invalid number of lines`. Seems like a bug (perhaps an old version of `head` is used). – jakub.g Nov 22 '13 at 12:30
-
14bsd-utils' head can't have negative numbers to -n, so this doesn't work on bsd and macos. – johannes_lalala Jan 22 '15 at 11:32
-
If you have a large file and want to watch progress, use `$ pv file.txt | tail -n +3 | head -n -4 > file.txt.new && mv file.txt.new file.txt`. You'll get a handy progress meter. – Sir Robert Mar 20 '17 at 19:16
-
1slightly tangential comment: i just came across a util that is similar to `tail` called Debian `buthead`, which more people should have the chance to enjoy: https://manpages.debian.org/jessie/buthead/buthead.1.en.html (it outputs all "but head"... as in "all but N lines from the head") – pestophagous Jul 27 '20 at 21:05
-
With `brew` on macOs you can install `coreutils` this will install GNU core utils including head or tail, the binary are prefixed with `g` to avoid collision with system utils. So simply prepending `g` to the above answer should work `gtail -n +3 file.txt | ghead -n -4 > file.txt.new && mv file.txt.new file.txt`. – bric3 Jun 08 '23 at 07:53
21
Head and Tail
cat input.txt | tail -n +3 | head -n -4
Sed Solution
cat input.txt | sed '1,2d' | sed -n -e :a -e '1,4!{P;N;D;};N;ba'

Sophie Alpert
- 139,698
- 36
- 220
- 238

Debaditya
- 2,419
- 1
- 27
- 46
-
4You don't need to use `cat`: `tail -n +3 input.txt | ...` works perfectly (and the same for `sed`). – huon May 05 '12 at 11:41
-
4Thank you for the sed solution; I needed it on a machine that doesn't allow negative line numbers for head. (The 4 in the first command should be negated, by the way.) – eswald Aug 08 '12 at 19:55
14
This is the quickest way I found:
sed -i 1,2d filename

finferflu
- 1,368
- 2
- 11
- 28
-
`-i extension Edit files in-place, saving backups with the specified extension. If a zero-length extension is given, no backup will be saved. It is not recommended to give a zero- length extension when in-place editing files, as you risk corruption or partial content in situations where disk space is exhausted, etc.` – finferflu Jan 22 '15 at 12:16
-
That's what the documentation says, at least the implementation of sed on my system… – finferflu Jan 22 '15 at 12:17
-
sorry, you are right. I didn't realize, because osx's bsdutils-sed doesn't know -i – johannes_lalala Jan 24 '15 at 13:00
-
-
4This only deletes the first two lines, but the last four should also be deleted. – jcsahnwaldt Reinstate Monica Apr 09 '17 at 19:35
-
Oops, you're right. I must have missed the last bit! I suppose the command might be combined with the `head` command, as suggested in the above reply. I'm gonna update my answer. – finferflu Apr 11 '17 at 12:27
-
Actually, that doesn't really work, so the best solution remains the one suggested by @Debaditya above. – finferflu Apr 11 '17 at 12:34
3
You can call the ex editor from the bash command line using the following sample. Note it uses a here document to end the list of commands to ex.
ex text.file << EOF
1,2d
$
-3,.d
x
EOF

octopusgrabbus
- 10,555
- 15
- 68
- 131

pizza
- 7,296
- 1
- 25
- 22