Delete First line of a file
up vote
93
down vote
favorite
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
shell-script sed ksh
add a comment |
up vote
93
down vote
favorite
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
shell-script sed ksh
add a comment |
up vote
93
down vote
favorite
up vote
93
down vote
favorite
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
shell-script sed ksh
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
shell-script sed ksh
shell-script sed ksh
edited Dec 5 '13 at 15:13
Zelda
4,7321526
4,7321526
asked Oct 16 '13 at 0:22
kickass13
493255
493255
add a comment |
add a comment |
11 Answers
11
active
oldest
votes
up vote
68
down vote
accepted
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
2
With BSDsed
, you may usesed -i .bak '1d' file.txt
.
– user26112
Oct 16 '13 at 0:37
is there a way that does not include temp file? Anyway, this will do the trick. Thanks a lot!
– kickass13
Oct 16 '13 at 0:38
@kickass13 possibly using a text editor such ased
.
– jordanm
Oct 16 '13 at 0:50
5
Theed
command would be:printf "%sn" 1d w q | ed file.txt
(I heart ed)
– glenn jackman
Oct 16 '13 at 1:44
1
@jonayreyes-exec sed -i '1d' {} ;
– jordanm
Aug 28 '17 at 19:12
|
show 5 more comments
up vote
123
down vote
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.
I've just tried it on my Fedora system and the output is above. You are correct - thanks for pointing that out.
– AsymLabs
Oct 16 '13 at 13:02
Thetail
trick worked for me (took less than 3 seconds on a 130mb file). Thanks!
– elo80ka
Aug 18 '14 at 17:51
echo "$(tail -n +2 file.txt)" > file.txt
is the perfect answer.
– Alex Raj Kaliamoorthy
Nov 22 '16 at 12:26
add a comment |
up vote
15
down vote
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
add a comment |
up vote
13
down vote
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
Yoursed '1d' d.txt
method did not include (or so it seems by reading your tests) themv
command. In my tests on FreeBSD with a 20MB file thesed -i
was the quickest.
– Sopalajo de Arrierez
Feb 11 at 23:04
add a comment |
up vote
9
down vote
ex
can be used for true in-place editing that does not involve a temp file
ex -c ':1d' -c ':wq' file.txt
2
ex does use a temp file.strace -e open ex -c ':1d' -c ':wq' foo
. ex truncates the original file with the temp file, where as GNU sed's -i option overwrites the original with the temp file. I am not sure how BSD's sed works.
– llua
Dec 5 '13 at 16:03
@llua, you are right. I noticed that too, but later
– iruvar
Dec 5 '13 at 16:06
add a comment |
up vote
3
down vote
You can use Vim in Ex mode:
ex -s -c '1d|x' file.txt
1
find first lined
deletex
save and close
add a comment |
up vote
1
down vote
The shortest and simplest way to delete the first line from a file using sed
is:
$ sed -i -n -e '2,$p' file.txt
add a comment |
up vote
0
down vote
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
add a comment |
up vote
0
down vote
To Delete a praticiler line in file
- Delete first line
Sed '1d' file
- Delete first and third line
Sed '1d3d' file
Delete charecter in line
1 Delete First two charter in lin
Sed 's/^..//' file
2 Delete last two chrectercin line
Sed 's/..£//' file
3 Delete blank line
Sed '/^£/d' file
4
Did the British retake the colonies? Last time I looked,£
≠$
.
– G-Man
Oct 22 '15 at 5:51
£ indicated doller sign..
– Vivek parikh
Oct 22 '15 at 8:27
add a comment |
up vote
0
down vote
Could use vim to do this:
vim -u NONE +'1d' +wq! /tmp/test.txt
add a comment |
up vote
-2
down vote
cat file01 | sed -e '1,3d'
// show content in file01 but remove the first and third line
2
the sed command you give will remove lines 1, 2 and 3.
– icarus
Feb 10 '17 at 1:29
1
This deletes more than is asked for and does not "keep the changes "
– Jeff Schaller
Feb 10 '17 at 1:55
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f96226%2fdelete-first-line-of-a-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
11 Answers
11
active
oldest
votes
11 Answers
11
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
68
down vote
accepted
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
2
With BSDsed
, you may usesed -i .bak '1d' file.txt
.
– user26112
Oct 16 '13 at 0:37
is there a way that does not include temp file? Anyway, this will do the trick. Thanks a lot!
– kickass13
Oct 16 '13 at 0:38
@kickass13 possibly using a text editor such ased
.
– jordanm
Oct 16 '13 at 0:50
5
Theed
command would be:printf "%sn" 1d w q | ed file.txt
(I heart ed)
– glenn jackman
Oct 16 '13 at 1:44
1
@jonayreyes-exec sed -i '1d' {} ;
– jordanm
Aug 28 '17 at 19:12
|
show 5 more comments
up vote
68
down vote
accepted
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
2
With BSDsed
, you may usesed -i .bak '1d' file.txt
.
– user26112
Oct 16 '13 at 0:37
is there a way that does not include temp file? Anyway, this will do the trick. Thanks a lot!
– kickass13
Oct 16 '13 at 0:38
@kickass13 possibly using a text editor such ased
.
– jordanm
Oct 16 '13 at 0:50
5
Theed
command would be:printf "%sn" 1d w q | ed file.txt
(I heart ed)
– glenn jackman
Oct 16 '13 at 1:44
1
@jonayreyes-exec sed -i '1d' {} ;
– jordanm
Aug 28 '17 at 19:12
|
show 5 more comments
up vote
68
down vote
accepted
up vote
68
down vote
accepted
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
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
answered Oct 16 '13 at 0:29
jordanm
30k28192
30k28192
2
With BSDsed
, you may usesed -i .bak '1d' file.txt
.
– user26112
Oct 16 '13 at 0:37
is there a way that does not include temp file? Anyway, this will do the trick. Thanks a lot!
– kickass13
Oct 16 '13 at 0:38
@kickass13 possibly using a text editor such ased
.
– jordanm
Oct 16 '13 at 0:50
5
Theed
command would be:printf "%sn" 1d w q | ed file.txt
(I heart ed)
– glenn jackman
Oct 16 '13 at 1:44
1
@jonayreyes-exec sed -i '1d' {} ;
– jordanm
Aug 28 '17 at 19:12
|
show 5 more comments
2
With BSDsed
, you may usesed -i .bak '1d' file.txt
.
– user26112
Oct 16 '13 at 0:37
is there a way that does not include temp file? Anyway, this will do the trick. Thanks a lot!
– kickass13
Oct 16 '13 at 0:38
@kickass13 possibly using a text editor such ased
.
– jordanm
Oct 16 '13 at 0:50
5
Theed
command would be:printf "%sn" 1d w q | ed file.txt
(I heart ed)
– glenn jackman
Oct 16 '13 at 1:44
1
@jonayreyes-exec sed -i '1d' {} ;
– jordanm
Aug 28 '17 at 19:12
2
2
With BSD
sed
, you may use sed -i .bak '1d' file.txt
.– user26112
Oct 16 '13 at 0:37
With BSD
sed
, you may use sed -i .bak '1d' file.txt
.– user26112
Oct 16 '13 at 0:37
is there a way that does not include temp file? Anyway, this will do the trick. Thanks a lot!
– kickass13
Oct 16 '13 at 0:38
is there a way that does not include temp file? Anyway, this will do the trick. Thanks a lot!
– kickass13
Oct 16 '13 at 0:38
@kickass13 possibly using a text editor such as
ed
.– jordanm
Oct 16 '13 at 0:50
@kickass13 possibly using a text editor such as
ed
.– jordanm
Oct 16 '13 at 0:50
5
5
The
ed
command would be: printf "%sn" 1d w q | ed file.txt
(I heart ed)– glenn jackman
Oct 16 '13 at 1:44
The
ed
command would be: printf "%sn" 1d w q | ed file.txt
(I heart ed)– glenn jackman
Oct 16 '13 at 1:44
1
1
@jonayreyes
-exec sed -i '1d' {} ;
– jordanm
Aug 28 '17 at 19:12
@jonayreyes
-exec sed -i '1d' {} ;
– jordanm
Aug 28 '17 at 19:12
|
show 5 more comments
up vote
123
down vote
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.
I've just tried it on my Fedora system and the output is above. You are correct - thanks for pointing that out.
– AsymLabs
Oct 16 '13 at 13:02
Thetail
trick worked for me (took less than 3 seconds on a 130mb file). Thanks!
– elo80ka
Aug 18 '14 at 17:51
echo "$(tail -n +2 file.txt)" > file.txt
is the perfect answer.
– Alex Raj Kaliamoorthy
Nov 22 '16 at 12:26
add a comment |
up vote
123
down vote
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.
I've just tried it on my Fedora system and the output is above. You are correct - thanks for pointing that out.
– AsymLabs
Oct 16 '13 at 13:02
Thetail
trick worked for me (took less than 3 seconds on a 130mb file). Thanks!
– elo80ka
Aug 18 '14 at 17:51
echo "$(tail -n +2 file.txt)" > file.txt
is the perfect answer.
– Alex Raj Kaliamoorthy
Nov 22 '16 at 12:26
add a comment |
up vote
123
down vote
up vote
123
down vote
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.
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.
edited Oct 16 '13 at 13:38
answered Oct 16 '13 at 11:32
AsymLabs
1,7061711
1,7061711
I've just tried it on my Fedora system and the output is above. You are correct - thanks for pointing that out.
– AsymLabs
Oct 16 '13 at 13:02
Thetail
trick worked for me (took less than 3 seconds on a 130mb file). Thanks!
– elo80ka
Aug 18 '14 at 17:51
echo "$(tail -n +2 file.txt)" > file.txt
is the perfect answer.
– Alex Raj Kaliamoorthy
Nov 22 '16 at 12:26
add a comment |
I've just tried it on my Fedora system and the output is above. You are correct - thanks for pointing that out.
– AsymLabs
Oct 16 '13 at 13:02
Thetail
trick worked for me (took less than 3 seconds on a 130mb file). Thanks!
– elo80ka
Aug 18 '14 at 17:51
echo "$(tail -n +2 file.txt)" > file.txt
is the perfect answer.
– Alex Raj Kaliamoorthy
Nov 22 '16 at 12:26
I've just tried it on my Fedora system and the output is above. You are correct - thanks for pointing that out.
– AsymLabs
Oct 16 '13 at 13:02
I've just tried it on my Fedora system and the output is above. You are correct - thanks for pointing that out.
– AsymLabs
Oct 16 '13 at 13:02
The
tail
trick worked for me (took less than 3 seconds on a 130mb file). Thanks!– elo80ka
Aug 18 '14 at 17:51
The
tail
trick worked for me (took less than 3 seconds on a 130mb file). Thanks!– elo80ka
Aug 18 '14 at 17:51
echo "$(tail -n +2 file.txt)" > file.txt
is the perfect answer.– Alex Raj Kaliamoorthy
Nov 22 '16 at 12:26
echo "$(tail -n +2 file.txt)" > file.txt
is the perfect answer.– Alex Raj Kaliamoorthy
Nov 22 '16 at 12:26
add a comment |
up vote
15
down vote
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
add a comment |
up vote
15
down vote
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
add a comment |
up vote
15
down vote
up vote
15
down vote
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
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
edited Oct 16 '13 at 0:58
answered Oct 16 '13 at 0:43
user26112
add a comment |
add a comment |
up vote
13
down vote
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
Yoursed '1d' d.txt
method did not include (or so it seems by reading your tests) themv
command. In my tests on FreeBSD with a 20MB file thesed -i
was the quickest.
– Sopalajo de Arrierez
Feb 11 at 23:04
add a comment |
up vote
13
down vote
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
Yoursed '1d' d.txt
method did not include (or so it seems by reading your tests) themv
command. In my tests on FreeBSD with a 20MB file thesed -i
was the quickest.
– Sopalajo de Arrierez
Feb 11 at 23:04
add a comment |
up vote
13
down vote
up vote
13
down vote
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
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
edited Jun 10 '15 at 4:29
Tejas
1,79221839
1,79221839
answered Jun 10 '15 at 4:04
waue0920
13112
13112
Yoursed '1d' d.txt
method did not include (or so it seems by reading your tests) themv
command. In my tests on FreeBSD with a 20MB file thesed -i
was the quickest.
– Sopalajo de Arrierez
Feb 11 at 23:04
add a comment |
Yoursed '1d' d.txt
method did not include (or so it seems by reading your tests) themv
command. In my tests on FreeBSD with a 20MB file thesed -i
was the quickest.
– Sopalajo de Arrierez
Feb 11 at 23:04
Your
sed '1d' d.txt
method did not include (or so it seems by reading your tests) the mv
command. In my tests on FreeBSD with a 20MB file the sed -i
was the quickest.– Sopalajo de Arrierez
Feb 11 at 23:04
Your
sed '1d' d.txt
method did not include (or so it seems by reading your tests) the mv
command. In my tests on FreeBSD with a 20MB file the sed -i
was the quickest.– Sopalajo de Arrierez
Feb 11 at 23:04
add a comment |
up vote
9
down vote
ex
can be used for true in-place editing that does not involve a temp file
ex -c ':1d' -c ':wq' file.txt
2
ex does use a temp file.strace -e open ex -c ':1d' -c ':wq' foo
. ex truncates the original file with the temp file, where as GNU sed's -i option overwrites the original with the temp file. I am not sure how BSD's sed works.
– llua
Dec 5 '13 at 16:03
@llua, you are right. I noticed that too, but later
– iruvar
Dec 5 '13 at 16:06
add a comment |
up vote
9
down vote
ex
can be used for true in-place editing that does not involve a temp file
ex -c ':1d' -c ':wq' file.txt
2
ex does use a temp file.strace -e open ex -c ':1d' -c ':wq' foo
. ex truncates the original file with the temp file, where as GNU sed's -i option overwrites the original with the temp file. I am not sure how BSD's sed works.
– llua
Dec 5 '13 at 16:03
@llua, you are right. I noticed that too, but later
– iruvar
Dec 5 '13 at 16:06
add a comment |
up vote
9
down vote
up vote
9
down vote
ex
can be used for true in-place editing that does not involve a temp file
ex -c ':1d' -c ':wq' 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
edited Dec 5 '13 at 16:08
answered Oct 16 '13 at 1:03
iruvar
11.7k62959
11.7k62959
2
ex does use a temp file.strace -e open ex -c ':1d' -c ':wq' foo
. ex truncates the original file with the temp file, where as GNU sed's -i option overwrites the original with the temp file. I am not sure how BSD's sed works.
– llua
Dec 5 '13 at 16:03
@llua, you are right. I noticed that too, but later
– iruvar
Dec 5 '13 at 16:06
add a comment |
2
ex does use a temp file.strace -e open ex -c ':1d' -c ':wq' foo
. ex truncates the original file with the temp file, where as GNU sed's -i option overwrites the original with the temp file. I am not sure how BSD's sed works.
– llua
Dec 5 '13 at 16:03
@llua, you are right. I noticed that too, but later
– iruvar
Dec 5 '13 at 16:06
2
2
ex does use a temp file.
strace -e open ex -c ':1d' -c ':wq' foo
. ex truncates the original file with the temp file, where as GNU sed's -i option overwrites the original with the temp file. I am not sure how BSD's sed works.– llua
Dec 5 '13 at 16:03
ex does use a temp file.
strace -e open ex -c ':1d' -c ':wq' foo
. ex truncates the original file with the temp file, where as GNU sed's -i option overwrites the original with the temp file. I am not sure how BSD's sed works.– llua
Dec 5 '13 at 16:03
@llua, you are right. I noticed that too, but later
– iruvar
Dec 5 '13 at 16:06
@llua, you are right. I noticed that too, but later
– iruvar
Dec 5 '13 at 16:06
add a comment |
up vote
3
down vote
You can use Vim in Ex mode:
ex -s -c '1d|x' file.txt
1
find first lined
deletex
save and close
add a comment |
up vote
3
down vote
You can use Vim in Ex mode:
ex -s -c '1d|x' file.txt
1
find first lined
deletex
save and close
add a comment |
up vote
3
down vote
up vote
3
down vote
You can use Vim in Ex mode:
ex -s -c '1d|x' file.txt
1
find first lined
deletex
save and close
You can use Vim in Ex mode:
ex -s -c '1d|x' file.txt
1
find first lined
deletex
save and close
edited 2 days ago
answered Apr 11 '16 at 0:01
Steven Penny
2,54021738
2,54021738
add a comment |
add a comment |
up vote
1
down vote
The shortest and simplest way to delete the first line from a file using sed
is:
$ sed -i -n -e '2,$p' file.txt
add a comment |
up vote
1
down vote
The shortest and simplest way to delete the first line from a file using sed
is:
$ sed -i -n -e '2,$p' file.txt
add a comment |
up vote
1
down vote
up vote
1
down vote
The shortest and simplest way to delete the first line from a file using sed
is:
$ sed -i -n -e '2,$p' file.txt
The shortest and simplest way to delete the first line from a file using sed
is:
$ sed -i -n -e '2,$p' file.txt
answered Dec 13 '16 at 9:55
starfry
3,10812747
3,10812747
add a comment |
add a comment |
up vote
0
down vote
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
add a comment |
up vote
0
down vote
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
add a comment |
up vote
0
down vote
up vote
0
down vote
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 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
edited Oct 8 '14 at 1:59
Community♦
1
1
answered Oct 21 '13 at 7:53
Ritesh Singh
11
11
add a comment |
add a comment |
up vote
0
down vote
To Delete a praticiler line in file
- Delete first line
Sed '1d' file
- Delete first and third line
Sed '1d3d' file
Delete charecter in line
1 Delete First two charter in lin
Sed 's/^..//' file
2 Delete last two chrectercin line
Sed 's/..£//' file
3 Delete blank line
Sed '/^£/d' file
4
Did the British retake the colonies? Last time I looked,£
≠$
.
– G-Man
Oct 22 '15 at 5:51
£ indicated doller sign..
– Vivek parikh
Oct 22 '15 at 8:27
add a comment |
up vote
0
down vote
To Delete a praticiler line in file
- Delete first line
Sed '1d' file
- Delete first and third line
Sed '1d3d' file
Delete charecter in line
1 Delete First two charter in lin
Sed 's/^..//' file
2 Delete last two chrectercin line
Sed 's/..£//' file
3 Delete blank line
Sed '/^£/d' file
4
Did the British retake the colonies? Last time I looked,£
≠$
.
– G-Man
Oct 22 '15 at 5:51
£ indicated doller sign..
– Vivek parikh
Oct 22 '15 at 8:27
add a comment |
up vote
0
down vote
up vote
0
down vote
To Delete a praticiler line in file
- Delete first line
Sed '1d' file
- Delete first and third line
Sed '1d3d' file
Delete charecter in line
1 Delete First two charter in lin
Sed 's/^..//' file
2 Delete last two chrectercin line
Sed 's/..£//' file
3 Delete blank line
Sed '/^£/d' file
To Delete a praticiler line in file
- Delete first line
Sed '1d' file
- Delete first and third line
Sed '1d3d' file
Delete charecter in line
1 Delete First two charter in lin
Sed 's/^..//' file
2 Delete last two chrectercin line
Sed 's/..£//' file
3 Delete blank line
Sed '/^£/d' file
edited Oct 22 '15 at 5:36
answered Oct 22 '15 at 5:30
Vivek parikh
6113
6113
4
Did the British retake the colonies? Last time I looked,£
≠$
.
– G-Man
Oct 22 '15 at 5:51
£ indicated doller sign..
– Vivek parikh
Oct 22 '15 at 8:27
add a comment |
4
Did the British retake the colonies? Last time I looked,£
≠$
.
– G-Man
Oct 22 '15 at 5:51
£ indicated doller sign..
– Vivek parikh
Oct 22 '15 at 8:27
4
4
Did the British retake the colonies? Last time I looked,
£
≠$
.– G-Man
Oct 22 '15 at 5:51
Did the British retake the colonies? Last time I looked,
£
≠$
.– G-Man
Oct 22 '15 at 5:51
£ indicated doller sign..
– Vivek parikh
Oct 22 '15 at 8:27
£ indicated doller sign..
– Vivek parikh
Oct 22 '15 at 8:27
add a comment |
up vote
0
down vote
Could use vim to do this:
vim -u NONE +'1d' +wq! /tmp/test.txt
add a comment |
up vote
0
down vote
Could use vim to do this:
vim -u NONE +'1d' +wq! /tmp/test.txt
add a comment |
up vote
0
down vote
up vote
0
down vote
Could use vim to do this:
vim -u NONE +'1d' +wq! /tmp/test.txt
Could use vim to do this:
vim -u NONE +'1d' +wq! /tmp/test.txt
answered Oct 17 '17 at 14:28
Hongbo Liu
1011
1011
add a comment |
add a comment |
up vote
-2
down vote
cat file01 | sed -e '1,3d'
// show content in file01 but remove the first and third line
2
the sed command you give will remove lines 1, 2 and 3.
– icarus
Feb 10 '17 at 1:29
1
This deletes more than is asked for and does not "keep the changes "
– Jeff Schaller
Feb 10 '17 at 1:55
add a comment |
up vote
-2
down vote
cat file01 | sed -e '1,3d'
// show content in file01 but remove the first and third line
2
the sed command you give will remove lines 1, 2 and 3.
– icarus
Feb 10 '17 at 1:29
1
This deletes more than is asked for and does not "keep the changes "
– Jeff Schaller
Feb 10 '17 at 1:55
add a comment |
up vote
-2
down vote
up vote
-2
down vote
cat file01 | sed -e '1,3d'
// show content in file01 but remove the first and third line
cat file01 | sed -e '1,3d'
// show content in file01 but remove the first and third line
answered Feb 10 '17 at 1:24
user215264
1
1
2
the sed command you give will remove lines 1, 2 and 3.
– icarus
Feb 10 '17 at 1:29
1
This deletes more than is asked for and does not "keep the changes "
– Jeff Schaller
Feb 10 '17 at 1:55
add a comment |
2
the sed command you give will remove lines 1, 2 and 3.
– icarus
Feb 10 '17 at 1:29
1
This deletes more than is asked for and does not "keep the changes "
– Jeff Schaller
Feb 10 '17 at 1:55
2
2
the sed command you give will remove lines 1, 2 and 3.
– icarus
Feb 10 '17 at 1:29
the sed command you give will remove lines 1, 2 and 3.
– icarus
Feb 10 '17 at 1:29
1
1
This deletes more than is asked for and does not "keep the changes "
– Jeff Schaller
Feb 10 '17 at 1:55
This deletes more than is asked for and does not "keep the changes "
– Jeff Schaller
Feb 10 '17 at 1:55
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f96226%2fdelete-first-line-of-a-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown