Tuesday, July 13, 2010

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

File Manipulation Examples Using Tac, Rev, Paste, and Join Unix Commands
by Ramesh Natarajan on October 19, 2009ShareThis

In 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 .113. 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-line1f2-line1f3-line1
f1-line2f2-line2f3-line2
f1-line3f2-line3f3-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 Singh4. 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

No comments: