Tuesday, 15 December 2015

Practical Example Of CUT command in UNIX/Linux

In this article we will see the basics of CUT command and real time practical  examples. CUT command basically used to extract sections from each line of input — usually from a file. Extraction of line segments can typically be done by bytes (-b), characters (-c), or fields (-f) but we uses -c & -f on regular basis to perform our day to day activities. In this article we will focus on -c & -f. We will use below example file for this article.

SYNTAX:
$ cut -d<delimiter> -<f/c><-><N><-/,>  [File_Name]

-c => Stands for Character
-f => Stands for field
-d => Stands for Delimiter
-  => Used for creating a window selection if used after c & f. We will see it in following examples.
File_Name => File of the name from where you want to extract. It is mandatory attributes until you are not providing input via pipe. Example is given below.

$ echo "Unix|Linux" | cut -d"|" -f1;

Note: Space is default delimiter for cut command.

Input File: F_Input_Data.txt
10001|A1|HR|10000|USA
10002|A2|FIN|20000|USA
10003|A3|NSS|30000|IND
10004|A4|SEC|40000|USA

>>>>>>>>>>>>>>>>>>>>>>>>>CHARACTER EXTRACTION<<<<<<<<<<<<<<

How to cut specified characters from a line/string/file
$ cut -c1  F_Input_Data.txtOutput: Will return 1 character from the line.
1
1
1
1


How to cut multiple characters from a line/string/file
$ cut -c1,2,3 F_Input_Data.txt
$ echo "Unix/Linux" | cut -c1,2,3 F_Input_Data.txt

Output: Will return 1st, 2nd & 3rd character from the line/file.
100
100
100
100


How to cut multiple characters in range from a line/string/file
$ cut -c1-3 F_Input_Data.txt
Output: Will return 1st, 2nd & 3rd character from the line/file. c1-3 creates a window which tells cut that selection will start from 1 and will go till 3.
100
100
100
100


How to cut characters from a position to end of the line
$ cut -c3-  F_Input_Data.txt
Output: Will return everything starting from 3rd character.
c3- creates a window which tells cut that selection will start from 3 and will go till end as right limit of window is not defined.
(default is end of line)
01|A1|HR|10000|USA
02|A2|FIN|20000|USA
03|A3|NSS|30000|IND
04|A4|SEC|40000|USA


How to cut characters from starting to a position of the line
$ cut -c-8  F_Input_Data.txt
Output: Will return everything starting from 3rd character. c-8 creates a window which tells cut that selection will start from 1st position and will go till 8th character as Left limit of window is not defined it will start from 1st position ( default start is 1st position)
10001|A1
10002|A2
10003|A3
10004|A4


How to print complete line using cut
$ cut -c-  F_Input_Data.txt
Output: Will print complete line. c- means no boundary go from 1st position to end position.


>>>>>>>>>>>>>>>>>>>>>>>>>FIELD EXTRACTION<<<<<<<<<<<<<<<<<<<


How to cut specific fields from every line in a file
$ cut -d"|" -f2 F_Input_Data.txt

Output: Will return 2nd field from pipe(|) delimited file.
A1
A2
A3
A4


How to cut multiple fields from every line in a file
$ cut -d"|" -f2,4 F_Input_Data.txt
Output: Will return 2nd & 4th field from pipe(|) delimited file.
A1|10000
A2|20000
A3|30000
A4|40000


How to cut fields xth to yth from a line
$ cut -d"|" -f2-4 F_Input_Data.txt

Output: Will return 2nd,3rd & 4th field from pipe(|) delimited file. f2-4 creates a window which tells cut that selection will start from 2nd field and will go till 4th field.
A1|HR|10000
A2|FIN|20000
A3|NSS|30000
A4|SEC|40000


How to cut a line to get all fields from first field to xth field
$ cut -d"|" -f-2 F_Input_Data.txt

Output: Will return 1st & 2nd field from pipe(|) delimited file. f-2 creates a window which tells cut that selection will start from 1st field and will go till 2nd field.
10001|A1
10002|A2
10003|A3
10004|A4


How to cut a line to get all fields from xth field to last field
$ cut -d"|" -f2- F_Input_Data.txt
Output: Will return every filed from 2nd field from pipe(|) delimited file. f2- creates a window which tells cut that selection will start from 2nd field and will go till last field.
A1|HR|10000|USA
A2|FIN|20000|USA
A3|NSS|30000|IND
A4|SEC|40000|USA

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...