Unix Head and Tail Commands
Unix Head and Tail Commands
Syntax:
$cut <option> filename
Options:
1)c:character
2)f:field
3)d:delimeter
Example:
1) I want to cut first charcter in the file?
$ cut -c 1 abc
1
2
3
4
5
6
7
8
Sed:
Sed is a Stream Editor used for modifying the files in unix (or linux). Whenever you want to make
changes to the file automatically, sed comes in handy to do this. Most people never learn its
power; they just simply use sed to replace text. You can do many things apart from replacing text
with sed.
Synatax:
$sed <option > filemane
Options:
1)p:print
2)n:number
3)e:multiple records
4)d:delete
5)i:insert
6)a:append
7)q:quit
Note:if you add the records and want to save the file permanently
give the > symbol file name it will save in that file.
$ sed '2a 10,dev,20000' abc >f1
String replacement by using sed command
Syntax:
Sed s|old string|new string|g
S represents substitute
G represents global
$ represents last record
. represents current record
Ctreate a file
$ cat ww
1,abc,2000
2,abc,3000
3,xyz,4000
4,ghi,5000
5,abc,6000
6,abc,1000
7,bji,6888
10)i want to replace the string 1st record to 5th record only?
$ sed '1,5 s|abc|def|g' ww
1,def,2000
2,def,3000
3,xyz,4000
4,ghi,5000
5,def,6000
6,abc,1000
7,bji,6888
Grep stands for Global search for Regular Expressions and Print.
Syntax:
grep [options] pattern filename
options:
1)i-case sensitives
2)v-inverse
3)c-count
4)e-extra string
5)w-word
6)n-number
1)i want to print all the records which are containing abc?
$ grep abc ww
1,abc,2000
2,abc,3000
5,abc,6000
6,abc,1000
2)i want print all the records which are not having abc records?
$ grep -v abc ww
3,xyz,4000
4,ghi,5000
7,bji,6888
3)i want to print all the records which are having abc with case
sensitive?
$ grep -i abc ww
1,abc,2000
2,abc,3000
5,abc,6000
6,abc,1000
8,ABc,7889
9,Abc,2345
Create a file
$ cat deva1
thgdg
wabc
abc
xabc
Abc
Grep w:it prints the complete word abc present in the line.
$ grep -w "abc" deva1
abc
Grep n:it prints all the matchings records of abc and then
corresponding lines.
$ grep -n "abc" deva1
2:wabc
3:abc
4:xabc
Tr:
Tr stands for translate or transliterate. The tr utility in unix or linux system is used to
translate, delete or squeeze characters.
Options:
d-delete
s-squeeze
it translates delimeters
$ tr "," ":" <abc
1:aaa:1000
2:bbb:2000
3:ccc:3000
4:ddd:4000
5:eee:5000
6:fff:6000
7:ddd:7000
8:ggg:8000
$ cat>ee
aaaaaaaa aaabbbbbbb bbbbbbcccc cccccccccccc
bbbbbbbbbbbbbbbaaaaaaaaaacccccccccccccc
$ tr -s a <ee
abbbbbbb bbbbbbcccc cccccccccc c
bbbbbbbbbbbbbbbacccccccccccccc
$ tr -s ab <ee
abcccccc cccccccccc
bacccccccccccccc
paste:
The paste command merges the lines from multiple files. The paste command sequentially writes
the corresponding lines from each file separated by a TAB delimiter on the unix terminal.
Syntax:
Paste <option> file1 file2
Option:
1)d:delimeter
2)s:vertical
$ cat>b1
a
b
c
d
e
f
sort command:
Sort: command in unix or linux system is used to order the elements or text. Sort command has
the capability of sorting numerical values and strings. The sort command can order the lines in a
text file.
Option:
1)k:field separator
2)n:numeric
3)r:reverse
4)c:check order
Create a file
$ cat>r
1 w 3
2 r 4
5 a 1
4 c 2
6 d 3
1)i want to sort the order based on 2nd field?
$ sort -k2 r
5 a 1
4 c 2
6 d 3
2 r 4
1 w 3