The tail command can be used to reverse the contents of a file. Here's an example.
# cat > filename
THIS IS AN EASY WAY TO REVERSE THE CONTENT OF A FILE.
THE REVERSAL WILL DO MORE THAN 10 LINES. IT WILL REVERSE THE ENTIRE FILE.
EOM
.
# tail -r filename
.
EOM
THE REVERSAL WILL DO MORE THAN 10 LINES. IT WILL REVERSE THE ENTIRE FILE.
THIS IS AN EASY WAY TO REVERSE THE CONTENT OF A FILE.
By the way, this command line entry will do the same thing as tail -r.
# perl -e 'print reverse <>' filename
This blog covers Unix system administration HOWTO tips for using inline for loops, find command, Unix scripting, configuration, SQL, various Unix-based tools, and command line interface syntax. The Unix OS supports tasks such as running hardware, device drivers, peripherals and third party applications. Share tips/comments. Read the comments. But most importantly: Read Disclaimer - Read Disclaimer.
5 comments:
You can also use 'tac' to perform the same action
tac is more flexible, but it isn't universally available like "tail" is.
Solaris doesn't have tac unless you install the coreutils package.
I had a need to write one script that worked on both linux and AIX. All of our linux installs have tac which is nice, but AIX did not. In AIX at least, tail -r is limited to 20K. I ended up using the following mash of commands.
cat -n filename|sort -rn|cut -c8-
Ugly but workable.
hey thanks for that :-D the "cat -n" solution in the comments is cool too
Yes, following observations...
1) tac is not universally available. FreeBSD and AIX not supported tac
2) tail should work in any situation.. not sure about Elijah's comment
3) perl solution written in the blog will not work. It will print whole file in reverse order and not print from last line to first line.
4) cat -n will not print lines from last to first.. it will just add the line number against respective line..
Post a Comment