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

mike at trausch.us mike at trausch.us
Wed Mar 28 11:27:50 EDT 2012


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”

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 729 bytes
Desc: OpenPGP digital signature
Url : http://mail.ale.org/pipermail/ale/attachments/20120328/2346663a/attachment-0001.bin 


More information about the Ale mailing list