| Command | Description |
|---|---|
| paste | To combine the content of file files line by line and printing the files content horizontally |
| wc | Displays the no of lines words and characters of any data |
| head | Displays the lines from top to bottom as per option given |
| tail | Displays the lines from bottom to top as per option given |
| cut | Used to retrieves specific characters or fields from data or file |
| tr | Used on out put of some other command to translate characters |
| sort | Used to sort data, by default sorting happens as per the English disctionary |
| grep | Used to pattern search in the given file |
| uniq | Removes adjacent repeated lines in given data of file |
| cmp | Used to compare two files, Its pinpoints at only first diffrence |
| comm | Used to compare two files but can be apply only on sorted files |
paste command
Used to combine 2 or more files content line by line and display the files content horizontally
Syntax
wc command
Used to display the no of lines,words and characters in any file or in any data.
syntax
head command
Used to display the set of lines from start in given file or data.
Syntax
tail command
Used to display the set of lines from end of given file or data.
Syntax
Examples on head and tail commands
consider file1.txt having 15 lines as showing below
To print first 10 lines from top
Note : If count is not specified in command ,then by default it will take first 10 lines
To print the first 7 lines
To print 10 lines from bottom
Note : If count is not specified in command ,then by default tail command will
take 10 lines from bottom
To print the 7 lines from bottom
To print the lines from line no 3 to 8
cut command :
Used to retreive specific fields or characters from a file or data, it is used on output
of some other command
Syntax
Examples
Consider a file name file3.txt having content as following
To retrieve the 7th character
To retrieve a character set from 5th to 9th character
To retrieve 5th and 9th character
We can pipe cut command on any command like date ie:
To cut the 4th field delimated by spaces
To cut the 4th to 6th field delimated by space
To cut the 4th and 6th field delimated by space
Note :
-c : for character number
-d : for delimiter (either space,comma,colon etc)
-f : for field number
tr (translate) command
Used to translate characters, by pipe on some other command
Syntax
Examples
To squeezing character or remove the extra spaces between words
type tr -s " " on output of command ie :
The output of command 'who am i' having extra space as
The following command will squeeze the words or remove the extra spaces
Translating the character without squeezing
Squeezing and then translation
tr Command examples:
Consider file2.txt having the below content
To remove the extra spaces or squeezing the space character
Squeezing space and translating the space to '$' character
Translating the spaces without squeezing
To convert the lower case into upper case letter
Translating all character 'H' or 'h' to '#'
sort command
Used to sort the data by default it sorts as per the english disctionary
It used on output of some other command or file content
Syntax
Examples
To sort as per the english disctionary
To sort in reverse order
To apply the numerical sort
To remove all duplicate recards in file or data
To merge the content of two or more file in sorted manner
Note:
If using -m option with sort command, file1.txt file2.txt should be in sorted order.
If the supplied arguments files(file1.txt, file2.txt) are not in sorted order then we should
use only 'sort' instead of 'sort –m'.
Consider the following data in student.txt, in which the first column is student id ,
2nd column is marks and 3rd is subject.
Sorting based on the second column, delimated by colon
Sorting based on the 3rd column, delimated by colon
To store the final merged data into onother one file
grep command
Used to search for PATTERN in each FILE or standard input.
Syntax
Examples
To search the pattern 'Linux' in file1.txt
To search the pattern 'Linux' in multiple files say file1.txt,file2.txt,file3.txt
To search for sequence of strings, including phrases
To ignore case while searching , use option '-i'
To print the count of lines which having pattern 'linux'
To print the lines which are not having pattern 'linux'
To search pattern for all txt files in current directory
To print the line numbers with each output lines ,where given pattern 'linux' found
To print only the name of the files where pattern 'linux' is matching
Some more useful grep command:
To prints lines containing any character
To prints lines containing period character
To prints blank lines or lines not containing any character.
To print line starting with any pattern
To print line ending with any pattern
Examples in which defining sone boundry of words
To print the lines which are containing words starting with pattern 'linux'
To print the lines which are containing words ending with pattern 'linux'
To print the lines which are containing given pattern 'linux' as a whole words
uniq command
To removes adjacent repeated lines in the given data or files
Syntax
Examples
consider the file as file4.txt which containing following data
To removes adjacent repeated lines in the given data or files
To print only adjustant duplicate lines
To print only uniq lines which is not adjustant duplicate
To display the line count of occurrences of line adjustant duplicate
cmp command
Used to compare two files byte by byte ,its pinpoints only the first diffresnce between two files
Syntax
Consider the content of file1.txt
Consider the content of file2.txt
Now on comparing the both the file
The above command is comparing both the files and only giving first difference at line no 4.
comm command
Used to compare sorted files FILE1 and FILE2 line by line.
Syntax
comm command gives the output in 3 columns
1.data uniq in first file
2.data uniq in second file
3.data common in both files
To use comm command both the files should be in sorted manner.
To sort the file1.txt and storing the result in file10.txt
To sort the file2.txt and storing the result in file20.txt
Now on using the 'comm' command on sorted files as following
Used to combine 2 or more files content line by line and display the files content horizontally
Syntax
$ paste file1.txt [file2.txt..]
wc command
Used to display the no of lines,words and characters in any file or in any data.
syntax
$ wc [-lwc] filename.txt
head command
Used to display the set of lines from start in given file or data.
Syntax
$ head [-n] filename.txt
tail command
Used to display the set of lines from end of given file or data.
Syntax
$ tail [-n] filename.txt
Examples on head and tail commands
consider file1.txt having 15 lines as showing below
$ cat file1.txt
1 One
2 Two
3 Three
4 Four
5 Five
6 Six
7 Seven
8 Eight
9 Nine
10 Ten
11 Eleven
12 Twelve
13 Thirteen
14 Fourteen
15 Fifteen
1 One
2 Two
3 Three
4 Four
5 Five
6 Six
7 Seven
8 Eight
9 Nine
10 Ten
11 Eleven
12 Twelve
13 Thirteen
14 Fourteen
15 Fifteen
To print first 10 lines from top
$ head file1.txt
(or)
$ head -10 file1.txt
(or)
$ head -10 file1.txt
Note : If count is not specified in command ,then by default it will take first 10 lines
To print the first 7 lines
$ head -7 file1.txt>
(or)
$ head file1.txt -n 7
(or)
$ head file1.txt -n 7
To print 10 lines from bottom
$ tail file1.txt
(or)
$ tail -10 file1.txt
(or)
$ tail -10 file1.txt
Note : If count is not specified in command ,then by default tail command will
take 10 lines from bottom
To print the 7 lines from bottom
$ tail -7 file1.txt
(or)
$ tail file1.txt -n 7
(or)
$ tail file1.txt -n 7
To print the lines from line no 3 to 8
$ head -8 file1.txt | tail -6
cut command :
Used to retreive specific fields or characters from a file or data, it is used on output
of some other command
Syntax
$ cut [option] [filename]
Examples
Consider a file name file3.txt having content as following
$ cat > file3.txt
hello how are you!
one two three four
hello how are you!
one two three four
To retrieve the 7th character
$ cat file3.txt | cut -c7
To retrieve a character set from 5th to 9th character
$ cat file3.txt | cut -c5-9
To retrieve 5th and 9th character
$ cat file3.txt | cut -c5,9
We can pipe cut command on any command like date ie:
$ date | cut -c7
$ date | cut -c5-9
$ date | cut -c5-9
To cut the 4th field delimated by spaces
$ date | cut -d "" -f4
To cut the 4th to 6th field delimated by space
$ date | cut -d "" -f4-6
To cut the 4th and 6th field delimated by space
$ date | cut -d "" -f4,6>
$ cat file3.txt | cut -d "" -f4,6
$ cat file3.txt | cut -d "" -f4,6
Note :
-c : for character number
-d : for delimiter (either space,comma,colon etc)
-f : for field number
tr (translate) command
Used to translate characters, by pipe on some other command
Syntax
$ tr [option] SET1 SET2
Examples
To squeezing character or remove the extra spaces between words
type tr -s " " on output of command ie :
The output of command 'who am i' having extra space as
$ who am i
mukesh.kumar pts/4 2014-03-12 16:08 (:0.0)
mukesh.kumar pts/4 2014-03-12 16:08 (:0.0)
The following command will squeeze the words or remove the extra spaces
$ who am i | tr -s " "
mukesh.kumar pts/4 2014-03-12 16:08 (:0.0)
mukesh.kumar pts/4 2014-03-12 16:08 (:0.0)
Translating the character without squeezing
$ who am i | tr " " "$"
Squeezing and then translation
$ who am i | tr -s " " "$"
tr Command examples:
Consider file2.txt having the below content
$ cat file2.txt
Hi Friends
Hello World
How are you !
Lets learn the commands
Hi Friends
Hello World
How are you !
Lets learn the commands
To remove the extra spaces or squeezing the space character
$ cat file2.txt | tr -s " "
output:
Hi Friends
Hello World
How are you !
Lets learn the commands
output:
Hi Friends
Hello World
How are you !
Lets learn the commands
Squeezing space and translating the space to '$' character
$ cat file2.txt | tr -s " " "$"
output:
Hi$Friends
Hello$World
How$are$you$!
Lets$learn$the$commands
output:
Hi$Friends
Hello$World
How$are$you$!
Lets$learn$the$commands
Translating the spaces without squeezing
$ cat file2.txt | tr " " "$"
output:
Hi$$$$$$$Friends
Hello$$$$$$World
How$are$$$$you$$$$$$!
Lets$$$$$$$$learn$the$$$$commands
output:
Hi$$$$$$$Friends
Hello$$$$$$World
How$are$$$$you$$$$$$!
Lets$$$$$$$$learn$the$$$$commands
To convert the lower case into upper case letter
$ cat file2.txt | tr "[a-z]" "[A-Z]"
output:
HI FRIENDS
HELLO WORLD
HOW ARE YOU !
LETS LEARN THE COMMANDS
output:
HI FRIENDS
HELLO WORLD
HOW ARE YOU !
LETS LEARN THE COMMANDS
Translating all character 'H' or 'h' to '#'
$ cat file2.txt | tr '[H,h]' '#'
output:
#i Friends
#ello World
#ow are you !
Lets learn t#e commands
output:
#i Friends
#ello World
#ow are you !
Lets learn t#e commands
sort command
Used to sort the data by default it sorts as per the english disctionary
It used on output of some other command or file content
Syntax
$ sort [option] [filename]
Examples
To sort as per the english disctionary
$ sort file.txt
To sort in reverse order
$ sort -r file.txt
To apply the numerical sort
$ sort -n file.txt
To remove all duplicate recards in file or data
$ sort -u file.txt
To merge the content of two or more file in sorted manner
$ sort -m file1.txt file2.txt
Note:
If using -m option with sort command, file1.txt file2.txt should be in sorted order.
If the supplied arguments files(file1.txt, file2.txt) are not in sorted order then we should
use only 'sort' instead of 'sort –m'.
Consider the following data in student.txt, in which the first column is student id ,
2nd column is marks and 3rd is subject.
$ cat student.txt
2001:90:Math
2002:60:Physics
2003:75:Chemistry
2004:84:Math
2005:94:Chemistry
2006:96:Math
2007:86:Physics
2008:91:Physics
2009:89:Physics
2001:90:Math
2002:60:Physics
2003:75:Chemistry
2004:84:Math
2005:94:Chemistry
2006:96:Math
2007:86:Physics
2008:91:Physics
2009:89:Physics
Sorting based on the second column, delimated by colon
$ sort -k2 -t ":" student.txt
(or)
$ sort +1 –t ":" student.txt
output:
2002:60:Physics
2003:75:Chemistry
2004:84:Math
2007:86:Physics
2009:89:Physics
2001:90:Math
2008:91:Physics
2005:94:Chemistry
2006:96:Math
(or)
$ sort +1 –t ":" student.txt
output:
2002:60:Physics
2003:75:Chemistry
2004:84:Math
2007:86:Physics
2009:89:Physics
2001:90:Math
2008:91:Physics
2005:94:Chemistry
2006:96:Math
Sorting based on the 3rd column, delimated by colon
$ sort -k3 -t ":" student.txt
(or)
$ sort +2 –t ":" student.txt
output:
2003:75:Chemistry
2005:94:Chemistry
2001:90:Math
2004:84:Math
2006:96:Math
2002:60:Physics
2007:86:Physics
2008:91:Physics
2009:89:Physics
(or)
$ sort +2 –t ":" student.txt
output:
2003:75:Chemistry
2005:94:Chemistry
2001:90:Math
2004:84:Math
2006:96:Math
2002:60:Physics
2007:86:Physics
2008:91:Physics
2009:89:Physics
To store the final merged data into onother one file
$ sort –m file1.txt file2.txt –o output
grep command
Used to search for PATTERN in each FILE or standard input.
Syntax
$ grep [OPTION]... PATTERN [FILE]
Examples
To search the pattern 'Linux' in file1.txt
$ grep Linux file.txt
To search the pattern 'Linux' in multiple files say file1.txt,file2.txt,file3.txt
$ grep Linux file1.txt file2.txt file3.txt
To search for sequence of strings, including phrases
$ grep 'hello world' file1.txt file2.txt file3.txt
To ignore case while searching , use option '-i'
$ grep -i linux file.txt
To print the count of lines which having pattern 'linux'
$ grep -c linux file.txt
To print the lines which are not having pattern 'linux'
$ grep -v pattern file.txt
To search pattern for all txt files in current directory
$ grep 'linux is' *
To print the line numbers with each output lines ,where given pattern 'linux' found
$ grep –n linux file.txt
To print only the name of the files where pattern 'linux' is matching
$ grep –l linux file1 file2 file3
Some more useful grep command:
To prints lines containing any character
$ grep '.' filename.txt
To prints lines containing period character
$ grep '\.' filename.txt
To prints blank lines or lines not containing any character.
$ grep –v '.' filename.txt
To print line starting with any pattern
$ grep '^pattern' filename.txt
To print line ending with any pattern
$ grep 'pattern$' filename.txt
Examples in which defining sone boundry of words
To print the lines which are containing words starting with pattern 'linux'
$ grep '\<linux' filename.txt
To print the lines which are containing words ending with pattern 'linux'
$ grep 'linux\>' filename.txt
To print the lines which are containing given pattern 'linux' as a whole words
$ grep '\<linux\>' filename.txt
uniq command
To removes adjacent repeated lines in the given data or files
Syntax
$ uniq [OPTION]... [INPUT [OUTPUT]]
Examples
consider the file as file4.txt which containing following data
cat file4.txt
math
physics
physics
chemistry
linux
math
math
window
math
physics
physics
chemistry
linux
math
math
window
To removes adjacent repeated lines in the given data or files
$ uniq file4.txt
output:
math
physics
chemistry
linux
math
window
output:
math
physics
chemistry
linux
math
window
To print only adjustant duplicate lines
$ uniq -d file4.txt
output:
physics
math
output:
physics
math
To print only uniq lines which is not adjustant duplicate
$ uniq -u file4.txt
output:
math
chemistry
linux
windo
output:
math
chemistry
linux
windo
To display the line count of occurrences of line adjustant duplicate
$ uniq -c file4.txt
Output:
1 math
2 physics
1 chemistry
1 linux
2 math
1 window
Output:
1 math
2 physics
1 chemistry
1 linux
2 math
1 window
cmp command
Used to compare two files byte by byte ,its pinpoints only the first diffresnce between two files
Syntax
$ cmp file1.txt file2.txt
Consider the content of file1.txt
$ cat file1.txt
john
monty
hi
linux
bye
john
monty
hi
linux
bye
Consider the content of file2.txt
$ cat file2.txt
john
monty
hi
unix
bye
john
monty
hi
unix
bye
Now on comparing the both the file
$ cmp file1.txt file2.txt
output:
file1.txt file2.txt differ: byte 15, line 4
output:
file1.txt file2.txt differ: byte 15, line 4
The above command is comparing both the files and only giving first difference at line no 4.
comm command
Used to compare sorted files FILE1 and FILE2 line by line.
Syntax
$ comm [OPTION]... FILE1 FILE2
comm command gives the output in 3 columns
1.data uniq in first file
2.data uniq in second file
3.data common in both files
To use comm command both the files should be in sorted manner.
To sort the file1.txt and storing the result in file10.txt
$ sort file1.txt > file10.txt
To sort the file2.txt and storing the result in file20.txt
$ sort file2.txt > file20.txt
Now on using the 'comm' command on sorted files as following
$ comm file10.txt file20.txt
output :
bye
hi
john
linux
monty
unix
output :
bye
hi
john
linux
monty
unix
No comments:
Post a Comment