How to pop (read&remove) a line of a file?
Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
I am trying to get the last line of a file and then delete it:
Read the line:
sed -n '$p' file
# or
tail -n 1 file
Remove the line:
sed -i '$d' file
This works, but is there a way to do this with just a single command ?
text-processing sed
add a comment |Â
up vote
2
down vote
favorite
I am trying to get the last line of a file and then delete it:
Read the line:
sed -n '$p' file
# or
tail -n 1 file
Remove the line:
sed -i '$d' file
This works, but is there a way to do this with just a single command ?
text-processing sed
I'm not focused onsed
. Any default tool will be fine.
– RoVo
29 mins ago
In what way doessed
not satisfy your requirements? To be able to suggest another way of doing it, we would need to know in what particular waysed
is not good enough. Or, do you want to perform both operations in one command?
– Kusalananda
27 mins ago
sed
is fine, but I'd like to open the file just once. If there is no way to do this, I'll be fine.
– RoVo
26 mins ago
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I am trying to get the last line of a file and then delete it:
Read the line:
sed -n '$p' file
# or
tail -n 1 file
Remove the line:
sed -i '$d' file
This works, but is there a way to do this with just a single command ?
text-processing sed
I am trying to get the last line of a file and then delete it:
Read the line:
sed -n '$p' file
# or
tail -n 1 file
Remove the line:
sed -i '$d' file
This works, but is there a way to do this with just a single command ?
text-processing sed
text-processing sed
edited 26 mins ago
asked 30 mins ago
RoVo
1,943213
1,943213
I'm not focused onsed
. Any default tool will be fine.
– RoVo
29 mins ago
In what way doessed
not satisfy your requirements? To be able to suggest another way of doing it, we would need to know in what particular waysed
is not good enough. Or, do you want to perform both operations in one command?
– Kusalananda
27 mins ago
sed
is fine, but I'd like to open the file just once. If there is no way to do this, I'll be fine.
– RoVo
26 mins ago
add a comment |Â
I'm not focused onsed
. Any default tool will be fine.
– RoVo
29 mins ago
In what way doessed
not satisfy your requirements? To be able to suggest another way of doing it, we would need to know in what particular waysed
is not good enough. Or, do you want to perform both operations in one command?
– Kusalananda
27 mins ago
sed
is fine, but I'd like to open the file just once. If there is no way to do this, I'll be fine.
– RoVo
26 mins ago
I'm not focused on
sed
. Any default tool will be fine.– RoVo
29 mins ago
I'm not focused on
sed
. Any default tool will be fine.– RoVo
29 mins ago
In what way does
sed
not satisfy your requirements? To be able to suggest another way of doing it, we would need to know in what particular way sed
is not good enough. Or, do you want to perform both operations in one command?– Kusalananda
27 mins ago
In what way does
sed
not satisfy your requirements? To be able to suggest another way of doing it, we would need to know in what particular way sed
is not good enough. Or, do you want to perform both operations in one command?– Kusalananda
27 mins ago
sed
is fine, but I'd like to open the file just once. If there is no way to do this, I'll be fine.– RoVo
26 mins ago
sed
is fine, but I'd like to open the file just once. If there is no way to do this, I'll be fine.– RoVo
26 mins ago
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
4
down vote
The goal is to have a single command that outputs the last line of a file, and at the same time deletes that line from the original file.
sed -i -e '$w /dev/stdout' -e 'd;' file
This would run the following sed
script:
$
w /dev/stdout
d
This writes the last line to /dev/stdout
and then deletes it. All other lines are written back into original file through the -i
option.
The script on the command line has to be split in two as there is no way to otherwise delimit the output filename of the w
command (other than inserting a literal newline).
With ed
:
ed -s file <<END_ED
p
d
w
END_ED
ed
opens the file file
and places the cursor on the last line of the file. The first command prints that line to standard output, the second deletes it, and the last command writes the buffer back to the file. Using ed
in this way may not be advisable on huge files.
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
The goal is to have a single command that outputs the last line of a file, and at the same time deletes that line from the original file.
sed -i -e '$w /dev/stdout' -e 'd;' file
This would run the following sed
script:
$
w /dev/stdout
d
This writes the last line to /dev/stdout
and then deletes it. All other lines are written back into original file through the -i
option.
The script on the command line has to be split in two as there is no way to otherwise delimit the output filename of the w
command (other than inserting a literal newline).
With ed
:
ed -s file <<END_ED
p
d
w
END_ED
ed
opens the file file
and places the cursor on the last line of the file. The first command prints that line to standard output, the second deletes it, and the last command writes the buffer back to the file. Using ed
in this way may not be advisable on huge files.
add a comment |Â
up vote
4
down vote
The goal is to have a single command that outputs the last line of a file, and at the same time deletes that line from the original file.
sed -i -e '$w /dev/stdout' -e 'd;' file
This would run the following sed
script:
$
w /dev/stdout
d
This writes the last line to /dev/stdout
and then deletes it. All other lines are written back into original file through the -i
option.
The script on the command line has to be split in two as there is no way to otherwise delimit the output filename of the w
command (other than inserting a literal newline).
With ed
:
ed -s file <<END_ED
p
d
w
END_ED
ed
opens the file file
and places the cursor on the last line of the file. The first command prints that line to standard output, the second deletes it, and the last command writes the buffer back to the file. Using ed
in this way may not be advisable on huge files.
add a comment |Â
up vote
4
down vote
up vote
4
down vote
The goal is to have a single command that outputs the last line of a file, and at the same time deletes that line from the original file.
sed -i -e '$w /dev/stdout' -e 'd;' file
This would run the following sed
script:
$
w /dev/stdout
d
This writes the last line to /dev/stdout
and then deletes it. All other lines are written back into original file through the -i
option.
The script on the command line has to be split in two as there is no way to otherwise delimit the output filename of the w
command (other than inserting a literal newline).
With ed
:
ed -s file <<END_ED
p
d
w
END_ED
ed
opens the file file
and places the cursor on the last line of the file. The first command prints that line to standard output, the second deletes it, and the last command writes the buffer back to the file. Using ed
in this way may not be advisable on huge files.
The goal is to have a single command that outputs the last line of a file, and at the same time deletes that line from the original file.
sed -i -e '$w /dev/stdout' -e 'd;' file
This would run the following sed
script:
$
w /dev/stdout
d
This writes the last line to /dev/stdout
and then deletes it. All other lines are written back into original file through the -i
option.
The script on the command line has to be split in two as there is no way to otherwise delimit the output filename of the w
command (other than inserting a literal newline).
With ed
:
ed -s file <<END_ED
p
d
w
END_ED
ed
opens the file file
and places the cursor on the last line of the file. The first command prints that line to standard output, the second deletes it, and the last command writes the buffer back to the file. Using ed
in this way may not be advisable on huge files.
edited 6 mins ago
answered 22 mins ago


Kusalananda
109k14211334
109k14211334
add a comment |Â
add a comment |Â
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f474838%2fhow-to-pop-readremove-a-line-of-a-file%23new-answer', 'question_page');
);
Post as a guest
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
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
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
I'm not focused on
sed
. Any default tool will be fine.– RoVo
29 mins ago
In what way does
sed
not satisfy your requirements? To be able to suggest another way of doing it, we would need to know in what particular waysed
is not good enough. Or, do you want to perform both operations in one command?– Kusalananda
27 mins ago
sed
is fine, but I'd like to open the file just once. If there is no way to do this, I'll be fine.– RoVo
26 mins ago