How to move the last line in a file? [duplicate]
Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
This question already has an answer here:
Need to move the last line of the file to second line of the same file
4 answers
Example of a file
line 1
line 2
line 3
line 4
line 4
should be after line 1
line 1
line 4
line 2
line 3
Important: It could be more than 4 lines somehow the script has to understand that it has to read the last line, not line 4.
text-processing
marked as duplicate by don_crissti, Kusalananda, roaima, Sundeep, Jeff Schaller Sep 8 at 11:35
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |Â
up vote
2
down vote
favorite
This question already has an answer here:
Need to move the last line of the file to second line of the same file
4 answers
Example of a file
line 1
line 2
line 3
line 4
line 4
should be after line 1
line 1
line 4
line 2
line 3
Important: It could be more than 4 lines somehow the script has to understand that it has to read the last line, not line 4.
text-processing
marked as duplicate by don_crissti, Kusalananda, roaima, Sundeep, Jeff Schaller Sep 8 at 11:35
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
Not very effective but it works with GNU sed:sed '1!d' file; sed '$!d' file; sed -n '2,$$d;p' file
â Cyrus
Sep 8 at 9:54
Bash isnâÂÂt a text editor
â Jeff Schaller
Sep 8 at 9:58
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
This question already has an answer here:
Need to move the last line of the file to second line of the same file
4 answers
Example of a file
line 1
line 2
line 3
line 4
line 4
should be after line 1
line 1
line 4
line 2
line 3
Important: It could be more than 4 lines somehow the script has to understand that it has to read the last line, not line 4.
text-processing
This question already has an answer here:
Need to move the last line of the file to second line of the same file
4 answers
Example of a file
line 1
line 2
line 3
line 4
line 4
should be after line 1
line 1
line 4
line 2
line 3
Important: It could be more than 4 lines somehow the script has to understand that it has to read the last line, not line 4.
This question already has an answer here:
Need to move the last line of the file to second line of the same file
4 answers
text-processing
text-processing
edited Sep 8 at 10:13
don_crissti
47.3k15125155
47.3k15125155
asked Sep 8 at 6:12
John Doe
263
263
marked as duplicate by don_crissti, Kusalananda, roaima, Sundeep, Jeff Schaller Sep 8 at 11:35
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by don_crissti, Kusalananda, roaima, Sundeep, Jeff Schaller Sep 8 at 11:35
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
Not very effective but it works with GNU sed:sed '1!d' file; sed '$!d' file; sed -n '2,$$d;p' file
â Cyrus
Sep 8 at 9:54
Bash isnâÂÂt a text editor
â Jeff Schaller
Sep 8 at 9:58
add a comment |Â
Not very effective but it works with GNU sed:sed '1!d' file; sed '$!d' file; sed -n '2,$$d;p' file
â Cyrus
Sep 8 at 9:54
Bash isnâÂÂt a text editor
â Jeff Schaller
Sep 8 at 9:58
Not very effective but it works with GNU sed:
sed '1!d' file; sed '$!d' file; sed -n '2,$$d;p' file
â Cyrus
Sep 8 at 9:54
Not very effective but it works with GNU sed:
sed '1!d' file; sed '$!d' file; sed -n '2,$$d;p' file
â Cyrus
Sep 8 at 9:54
Bash isnâÂÂt a text editor
â Jeff Schaller
Sep 8 at 9:58
Bash isnâÂÂt a text editor
â Jeff Schaller
Sep 8 at 9:58
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
An alternative, slower, longer version.
#!/bin/sh
if [ ! -f "$1" ]
then
printf "No input filen"
exit
fi
FIRST=`head -n 1 "$1"`
LAST=`tail -n 1 "$1"`
MID=`sed -n '$d; 2,$p' "$1"`
printf "$FIRSTn$LASTn$MIDn"
exit
Slower because using head
, tail
and sed
to access the file each time. On a large file (or many files) this could be very noticeable.
Thesed
pipeline could be shortened intosed -n '$d; 2,$p'
â Kusalananda
Sep 8 at 8:59
@Kusalananda - Thanks, updated. The answers on the linked duplicate are way better. This must be some kind of test question.
â Tigger
Sep 8 at 12:11
add a comment |Â
up vote
2
down vote
$ printf '4m1n,pn' | ed -s file
line 1
line 4
line 2
line 3
The short ed
script
4m1
,p
will move line four to after line one, and then display the contents of the editing buffer in the terminal.
To save the result to a new file, you may use a redirection like
printf '4m1n,pn' | ed -s file >newfile
or you may tell ed
to save the file with
printf '4m1nw newfilen' | ed -s file
The command w newfile
would save the edited buffer into the named file. The command w
with no filename would save the file back to the original filename.
Since the line editor ed
reads the file into memory, it is okay to use it for this kind of thing if the file is of reasonable but not too large size (less than gigabytes).
To move the last line, change 4
to $
in the commands above.
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
An alternative, slower, longer version.
#!/bin/sh
if [ ! -f "$1" ]
then
printf "No input filen"
exit
fi
FIRST=`head -n 1 "$1"`
LAST=`tail -n 1 "$1"`
MID=`sed -n '$d; 2,$p' "$1"`
printf "$FIRSTn$LASTn$MIDn"
exit
Slower because using head
, tail
and sed
to access the file each time. On a large file (or many files) this could be very noticeable.
Thesed
pipeline could be shortened intosed -n '$d; 2,$p'
â Kusalananda
Sep 8 at 8:59
@Kusalananda - Thanks, updated. The answers on the linked duplicate are way better. This must be some kind of test question.
â Tigger
Sep 8 at 12:11
add a comment |Â
up vote
2
down vote
accepted
An alternative, slower, longer version.
#!/bin/sh
if [ ! -f "$1" ]
then
printf "No input filen"
exit
fi
FIRST=`head -n 1 "$1"`
LAST=`tail -n 1 "$1"`
MID=`sed -n '$d; 2,$p' "$1"`
printf "$FIRSTn$LASTn$MIDn"
exit
Slower because using head
, tail
and sed
to access the file each time. On a large file (or many files) this could be very noticeable.
Thesed
pipeline could be shortened intosed -n '$d; 2,$p'
â Kusalananda
Sep 8 at 8:59
@Kusalananda - Thanks, updated. The answers on the linked duplicate are way better. This must be some kind of test question.
â Tigger
Sep 8 at 12:11
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
An alternative, slower, longer version.
#!/bin/sh
if [ ! -f "$1" ]
then
printf "No input filen"
exit
fi
FIRST=`head -n 1 "$1"`
LAST=`tail -n 1 "$1"`
MID=`sed -n '$d; 2,$p' "$1"`
printf "$FIRSTn$LASTn$MIDn"
exit
Slower because using head
, tail
and sed
to access the file each time. On a large file (or many files) this could be very noticeable.
An alternative, slower, longer version.
#!/bin/sh
if [ ! -f "$1" ]
then
printf "No input filen"
exit
fi
FIRST=`head -n 1 "$1"`
LAST=`tail -n 1 "$1"`
MID=`sed -n '$d; 2,$p' "$1"`
printf "$FIRSTn$LASTn$MIDn"
exit
Slower because using head
, tail
and sed
to access the file each time. On a large file (or many files) this could be very noticeable.
edited Sep 8 at 12:08
answered Sep 8 at 8:23
Tigger
1,971812
1,971812
Thesed
pipeline could be shortened intosed -n '$d; 2,$p'
â Kusalananda
Sep 8 at 8:59
@Kusalananda - Thanks, updated. The answers on the linked duplicate are way better. This must be some kind of test question.
â Tigger
Sep 8 at 12:11
add a comment |Â
Thesed
pipeline could be shortened intosed -n '$d; 2,$p'
â Kusalananda
Sep 8 at 8:59
@Kusalananda - Thanks, updated. The answers on the linked duplicate are way better. This must be some kind of test question.
â Tigger
Sep 8 at 12:11
The
sed
pipeline could be shortened into sed -n '$d; 2,$p'
â Kusalananda
Sep 8 at 8:59
The
sed
pipeline could be shortened into sed -n '$d; 2,$p'
â Kusalananda
Sep 8 at 8:59
@Kusalananda - Thanks, updated. The answers on the linked duplicate are way better. This must be some kind of test question.
â Tigger
Sep 8 at 12:11
@Kusalananda - Thanks, updated. The answers on the linked duplicate are way better. This must be some kind of test question.
â Tigger
Sep 8 at 12:11
add a comment |Â
up vote
2
down vote
$ printf '4m1n,pn' | ed -s file
line 1
line 4
line 2
line 3
The short ed
script
4m1
,p
will move line four to after line one, and then display the contents of the editing buffer in the terminal.
To save the result to a new file, you may use a redirection like
printf '4m1n,pn' | ed -s file >newfile
or you may tell ed
to save the file with
printf '4m1nw newfilen' | ed -s file
The command w newfile
would save the edited buffer into the named file. The command w
with no filename would save the file back to the original filename.
Since the line editor ed
reads the file into memory, it is okay to use it for this kind of thing if the file is of reasonable but not too large size (less than gigabytes).
To move the last line, change 4
to $
in the commands above.
add a comment |Â
up vote
2
down vote
$ printf '4m1n,pn' | ed -s file
line 1
line 4
line 2
line 3
The short ed
script
4m1
,p
will move line four to after line one, and then display the contents of the editing buffer in the terminal.
To save the result to a new file, you may use a redirection like
printf '4m1n,pn' | ed -s file >newfile
or you may tell ed
to save the file with
printf '4m1nw newfilen' | ed -s file
The command w newfile
would save the edited buffer into the named file. The command w
with no filename would save the file back to the original filename.
Since the line editor ed
reads the file into memory, it is okay to use it for this kind of thing if the file is of reasonable but not too large size (less than gigabytes).
To move the last line, change 4
to $
in the commands above.
add a comment |Â
up vote
2
down vote
up vote
2
down vote
$ printf '4m1n,pn' | ed -s file
line 1
line 4
line 2
line 3
The short ed
script
4m1
,p
will move line four to after line one, and then display the contents of the editing buffer in the terminal.
To save the result to a new file, you may use a redirection like
printf '4m1n,pn' | ed -s file >newfile
or you may tell ed
to save the file with
printf '4m1nw newfilen' | ed -s file
The command w newfile
would save the edited buffer into the named file. The command w
with no filename would save the file back to the original filename.
Since the line editor ed
reads the file into memory, it is okay to use it for this kind of thing if the file is of reasonable but not too large size (less than gigabytes).
To move the last line, change 4
to $
in the commands above.
$ printf '4m1n,pn' | ed -s file
line 1
line 4
line 2
line 3
The short ed
script
4m1
,p
will move line four to after line one, and then display the contents of the editing buffer in the terminal.
To save the result to a new file, you may use a redirection like
printf '4m1n,pn' | ed -s file >newfile
or you may tell ed
to save the file with
printf '4m1nw newfilen' | ed -s file
The command w newfile
would save the edited buffer into the named file. The command w
with no filename would save the file back to the original filename.
Since the line editor ed
reads the file into memory, it is okay to use it for this kind of thing if the file is of reasonable but not too large size (less than gigabytes).
To move the last line, change 4
to $
in the commands above.
edited Sep 8 at 8:07
answered Sep 8 at 8:02
Kusalananda
107k14209329
107k14209329
add a comment |Â
add a comment |Â
Not very effective but it works with GNU sed:
sed '1!d' file; sed '$!d' file; sed -n '2,$$d;p' file
â Cyrus
Sep 8 at 9:54
Bash isnâÂÂt a text editor
â Jeff Schaller
Sep 8 at 9:58