We have variable length text files being received in which the “header” line we use in automated processing is the last line of the file rather than the first line.
What is the best way to move that last line of a text file to be the first line?
Please answer the question as asked and don’t suggest making the file be in the correct order when we receive it or changing the automated processing to read the last line first. Assume those aren’t options.
What I came up with was to do tail -1 >newfile and head -<number of lines – 1> but was thinking there ought to be a better way.
I see sed can deal with last line, first line and insertion but the examples I’m finding address either/or not moving last line to first line.
Athena®, Created for the Cause™
Making a Difference in the Fight Against Breast Cancer
How and Why I Should Support Bottled Water!
Do not relinquish your right to choose bottled water as a healthy alternative to beverages that contain sugar, calories, etc. Your support of bottled water will make a difference! Your signatures count! Go to
http://www.bottledwatermatters.org/luv-bottledwater-iframe/dswaters and sign a petition to support your right to always choose bottled water. Help fight federal and state issues,
such as bottle deposits (or taxes) and organizations that want to ban the sale of bottled water. Support community curbside recycling programs. Support bottled water as a healthy way to maintain proper hydration. Our goal is 50,000 signatures. Share this petition
with your friends and family today!
---------------------------------
CONFIDENTIALITY NOTICE: This e-mail may contain privileged or confidential information and is for the sole use of the intended recipient(s). If you are not the intended recipient, any disclosure, copying, distribution, or use of the contents of this information
is prohibited and may be unlawful. If you have received this electronic transmission in error, please reply immediately to the sender that you have received the message in error, and delete it. Thank you.
----------------------------------
I found this works:
awk '{a[NR]=$0} END {print a[NR]; for (i=1;i<NR;i++) print a[i]}' originalfile >newfile
Where you replace originalfile and newfile with real file names.
"Best" is subjective...
> Please answer the question as asked and don’t suggest making the file be
> in the correct order when we receive it or changing the automated
> processing to read the last line first. Assume those aren’t options.
k...
> What I came up with was to do tail -1 >newfile and head -<number of
> lines – 1> but was thinking there ought to be a better way.
I would do something similar:
#!/bin/bash
# Put the last line of the file specified into the
# first line of the file specified.
TMP_FILE="$1".$$
tac "$1"|head -n 1 > "${TMP_FILE}"
tac "$1"|tail -n +2|tac >> "${TMP_FILE}"
mv "${TMP_FILE}" "$1"
--- Mike
--
A man who reasons deliberately, manages it better after studying Logic
than he could before, if he is sincere about it and has common sense.
--- Carveth Read, “Logic”
_______________________________________________
Ale mailing list
A...@ale.org
http://mail.ale.org/mailman/listinfo/ale
See JOBS, ANNOUNCE and SCHOOLS lists at
http://mail.ale.org/mailman/listinfo
tac originalfile >newfile
Thanks.
-----Original Message-----
From: ale-b...@ale.org [mailto:ale-b...@ale.org] On Behalf Of mi...@trausch.us
Sent: Wednesday, March 28, 2012 11:28 AM
To: a...@ale.org
Subject: Re: [ale] Move last line of a file to first line
On 03/28/2012 11:10 AM, Lightner, Jeff wrote:
> What is the best way to move that last line of a text file to be the
> first line?
"Best" is subjective...
> Please answer the question as asked and don't suggest making the file be
> in the correct order when we receive it or changing the automated
> processing to read the last line first. Assume those aren't options.
k...
> What I came up with was to do tail -1 >newfile and head -<number of
> lines - 1> but was thinking there ought to be a better way.
I would do something similar:
#!/bin/bash
# Put the last line of the file specified into the
# first line of the file specified.
TMP_FILE="$1".$$
tac "$1"|head -n 1 > "${TMP_FILE}"
tac "$1"|tail -n +2|tac >> "${TMP_FILE}"
mv "${TMP_FILE}" "$1"
--- Mike
--
A man who reasons deliberately, manages it better after studying Logic
than he could before, if he is sincere about it and has common sense.
--- Carveth Read, "Logic"
Athena(r), Created for the Cause(tm)
Making a Difference in the Fight Against Breast Cancer
---------------------------------
CONFIDENTIALITY NOTICE: This e-mail may contain privileged or confidential information and is for the sole use of the intended recipient(s). If you are not the intended recipient, any disclosure, copying, distribution, or use of the contents of this information is prohibited and may be unlawful. If you have received this electronic transmission in error, please reply immediately to the sender that you have received the message in error, and delete it. Thank you.
----------------------------------
I would do this in 2 passes.
# First get the last line into a new file. (assuming the file in
question is passed in as the first argument)
tail -n1 $1 > $1.mod
# Second add the rest of the file, less the last line.
sed '$d' $1 >> $1.mod
This is working with streams. It requires an additional $filesize of
disk space, but it does not require $filesize of RAM.
If you want to be really efficient, use ed. ;-)
# This will trim the last line of the file in place, but I'm not
taking the time to figure out how to add it to the beginning.
ed "$1" << EOF
$
d
w
EOF
Here it is in downloadable form: https://gist.github.com/2228256
--
.!# RichardBronosky #!.
--
.!# RichardBronosky #!.
I used to used ed all the time when I first starting using UNIX. You
can incorporate Ed's ed commands in a here document, stick it in a
script and you have all you need:
cat <<END|ed data
m0
w
q
END
--
Until later, Geoffrey
"I predict future happiness for America if they can prevent
the government from wasting the labors of the people under
the pretense of taking care of them."
- Thomas Jefferson