Delete First line of a file
How can I delete the first line of a file and keep the changes?
I tried this but it erases the whole content of the file.
$sed 1d file.txt > file.txt
The reason file.txt is empty after that command is the order in which the shell does things. The first thing that happens with that line is the redirection. The file “file.txt” is opened and truncated to 0 bytes. After that the sed command runs, but at the point the file is already empty.
There are a few options, most involve writing to a temporary file.
sed '1d' file.txt > tmpfile; mv tmpfile file.txt # POSIX
sed -i '1d' file.txt # GNU sed only, creates a temporary file
perl -ip -e '$_ = undef if $. == 1' file.txt # also creates a temporary file
Also take a look at sponge
from
moreutils
. sponge
soaks in data from
standard input until standard input’s writing end closes before writing to a
file. It is used like so:
sed '1d' file.txt | sponge file.txt
ex
can be used for true in-place editing that does not involve a temp file
ex -c ':1d' -c ':wq' file.txt
An alternative very lightweight option is just to ‘tail’ everything but the first line (this can be an easy way to remove file headers generally):
# -n +2 : start at line 2 of the file.
tail -n +2 file.txt > file.stdout
Following @Evan Teitelman, you can:
tail -n +2 file.txt | sponge file.txt
To avoid a temporary file. Another option might be:
echo "$(tail -n +2 file.txt)" > file.txt
And so forth. Testing last one:
[user@work ~]$ cat file.txt
line 1
line 2
line 3
line 4
line 5
[user@work ~]$ echo "$(tail -n +2 file.txt)" > file.txt
[user@work ~]$ cat file.txt
line 2
line 3
line 4
line 5
[user@work ~]$
Oops we lost a newline (per @1_CR comment below), try instead:
printf "%snn" "$(tail -n +2 file.txt)" > file.txt
[user@work ~]$ cat file.txt
line 1
line 2
line 3
line 4
line 5
[user@work ~]$ printf '%snn' "$(tail -n +2 file.txt)" > file.txt
[user@work ~]$ cat file.txt
line 2
line 3
line 4
line 5
[user@work ~]$
Coming back to sed, try:
printf '%snn' "$(sed '1d' file.txt)" > file.txt
or perhaps
echo -e "$(sed '1d' file.txt)n" > file.txt
To avoid side effects.
This command will remove 1 line and save as “file.txt”.
sed '1d' file.txt > /tmp/file.txt && mv /tmp/file.txt file.txt || rm -f /tmp/file.txt
This topic is interest, so I test the benchmark in 3 ways:
sed '1d' d.txt > tmp.txt
tail -n +2 d.txt > tmp.txt
sed -i '1d' d.txt
Note that target d.txt
is 5.4GB file
Get the result :
run 1 : sed '1d' d.txt > r1.txt
14s
run 2 : tail -n +2 d.txt > r2.txt
20s
run 3 : sed -i '1d' d.txt
88s
Conclusion : It seems below be the quickest way:
sed '1d' file.txt > tmpfile; mv tmpfile file.txt
To delete a particular line in file:
- Delete first line
sed '1d' file
- Delete first and third line
sed '1d3d' file
To delete a charecter in a line
- Delete First two charters in lin
sed 's/^..//' file
- Delete last two chrecters in line
sed 's/..$//' file
- Delete blank line
sed '/^$/d' file
You can use Vim in Ex mode:
ex -s -c '1d|x' file.txt
-
1
find first line -
d
delete -
x
save and close
The shortest and simplest way to delete the first line from a file using sed
is:
$ sed -i -n -e '2,$p' file.txt
Could use vim to do this:
vim -u NONE +'1d' +wq! /tmp/test.txt
You can try this:
awk 'NR>1 {print}' file.txt > file1.txt