Learn how to use cut command in various scenarios and in scripting with lots of examples. Understand how to extract selective data using cut command.
Everybody in Linux or Unix environment is familiar with grep command which is used for pattern finding/filtering. This is kind of selective data extraction from source data. cut is another command which is useful for selective data extraction.
cut command basically extracts portion of data from each line of given file or supplied data. Syntax to be used for cut command is
# cut [options] [file]
Below different options can be used with cut command :
-c To select only this number of character.
Roughly you can say its a column number to be extracted. See below example:
# cat test sd teh dxff cq2w31q5 # cut -b 4 test f w
Here, cut is instructed to select only 4th character. If you look closely, in output it shows only 4th column letters. Lines which are having less than 4 characters are shown blank in cut output!
This option is useful in scripting when there is a need of single column extraction from supplied data.
-b To select only this number of bytes.
This option tells command to select only given number of byte from each line of data and show in output. In most of the cases its output is same as -c option since each character is treated as one byte in human readable text file.
-d Use specified delimiter instead of TAB
Normally cut uses tab as a delimiter while filtering data. If your file has different delimiter like comma (,) in CSV file or colon (:) in /etc/passwd file then you can specify this different delimiter to cut command. Command will process data accordingly. This option should always be used with b,c or f options. Only using -d option will result in below error :
# cut -d a test cut: you must specify a list of bytes, characters, or fields Try `cut --help' for more information.
-f To select only specified number of fields.
This option is to specify which all fields to be filtered from data. This option if not supplied with delimiter character data (-d option/ no TAB in data) then it will print all lines. For example if you say -f 2 then it search for TAB or supplied delimiter values if any in data. If found then it will print 2nd field from delimiter for that line. For lines where it wont find delimiter, it will print whole line.
# cat test this is test file raw data asdgfghtr data #cut -f1 test this is test file raw data asdgfghtr # cut -d" " -f2 test is data asdgfghtr data
Field numbering is as below:
Field one is the left side field from first occurrence of delimiter
Field second is right side field from first occurrence of delimiter
Fields third is right side second field from first occurrence of delimiter
Observe above example, where it prints all lines as it is when delimiter is not specified. When delimiter is defined as single space ” ” then it prints field two according to numbering explained above.
-s To print only lines with delimiters
Missing delimiter causes cut to print whole line as it is which may mislead desired output. This is dangerous when command is being used in scripts. Hence -s option is available which restricts cut to display whole line in case of missing delimiter.
#cut -f1 -s test
Using same test file from above example when -s is specified then there is no output. This is becuase default delimiter TABdoes not exist in file.
Number to be used with -b, -c and -f options:
In all above examples we have declared single integers like 2,4 for -b, -c and -f options. But we can also use below system :
- x : Single number like we used in all examples
- x- : From xth bye, character or field till end of line
- x-y : From xth byte, character or field till y th
- -y : From first byyte, character or field till y th
#cut -d" " -f2- test is test file data asdgfghtr data # cut -d" " -f2-3 test is test data asdgfghtr data # cut -d" " -f-1 test this raw asdgfghtr data
Few examples of cut command :
To get list of users from /etc/passwd file, we will use delimiter as : and cut out first field.
# cut -d: -f1 /etc/passwd root bin daemon adm lp ------ output clipped -----
Cut command can be feed with data from pipe as well. In that case last [file] parameter shouldnt be defined. Command will read input from pipe and process data accordingly. For example we are grep ing user with uid 0 and then getting their usernames using cut like below:
# cat /etc/passwd |grep :0: | cut -d: -f1 root sync shutdown halt operator
Getting userid and group id from /etc/passwd file along with their usernames.
# cat /etc/passwd |cut -d: -f1-4 root:x:0:0 bin:x:1:1 daemon:x:2:2 adm:x:3:4 lp:x:4:7 sync:x:5:0