- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- How to reverse a file....
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-18-2004 04:00 PM
11-18-2004 04:00 PM
How to reverse a file....
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-18-2004 04:05 PM
11-18-2004 04:05 PM
Re: How to reverse a file....
Use 'rev' command. It reverses the characters in each line. 'man rev' for more information.
-Sri
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-18-2004 04:11 PM
11-18-2004 04:11 PM
Re: How to reverse a file....
rev your_file |awk '{LINE[NR]=$0}
END {
for (i=NR;i;i--)
print LINE[i]
}'
-Sri
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-18-2004 04:38 PM
11-18-2004 04:38 PM
Re: How to reverse a file....
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-18-2004 04:48 PM
11-18-2004 04:48 PM
Re: How to reverse a file....
-Sri
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-18-2004 06:21 PM
11-18-2004 06:21 PM
Re: How to reverse a file....
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"
@ 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-18-2004 06:59 PM
11-18-2004 06:59 PM
Re: How to reverse a file....
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