File Manipulation Examples Using Tac, Rev, Paste, and Join Unix Commands

by Ramesh Natarajan on October 19, 2009

Linux tac, rev, paste, and join commandIn this article, let us review how to use Unix tac command, rev command, paste command, and join command with practical examples.

1. tac command – Print file in reverse (last line first)

The word tac is reverse of the word cat. The tac command functionality is also reverse of the cat command. cat command prints the file. tac command prints the file in reverse order with the last line first.

$ cat thegeekstuff.txt
1. Linux Sysadmin, Scripting etc.,
2. Databases Oracle, mySQL etc.,
3. Hardware
4. Security (Firewall, Network, Online Security etc)
5. Storage
6. Cool gadgets and websites
7. Productivity (Too many technologies to explore, not much time available)
8. Website Design
9. Software Development
10. Windows Sysadmin, reboot etc.,
11. Adding 1's and 0's

$ tac thegeekstuff.txt
11. Adding 1's and 0's
10. Windows Sysadmin, reboot etc.,
9. Software Development
8. Website Design
7. Productivity (Too many technologies to explore, not much time available)
6. Cool gadgets and websites
5. Storage
4. Security (Firewall, Network, Online Security etc)
3. Hardware
2. Databases Oracle, mySQL etc.,
1. Linux Sysadmin, Scripting etc.,

2. rev command – Reverse the order of characters in every line

Reverse the order of characters in every line as shown in the example below. It is different from tac command, as rev command reverses each character of the line, whereas tac command reverses each line of the file.

$ rev thegeekstuff.txt
,.cte gnitpircS ,nimdasyS xuniL .1
,.cte LQSym ,elcarO sesabataD .2
erawdraH .3
)cte ytiruceS enilnO ,krowteN ,llaweriF( ytiruceS .4
egarotS .5
setisbew dna stegdag looC .6
)elbaliava emit hcum ton ,erolpxe ot seigolonhcet ynam ooT( ytivitcudorP .7
ngiseD etisbeW .8
tnempoleveD erawtfoS .9
,.cte toober ,nimdasyS swodniW .01
s'0 dna s'1 gniddA .11

3. paste command – Merge file lines

Paste the line1 of file1, file2, .. fileN into the line1 of the output. It will repeat the same for all lines. Each file’s line will be delimited by tab.

Paste output:

$ paste f1 f2 f3
f1-line1<tab>f2-line1<tab>f3-line1
f1-line2<tab>f2-line2<tab>f3-line2
f1-line3<tab>f2-line3<tab>f3-line3
...

In the following example, corresponding lines from three different files are combined and shown appropriately.

$ cat emp-number.txt
100
200
300
400
500

$ cat emp-firstname.txt
Emma
Alex
Madison
Sanjay
Nisha

$ cat emp-lastname.txt
Thomas
Jason
Randy
Gupta
Singh

$ paste emp-number.txt emp-firstname.txt emp-lastname.txt
100     Emma    Thomas
200     Alex    Jason
300     Madison Randy
400     Sanjay  Gupta
500     Nisha   Singh

4. join – Join lines of two files based on a common field

You can join two files based on a common field, that you can specify using field.

Syntax:
$ join -t':' -1 N -2 N file1 file2
  • -t’:’ – : is the field separator
  • -1 N : Nth field in 1st file
  • -2 N : Nth field in 2nd file
  • file1 file2 : files that should be joined

In this example, let us combine employee.txt and bonus.txt files using the common employee number field.

$ cat employee.txt
100     Emma    Thomas
200     Alex    Jason
300     Madison Randy
400     Sanjay  Gupta
500     Nisha   Singh

$ cat bonus.txt
$5,000  100
$5,500  200
$6,000  300
$7,000  400
$9,500  500

$ join  -1 1 -2 2 employee.txt bonus.txt
100 Emma Thomas $5,000
200 Alex Jason $5,500
300 Madison Randy $6,000
400 Sanjay Gupta $7,000
500 Nisha Singh $9,500
Download Free eBook - Linux 101 Hacks

Get free Unix tutorials, tips and tricks straight to your email in-box.

If you enjoyed this article, you might also like..

  1. How To Manage Packages Using apt-get, apt-cache, apt-file and dpkg Commands ( With 13 Practical Examples )
  2. Unix Sed Tutorial: Multi-Line File Operation with 6 Practical Examples
  3. Awk Introduction Tutorial – 7 Awk Print Examples
  4. The Power of Z Commands – Zcat, Zless, Zgrep, Zdiff Examples
  5. Unix LS Command: 15 Practical Examples
  

Vim 101 Hacks Book

{ 1 trackback }

Danaville » Blog Archive » unix
October 19, 2009 at 11:07 am

{ 4 comments… read them below or add one }

1 louic October 19, 2009 at 3:53 am

I did not know about the paste command, but it will be very useful for me. Thanks again, for the clear and to-the-point examples!

2 Mauricio October 19, 2009 at 6:58 am

Excelente página

Translation: Spanish » English

Excellent page

3 JJ October 20, 2009 at 4:33 am

tac command is a new one for me … Thanks!!

4 litd March 15, 2010 at 10:15 am

Hi,

I have a question about join.

how could the output file use tab instead of space as separator in the above sample?

Thanks!

Leave a Comment

Previous post:

Next post: