5/9/12

CUT command unix/linux


CUT command :
i'm going to take below sample file for the examples shown.
-> cat test.txt
201200001|201200001|LIFE|A
201200002|201200002|STD|B
201200003|201200003|LTD|C
201200004|201200004|LTD|X
201200005|201200005|LTD|E

cut command will be used with the following options
  • cut -c
  • cut -d with -f
1) cut -c :

-> cut -c 11 test.txt
 the above command will give the 11th character from each line of the file
2
2
2
2
2

-> cut -c 11,12 test.txt
the above command will give the 11th and 12th characters from each line
20
20
20
20
20

-> cut -c 1-10 test.txt
the above command will give characters from 1 to 10 from each line
201200001|
201200002|
201200003|
201200004|
201200005|

-> cut -c -8 test.txt
the above command will return charaters from 1 to 8 from each like similary like above result
20120000
20120000
20120000
20120000
20120000

-> cut -c 10- test.txt

the above command will return charaters from 10th to end of the line from each line.
|201200001|LIFE|A
|201200002|STD|B
|201200003|LTD|C
|201200004|LTD|X
|201200005|LTD|E

2) cut -d with -f:
character followed by -d in the unix command is a delimeter either we can use in the quotes     (-d "|") or we can use with back slash after the -d ( -d\).

-f specifies the filed number to return

-> cut -d "|" -f3 test.txt
or we can use
-> cut -d\| -f3 test.txt
the above command returns 3rd filed in each line which have the pipe(|) as the delimeter
LIFE
STD
LTD
LTD
LTD
-> cut -d\| -f3,4 test.txt
the above command will return the 3rd and 4th filed from each line

LIFE|A
STD|B
LTD|C
LTD|X
LTD|E
-> cut -d\| -f3,5 test.txt
the above command only gives 3rd filed bcz there is no 5th filed
LIFE
STD
LTD
LTD
LTD

-> cut -d\| -f1- test.txt
the above command will return data from filed1 to the end of each line.

201200001|201200001|LIFE|A
201200002|201200002|STD|B
201200003|201200003|LTD|C
201200004|201200004|LTD|X
201200005|201200005|LTD|E
-> cut -d\| -f-3 test.txt
the above command will return data from field1 to field3 from each line.

201200001|201200001|LIFE
201200002|201200002|STD
201200003|201200003|LTD
201200004|201200004|LTD
201200005|201200005|LTD










0 comments:

Post a Comment