diff,cut,head,tail,more,less,paste,join,uniq,comm,tr,sort,

The diff command

mohammedrafi@NOC-RAFI:~$ vim file1
I go for shopping on Saturday
I rest completely on Sunday
I use Facebook & Tweeter for social networking

mohammedrafi@NOC-RAFI:~$ vim file2
Today is Monday.
I go for shopping on Saturday
I rest completely on Sunday
I use Facebook & Tweeter for social networking

mohammedrafi@NOC-RAFI:~$ diff file1 file2
0a1
> Today is Monday.

Output:
0a1
> Today is Monday
In the output, 0a1 tells us that line number 1 is added in file2 .

mohammedrafi@NOC-RAFI:~$ vim file1
Today is Monday
I go for shopping on Saturday
I rest completely on Sunday
I use Facebook & Tweeter for social networking

mohammedrafi@NOC-RAFI:~$ vim file2
Today is Monday
I go for shopping on Saturday
I rest completely on Sunday

mohammedrafi@NOC-RAFI:~$ diff file1 file2
4d3
< I use Facebook & Tweeter for social networking

The output is as follows:
4d3
< I use Facebook & Tweeter for social networking.
In the output, 4d3 tells us that line number 4 is deleted in file2 . Similarly, the change command will show us changes in file as well.

##############################################################

The cut command is used to extract specified columns/characters of a text, which is given as follows:
•-c :Will specify the filtering of characters
•-d :Will specify the delimiter for fields
•-f :Will specify the field number

mohammedrafi@NOC-RAFI:~$ cut -d: -f1,3 /etc/passwd |head
root:0
daemon:1
bin:2
sys:3
sync:4
games:5
man:6
lp:7
mail:8
news:9

mohammedrafi@NOC-RAFI:~$ cut -c1-10 /etc/passwd |head
root:x:0:0
daemon:x:1
bin:x:2:2:
sys:x:3:3:
sync:x:4:6
games:x:5:
man:x:6:12
lp:x:7:7:l
mail:x:8:8
news:x:9:9

mohammedrafi@NOC-RAFI:~$ head -5 /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync

mohammedrafi@NOC-RAFI:~$ sudo head -5 /etc/shadow
root:!:17185:0:99999:7:::
daemon:*:16848:0:99999:7:::
bin:*:16848:0:99999:7:::
sys:*:16848:0:99999:7:::
sync:*:16848:0:99999:7:::

mohammedrafi@NOC-RAFI:~$ sudo paste /etc/passwd /etc/shadow |head -5

root:!:17185:0:99999:7:::daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin daemon:*:16848:0:99999:7:::bin:x:2:2:bin:/bin:/usr/sbin/nologin bin:*:16848:0:99999:7:::sys:x:3:3:sys:/dev:/usr/sbin/nologin sys:*:16848:0:99999:7:::sync:x:4:65534:sync:/bin:/bin/sync sync:*:16848:0:99999:7:::

#################################################################

mohammedrafi@NOC-RAFI:~$ seq 100 >sample.txt

mohammedrafi@NOC-RAFI:~$ head sample.txt
1
2
3
4
5
6
7
8
9
10

mohammedrafi@NOC-RAFI:~$ tail sample.txt
91
92
93
94
95
96
97
98
99
100

mohammedrafi@NOC-RAFI:~$ head -3 sample.txt
1
2
3

mohammedrafi@NOC-RAFI:~$ tail -5 sample.txt
96
97
98
99
100

mohammedrafi@NOC-RAFI:~$ head -65 sample.txt |tail -10
56
57
58
59
60
61
62
63
64
65

#######################################################

mohammedrafi@NOC-RAFI:~$ cat one
India
UK
Canada
US
Ireland

mohammedrafi@NOC-RAFI:~$ cat two
New Delhi
London
Toronto
Washington
Dublin

mohammedrafi@NOC-RAFI:~$ cat -n one >one.txt

mohammedrafi@NOC-RAFI:~$ cat -n two >two.txt

mohammedrafi@NOC-RAFI:~$ join one.txt two.txt
1 India New Delhi
2 UK London
3 Canada Toronto
4 US Washington
5 Ireland Dublin

In this case, for both the files, the common fields are the fields which have serial numbers that are the same in both files. We can combine both files by following command:

################################################################

mohammedrafi@NOC-RAFI:~$ cat >> uniq_test
1
2
2
4
4
8
9
10
10
14
14
mohammedrafi@NOC-RAFI:~$ uniq uniq_test
1
2
4
8
9
10
14
mohammedrafi@NOC-RAFI:~$ uniq -d uniq_test
2
4
10
14

##################################################

mohammedrafi@NOC-RAFI:~$ cat >> test1
Barack Obama
David Cameron
Narendra Modi

mohammedrafi@NOC-RAFI:~$ cat >> test2
Barack Obama
Engela Merkel
Vladimir Putin

mohammedrafi@NOC-RAFI:~$ comm test1 test2
Barack Obama
David Cameron
Engela Merkel
Narendra Modi
Vladimir Putin

##################################################################

tr commnad

mohammedrafi@NOC-RAFI:~$ vim tr_test
The tr command is a Linux utility for text processing such as translating, deleting, or squeezing repeated characters, which is shown as follows

This will translate the lower case characters to upper case:
mohammedrafi@NOC-RAFI:~$ tr ‘[a-z]’ ‘[A-Z]’ <tr_test
THE TR COMMAND IS A LINUX UTILITY FOR TEXT PROCESSING SUCH AS TRANSLATING, DELETING, OR SQUEEZING REPEATED CHARACTERS, WHICH IS SHOWN AS FOLLOWS

#######################################################################

Sort: It sorts the contents of a text file, line by line.
•-n : Will sort as per the numeric value
•-d : Will sort as per the dictionary meaning
•-r : Will sort in the reverse order
•-t : Option to specify delimiter for fields
•+num : Specifies sorting field numbers
•-knum : Specifies sorting filed numbers
•$ sort +4 sample.txt : This will sort according to the 4th field

sort –k4 sample.txt : This will sort according to the 4th field

 

Advertisements

One thought on “diff,cut,head,tail,more,less,paste,join,uniq,comm,tr,sort,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s