Operating System - HP-UX
1855296 Members
2601 Online
104109 Solutions
New Discussion

Re: How to reverse a File

 
SOLVED
Go to solution

How to reverse a File

hey Admins,

How can i display a file upside down.. Ie the last line fist and fist line last...

any command for this?

Thanks,
George
When one door closes, God opens anather one. But we stare at the closed one so long that we miss the open door
14 REPLIES 14
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: How to reverse a File

There are number of ways to do this (awk, Perl, ...) but here's a Perl method:

cat yourfile | perl -e '@x = ; while ($y = pop @x) {print $y;}' > newfile

Regards, Clay
If it ain't broke, I can fix that.
H.Merijn Brand (procura
Honored Contributor

Re: How to reverse a File

shorter:

# perl -e'print reverse<>' file

There is a faster way. From GNU file utils, there is a command called 'tac' (reverse of cat)

http://hpux.connect.org.uk/hppd/hpux/Gnu/coreutils-5.0.91/

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
f. halili
Trusted Contributor

Re: How to reverse a File

HERE's my sample file:

# cat testfile
aaa
bbb
ccc
ddd

HERES' the command I use:

# cat testfile | sed '1!G;h;$!d'
ddd
ccc
bbb
aaa

- FHALILI
derekh
Laurent Menase
Honored Contributor

Re: How to reverse a File

Hi Georges,

awk ' BEGIN { i=0 }
{ printf "%d %s\n",i,$0 }'
Happy new year
Laurent
john korterman
Honored Contributor

Re: How to reverse a File

Hi,
with smaller files you can use an ed command in a script. This works directly in the file itself, e.g.:

ed $1 <g/^/m0
w
EOF

try running the above with you file as $1.

regards,
John K.

it would be nice if you always got a second chance
Chris Watkins_1
Respected Contributor

Re: How to reverse a File

Thanks to the above posters, for new ideas.
I would have probably done it this way:


[start script code]
#!/bin/sh

echo "Enter the filename to reverse --> \c"
read filename
echo "Enter the desired new filename --> \c"
read newfilename

lines=`cat ${filename} |wc -l`
while [ ${lines} -gt 0 ]
do
echo `head -${lines} ${filename} |tail -n 1` >>${newfilename}
(( lines=${lines} - 1 ))
done
echo Finished!

[end script code]


So many more elegant (and short!) ways to do this.
Note to self... must learn perl.
Not without 2 backups and an Ignite image!

Re: How to reverse a File

Thanks to Everyone...

How many ways to do a single thing... Thank god for the ITRC..

Happy new year to all

When one door closes, God opens anather one. But we stare at the closed one so long that we miss the open door
KapilRaj
Honored Contributor

Re: How to reverse a File

There are two more ways ....

1 : cat filename |awk '{ s[NR] = $0} END { for ( i = NR ; i >= 1 ; i-- ) {print s[i] } }'

2: KEEP THE MONITOR (Display Unit) REVERSE !!

Happy New Year

Kaps
Nothing is impossible
John Poff
Honored Contributor

Re: How to reverse a File

Hi,

Those are all fine software methods for reversing the file. You can also do it using hardware.

1. Turn your monitor upside down.

2. cat filename

;)

JP
Alex Ostapenko
Advisor

Re: How to reverse a File

...and another method using "pr" command:

cat testfile | pr -t -n | sort -nrk1,1 | cut -f2-

- where pr "-t" suppresses page title, "-n" adds line numbers
- where sort "-k1,1" specifies first field for sorting, "-n" specifies numeric sort, and "-r" specifies in reverse
- where cut "-f2-" means cut 2nd field and on

=:-) Alex
Elmar P. Kolkman
Honored Contributor

Re: How to reverse a File

I just thought of the same solution:
grep -n '^.' | sort -rn | cut -d: -f2-

The same way, but using some different commands...

As for the up-side-down monitor: you'll have to alter your character set to up-side-down characters and do a rev instead of cat, otherwise the text is a bit hard to read... (rev prints the line right-to-left instead of left-to-right, for the ones who don't know the command)... ;-)
Every problem has at least one solution. Only some solutions are harder to find.
H.Merijn Brand (procura
Honored Contributor

Re: How to reverse a File

Elmar, that won't work :)

grepping for ^. will skip the empty lines in a file.

Enjoy, Have FUN! H.Merijn [ Who wonders about the point assignment in this thread. I just cannot grasp what the value diff per post is for the reader ... ]
Enjoy, Have FUN! H.Merijn
Elmar P. Kolkman
Honored Contributor

Re: How to reverse a File

Ok. You're right. I looked further and found a command I forgot... nl will do the trick. And it is even shorter...

nl | sort -rn | cut -f2-

Happy new year and the best wishes for 2004 (and beyond, of course).
Every problem has at least one solution. Only some solutions are harder to find.
Graham Cameron_1
Honored Contributor

Re: How to reverse a File

Elmar

You need to add the "-ba" switches to nl, else again it loses blanks.


George

This came up a week earier, most of the solutions are similar to above.
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=9248

-- Graham
Computers make it easier to do a lot of things, but most of the things they make it easier to do don't need to be done.