Operating System - HP-UX
1830214 Members
1421 Online
109999 Solutions
New Discussion

How to reverse a file....

 
Kenneth Jensen_1
New Member

How to reverse a file....

Hi, i'm very new to all this....

i need to reverse a file... not by lines....
by letters...

so that:
Line1
Line2
Line3
Becomes:
3eniL
2eniL
1eniL

It's big files that i want to reverse...

Can anyone help me with this....
In a simple manor...

best regards Kenneth
6 REPLIES 6
Sridhar Bhaskarla
Honored Contributor

Re: How to reverse a file....

Hi Kenneth,

Use 'rev' command. It reverses the characters in each line. 'man rev' for more information.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Sridhar Bhaskarla
Honored Contributor

Re: How to reverse a file....

You said 'not by lines' but your examples does reverse the lines too.. In that case use this small awk script

rev your_file |awk '{LINE[NR]=$0}
END {
for (i=NR;i;i--)
print LINE[i]
}'

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Hein van den Heuvel
Honored Contributor

Re: How to reverse a file....

If it is a modest size file (up to 500MB or so?) then the array approach should work well in either AWK or PERL.

For really large files (Gigabytes) that would fail, and you need to go over the disk.
I would pre-pend the lines with a line number, reverse sort, and cut off the number.
Something like:

perl -ne 'printf ("%06d %s",$.,$_)' file | sort -r | cut -c8- | rev

If you need to do this a lot, then i'd suggest a (C or perl) program that LSEEKs from the end of the file to the beginning in 'binmode'. Allign at a 'nice' buffer size. 16kb? 64kb? 128kb? Then walk the input buffer reversing lines into an output buffer and append to a fresh output. You'll just have to deal with records crossing the buffers, and with the incomplete last block in the input file (the first one read).
SMOP!

What problem are you really trying to solve?
I'm curious! This sounds like an 'odd' request!
There may be better ways to solve the 'real' problem.


hth,
Hein.
Sridhar Bhaskarla
Honored Contributor

Re: How to reverse a file....

I guess I need to sleep.. I didn't see your 'big files' clause either.. Since my awk array reads the lines into memory, you can probably try 'splitting' files into multiple chunks and apply the reverse logic on each chunk..

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Stefan Schulz
Honored Contributor

Re: How to reverse a file....

Hi Kenneth,

here is a low level approach to this. Read the file in reverse order to reverse the lines. Pipe every line through rev to reverse the line itselfe bevor writing it to a new line.

I have something similar in a C-Shell script. Here is the interesting part. You might want to adapt it to a ksh script:

set n = `cat $liste | wc -l`
while ($n >= 1 )
sed -n -e "$n P" | rev >>
@ n--
end


As i said, this is a low level approach. Not very cool and not really fast, but it should work.

Hope this helps

Regards Stefan
No Mouse found. System halted. Press Mousebutton to continue.
H.Merijn Brand (procura
Honored Contributor

Re: How to reverse a file....

# perl -e'print reverse<>' file | rev

But that will fail for BIG files, since it reads the complete file in memory.
For big files, perl has a module:
ftp://download.xs4all.nl/pub/mirror/CPAN/modules/by-module/File/File-ReadBackwards-1.02.tar.gz

But - as usual - 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.2.1/

# tac file | rev

--
lt09:/home/merijn 101 > cat file
Line 1

Line 3
e 4

Line 6
lt09:/home/merijn 102 > tac file
Line 6

e 4
Line 3

Line 1
lt09:/home/merijn 103 > rev file
1 eniL

3 eniL
4 e

6 eniL
lt09:/home/merijn 104 > tac file | rev
6 eniL

4 e
3 eniL

1 eniL
lt09:/home/merijn 105 >
--

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn