≡ Menu

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

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
Add your comment

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

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

Comments on this entry are closed.

  • louic October 19, 2009, 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!

  • Mauricio October 19, 2009, 6:58 am

    Excelente página

    Translation: Spanish » English

    Excellent page

  • JJ October 20, 2009, 4:33 am

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

  • litd March 15, 2010, 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!

  • Jim December 12, 2010, 12:27 am

    to use tabs as delimiters instead of commas change the -t parameter.

    if you want to change the commas to tabs then use tr

  • litd December 14, 2010, 1:28 pm

    Hi Jim,

    Thanks for the tip. However, would you please use the sample above to show the result file using tr?

    Thanks!

  • argv January 7, 2011, 10:14 pm

    isn’t it possible to emulate the basic function of paste with the shell, using nested for loops and printf?

    is paste another one of unix’s many redundant utilities?

    is paste(1) redundant? (at least partially)
    here’s an example of what i mean:
    http://pastebin.com/4grdppt9

  • Mark D. Blackwell January 1, 2012, 12:36 pm

    Using tabs as the separator with Gnu join (even though they look better) failed (at least on cygwin). I’m using:
    $ join –version
    join (GNU coreutils) 8.14
    Packaged by Cygwin (8.14-1)

    I got this error:
    $ join -t “\t” -1 1 -2 2 temp emp-bonus
    join: multi-character tab `\\t’.

    Colons also are a common field separator in Unix. These commands work well:
    $ paste -d : emp-number emp-firstname emp-lastname > temp
    $ join -t : -1 1 -2 2 temp emp-bonus

    $ cat emp-firstname emp-number emp-lastname emp-bonus
    Emma
    Alex
    Madisonny
    Sanjay
    Nisha
    100
    200
    300
    400
    500
    Thomas
    Jason
    Randy
    Gupta
    Singh
    $5,000:100
    $5,500:200
    $6,000:300
    $7,000:400
    $9,500:500

    $ paste -d : emp-number emp-firstname emp-lastname > temp
    $ cat temp
    100:Emma:Thomas
    200:Alex:Jason
    300:Madisonny:Randy
    400:Sanjay:Gupta
    500:Nisha:Singh

    $ join -t : -1 1 -2 2 temp emp-bonus
    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

    To tab-space the output (though long fields look bad):
    $ join -t : -1 1 -2 2 temp emp-bonus | tr : “\t”
    100 Emma Thomas $5,000
    200 Alex Jason $5,500
    300 Madisonny Randy $6,000
    400 Sanjay Gupta $7,000
    500 Nisha Singh $9,500

  • Ravi January 22, 2013, 2:47 am

    Its really very useful commands, i was unaware of these commands, Thanks a lot.

  • Anonymous November 5, 2013, 3:28 am

    It is very useful. thanks

  • Swetha. November 5, 2013, 3:29 am

    Its really very useful commands Thanks

  • Colum Paget January 12, 2014, 3:45 pm

    I thought I knew most of the major unix shell commands (been using it for 20 years) but these are new to me! Great post. Thanks.

  • Colum Paget January 12, 2014, 4:01 pm

    And I’ve used ‘rev’ already! Thanks again!

    Colum

  • Vignesh January 5, 2015, 6:49 am

    i tyried using REV command however i encountered with the error stating “ksh: rev: not found”

    kindly guide me on wat changes do i need to do for this.