Wednesday, August 15, 2007

display word or text file in reversed pattern

Here's a quick way of display text based files in reversed style line per line or character per character, using tac and rev linux commands. As you can notice, tac is the reversed spelling of cat. Tac basically functions the same like `cat` linux command on displaying text files but it is done on reverse pattern or order.

Assumming we have a file named file.txt. To display file.txt contents using cat, we usually do it like so:

# cat file.txt
~~~~~~~~~~~
1
2
3
4
5
~~~~~~~~~~~

Using tac linux command with the intention of reversing the contetnts fo the file in reversed line per line basis, we would have the reversed display of the file like so:

# tac file.txt
~~~~~~~~~~~
5
4
3
2
1
~~~~~~~~~~~

stdout redirection can also be applied with this command to redirect output to another text file like so:

# tac file.txt > reversedfile.txt



Noticed that the reversal was done on line per line basis and not by character per character.


If you wish to reverse a file or word character per character, this is how to do that.

# echo "12345" | rev

displays
~~~~~~~~~~~~~~~~~
54321
~~~~~~~~~~~~~~~~~


Hence, applying rev linux command to linux text files would show us the below results

# cat file.txt

~~~~~~~~~~~~~~~
12345
abcde
~~~~~~~~~~~~~~~

Doing the reversal on character per character basis would now be

# cat file.txt | rev

~~~~~~~~~~~~~~~
54321
edcba
~~~~~~~~~~~~~~~

which can be redirected to file like so:

# cat file.txt | rev > reversedcharacter.txt


Done!

0 comments:

Sign up for PayPal and start accepting credit card payments instantly.
ILoveTux - howtos and news | About | Contact | TOS | Policy