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

Richard Bronosky Richard at Bronosky.com
Wed Mar 28 13:06:43 EDT 2012


Ooooo! I like this, Jim. But, there is no need for regexing every line
of the doc twice.
file=$1
last=$(tail -n 1 $file)
# if you do this in a chmoded file you have to double the backslash as
I have in the next line
sed -e '$d' -e "1 i\\
$last
" $file

Here it is in downloadable form: https://gist.github.com/2228256

On Wed, Mar 28, 2012 at 11:54 AM, Jim Kinney <jim.kinney at gmail.com> wrote:
> 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/
>
>
> _______________________________________________
> 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
>



-- 
.!# RichardBronosky #!.



More information about the Ale mailing list