reversing order of lines in a file

how can i reverse the line order in text files?
(but total number of the lines is not constant )

for example i have a file like this:

line1
line2
line3
.
.
lineN

i wantto make it like this:

lineN
.
.
.
line3
line2
line1

U can try something like this>

#!/usr/bin/awk -f

#< AWK - Reverse order of words in each line of a file

{ cnt = NF;
while ( cnt > 0 ) {
printf( "%s ", $cnt );
--cnt;
}
printf( "\n" );
}

There is a GNU utility called tac, part of coreutils, that does what you want.

If you don't have it, and cannot install it:

awk '{x[NR] = $0}
 END { while ( NR > 0 ) print x[NR--] }' [FILENAME ...]
sed '1!G;h;$!d'    file
sed -n '1!G;h;$p' file

from sed - one liners

Try this...

cat -n <filename>| sort -r | cut -f2-20 > reversedfile

Experts please comment if anything wrong with this.... :cool:

Though complex , You can have script presented below does the job!!

#!/bin/ksh
Tot=$(sed -n $= FILE )
i=0;
while [[ $i -le $Tot ]] ; do
tail -n"$i" FILE | head -1
((i=i+1))
done

You can extend the script for multiple files. Experts please comment on the approach

This is horrendously slow, as it calls two external commands for every line in the file. Here are timings for a 181-line file (xx.sh is your script):

 $ time tac .bashrc > /dev/null

real    0m0.021s
user    0m0.002s
sys     0m0.005s
$ time awk '{x[NR]=$0}END{while (NR) print x[NR--]}' .bashrc > /dev/null

real    0m0.010s
user    0m0.003s
sys     0m0.006s
$ time xx.sh .bashrc > /dev/null

real    0m2.060s
user    0m0.483s
sys     0m1.420s

This might be a bit faster:
nl -ba inputfile | sort -nr | cut -f2-

I think you should use "awk" instead of cut.

cat -n <filename> | sort -r | awk '{print $2}' > reversedfile

tail -r <filename>

would reverse entire file.

The -r option to tail is not standard; it is available on *BSD, but not GNU.
$ tail -r .bashrc
tail: invalid option -- r
Try `tail --help' for more information

reverse.pl

#!/usr/bin/perl
print reverse <>;
reverse.pl filename

use command tac.

tac file1 > file1.tmp
mv file1.tmp file1 :slight_smile:

A way to reverse the file and preserve the order. Number the lines, sort, remove the numbers.

cat -n myfile| sort -n -r -k 1 |cut -f2-

How can I reverse lines of the second and the third comlumns?

For example, from file

a 1 11
b 2 12
c 3 13
d 4 14
e 5 15

to like this:
a 5 15
b 4 14
c 3 13
d 2 12
e 1 11

(Sorry my pooring english.)

If you have the tac command:

cut -d ' ' -f1 file > col1
cut -d ' ' -f2- file | tac > col2
paste -d ' ' col1 col2

Thank you very much for the ready answer!

without tac you can do this way...

cat infile | cut -d ' ' -f1 > col1
cat infile | cut -d ' ' -f2 | sort -r > col2
cat infile | cut -d ' ' -f3 | sort -r > col3
paste -d ' ' col1 col2 col3

Another one:

awk 'NR==FNR{a[++i]=$2 FS $3;next}{print $1,a[i--]}' file file

Thanks! What if there are empty columns ahead of table?

For example:

          a 1 11
          b 2 12
          c 3 13
          d 4 14
          e 5 15

instead of

   a 1 11
   b 2 12
   c 3 13
   d 4 14
   e 5 15

-----Post Update-----
I found solution

#!/bin/sh
cut -d ' ' -f 1-8 file > col1
cut -d ' ' -f9- file | tac > col2
paste -d ' ' col1 col2