[ale] Move last line of a file to first line

Lightner, Jeff JLightner at water.com
Wed Mar 28 12:02:13 EDT 2012


I wasn't aware of the tac command.  It may be all I need as I don't think the order of the other records is important in which case this should work:

tac originalfile >newfile

Thanks.





-----Original Message-----
From: ale-bounces at ale.org [mailto:ale-bounces at ale.org] On Behalf Of mike at trausch.us
Sent: Wednesday, March 28, 2012 11:28 AM
To: ale at 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.
----------------------------------




More information about the Ale mailing list