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

Jim Kinney jim.kinney at gmail.com
Wed Mar 28 11:54:00 EDT 2012


last=$(tail -n 1 file)
first=$(head -n 1 file)
sed -i -e "/$last/d" -e "/$first/ i\
$last
" file

On Wed, Mar 28, 2012 at 11:27 AM, mike at trausch.us <mike at trausch.us> wrote:

> 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”
>
>
> _______________________________________________
> Ale mailing list
> Ale at ale.org
> http://mail.ale.org/mailman/listinfo/ale
> See JOBS, ANNOUNCE and SCHOOLS lists at
> http://mail.ale.org/mailman/listinfo
>
>


-- 
-- 
James P. Kinney III

As long as the general population is passive, apathetic, diverted to
consumerism or hatred of the vulnerable, then the powerful can do as they
please, and those who survive will be left to contemplate the outcome.
- *2011 Noam Chomsky

http://heretothereideas.blogspot.com/
*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.ale.org/pipermail/ale/attachments/20120328/32e6eb32/attachment.html 


More information about the Ale mailing list