Sei sulla pagina 1di 8

Unix head and tail commands

Note:$head abc(it display top 10 records of the file)


Q)How to delete last two records?
A) $ head -n -2 filename

Tail:displays last records of the file.


$ tail -3 abc
6,fff,6000
7,ddd,7000
8,ggg,8000

Note:$tail abc(it display last 10 records of the file)


By using head and tail commands we get in between records?
Q)in a file i have 10 records and i want to get 4th to 8th reoords?
A)head -8 filename|tail -5
Cut:
Cut command in unix (or linux) is used to select sections of text from each line of files. You can
use the cut command to select fields or columns from a line by specifying a delimiter or you can
select a portion of text by specifying the range or characters. Basically the cut command slices a
line and extracts the text.

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

2)i want to cut first three characters in the file?


$ cut -c -3 abc (or) cut c 1-3 abc
1,a
2,b
3,c
4,d
5,e
6,f
7,d
8,g

3)i want first field in the file?


ans) By using delimeter(d) and field(f)
$ cut -d "," -f 1 abc
1
2
3
4
5
6
7
8

4)i want first field and second field in the file?


$ cut -d "," -f 1,2 abc
1,aaa
2,bbb
3,ccc
4,ddd
5,eee
6,fff
7,ddd
8,ggg

5)i want first and third field in the file?


$ cut -d "," -f 1,3 abc
1,1000
2,2000
3,3000
4,4000
5,5000
6,6000
7,7000
8,8000

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

$ sed 2p abc(it print the second record twice)


1,aaa,1000
2,bbb,2000
2,bbb,2000
3,ccc,3000
4,ddd,4000
5,eee,5000
6,fff,6000
7,ddd,7000
8,ggg,8000

1)i want only second record with out repeating?


a) by using n it print only once.
$ sed -n 2p abc
2,bbb,2000
2)How to print second to fifth record in the file?
$ sed -n "2,5p" abc
2,bbb,2000
3,ccc,3000
4,ddd,4000
5,eee,5000

3)How to print first and third record in the file?


$ sed -ne "1p" -e "3p" abc
1,aaa,1000
3,ccc,3000

4)How to print first three records with out using n and p?


Ans) By using q option
$ sed "3q" abc
1,aaa,1000
2,bbb,2000
3,ccc,3000

5)How to delete second record in a file?


Ans)By using d option
$ sed "2d" abc
1,aaa,1000
3,ccc,3000
4,ddd,4000
5,eee,5000
6,fff,6000
7,ddd,7000
8,ggg,8000

6)How to delete fourth record to sixth record?


$ sed "4,6d" abc
1,aaa,1000
2,bbb,2000
3,ccc,3000
7,ddd,7000
8,ggg,8000

Q)how to delete 3,5,6, records?


A)sed e 3d e 5d e 6d filename
7)How to insert new record at 4th position?
$ sed '4i 9,sri,20000' abc
1,aaa,1000
2,bbb,2000
3,ccc,3000
9,sri,20000
4,ddd,4000
5,eee,5000
6,fff,6000
7,ddd,7000
8,ggg,8000

8)How to insert the record after two records?


$ sed '2a 10,dev,20000' abc
1,aaa,1000
2,bbb,2000
10,dev,20000
3,ccc,3000
4,ddd,4000
5,eee,5000
6,fff,6000
7,ddd,7000
8,ggg,8000

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

9)replace abc with def


$ sed 's|abc|def|g' ww
1,def,2000
2,def,3000
3,xyz,4000
4,ghi,5000
5,def,6000
6,def,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

11)i want to replace the string 2nd record to last record?


$ sed '2,$ s|abc|def|g' ww
1,abc,2000
2,def,3000
3,xyz,4000
4,ghi,5000
5,def,6000
6,def,1000
7,bji,6888

Grep:it is used for searching pattern.

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

4)How to print multiple strings with matching pattern?


$ grep -e "abc" -e "xyz" ww
1,abc,2000
2,abc,3000
3,xyz,4000
5,abc,6000
6,abc,1000
10,xyz,4563

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

i want to print with case sensitive?


$ grep -i -w "abc" deva1
abc
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

Q)how to find patteren in files?


A)$grep iR name.*
Q)How do you get list of empty lines?
A)grep ^$ filename
Q)count of empty lines?
A)grep c ^$ filename
Q)how to remove empty lines?
A)grep v ^$

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

2)how to convert small letters to capital letters?


$ tr "[a-z]" "[A-Z]" < 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

3)how to delete all small letters in file?


$ tr -d "[a-z]" <abc
1,,1000
2,,2000
3,,3000
4,,4000
5,,5000
6,,6000
7,,7000
8,,8000

-s : replaces repeated characters listed in the set1 with single occurrence.

$ 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

Create two files:b and b1


$ cat>b
1
2
3
4
5
6

$ cat>b1
a
b
c
d
e
f

1)How to merge two files?


$ paste b b1
1 a
2 b
3 c
4 d
5 e
6 f

2)How to add delimeter in between the files?


$ paste -d "," b b1
1,a
2,b
3,c
4,d
5,e
6,f

3)How to changefiles to vertical?


$ paste -s b b1
1 2 3 4 5 6
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

2)i want to sort the order based on numeric column?


$ sort -nk1 r
1 w 3
2 r 4
4 c 2
5 a 1
6 d 3

3)i want to sort reverse order on third field?


$ sort -nrk3 r
2 r 4
6 d 3
1 w 3
4 c 2
5 a 1

4) How to check order?


$ sort -c r
sort: r:4: disorder: 4 c 2

Potrebbero piacerti anche