Home » 2014 » December » 16 » CUT command in Linux : cut the file vertically

9:39 PM
CUT command in Linux : cut the file vertically

CUT- Remove sections from each line of files vertically. This command basically cuts the file vertically and returns a certain field from each line from a file.

FORMAT
       cut [OPTION]... [FILE]...

DESCRIPTION
       Print selected parts of lines from each FILE to standard output.

       Mandatory arguments to long options are mandatory for short options too.

       -b, --bytes=LIST
              select only these bytes

We are using below file for demo:

shanky@localhost:/home/shanky/test:> cat afile1
first second third four
first second third four
first second third four
first second third four
first second third four

To print only the 5th character from each line of a file:

shanky@localhost:/home/shanky/test:> cut -b 5 afile1
t
t
t
t
t

-c, --characters=LIST
              select only these characters. Eg. the below command will print only first character from each line of the file.

shanky@localhost:/home/shanky/test:> cut -c 1 afile1
f
f
f
f
f

 

       -d, --delimiter=DELIM
              use DELIM instead of TAB for field delimiter. If we dont give any delimiter, it will use tab or space. In the below example, we have used delimiter ":" to seperate the fields.


shanky@localhost:/home/shanky/test:> cat afile1|tr -s " " ":">afile2
shanky@localhost:/home/shanky/test:> cat afile2
first:second:third:four
first:second:third:four
first:second:third:four
first:second:third:four
first:second:third:four

shanky@localhost:/home/shanky/test:> cut -d ":" -f2,4 afile2
second:four
second:four
second:four
second:four
second:four

 

       -f, --fields=LIST
              select only these fields;  also print any line that contains no delimiter character, unless the -s option is specified

shanky@localhost:/home/shanky/test:> cat afile1
first second third four
first second third four
first second third four
first second third four
first second third four

shanky@localhost:/home/shanky/test:> cut -d " " -f2,4 afile1
second four
second four
second four
second four
second four

 

  -s, --only-delimited
              do not print lines not containing delimiters

 


  

--output-delimiter=STRING
              use STRING as the output delimiter. The default is to use the input delimiter

shanky@localhost:/home/shanky/test:> df -k|grep %|tr -s " " ":"|cut -d ":" -f5,6|grep ^[8-9][0-9]%*
82%:/nfs/fs1/kwstrs
82%:/nfs/fs1/adm

 

shanky@localhost:/home/shanky/test:> df -k|grep %|tr -s " " ":"|cut --output-delimiter "   " -d ":" -f5,6|grep ^[8-9][0-9]%*
82%   /nfs/fs1/kwstrs
82%   /nfs/fs1/adm

 

See the difference of above two outputs. In the first output, the delimiter is ":" which is the input delimiter and in second output we have given "space" as output delimiter.


Use one, and only one of -b, -c or -f.  Each LIST is made up of one range, or many ranges separated by commas.  Selected input is written in the same order that it is read, and is written exactly once.  Each range is one of:

 N N'th byte, character or field, counted from 1
 N- from N'th byte, character or field, to end of line

 N-M

-M

from N'th to M'th (included) byte, character or field

from first to M'th (included) byte, character or field

          


See below examples executed on the same file:

shanky@localhost:/home/shanky/test:> cut -b 5-10 afile1
t seco
t seco
t seco
t seco
t seco

 

 

The above command will print from 5th to 10th byte, character or field

 
 

Category: Open System-Linux | Views: 1579 | Added by: shanky | Tags: cut command in linux, cut command in unix, shankys blogs, cut, shankys, cut example, cut command with example | Rating: 5.0/1

Related blogs


You may also like to see:


[2014-03-13][Open System-Linux]
crontab: A command in linux to automatically start/repeat a process at certian time and interval
[2014-06-05][Open System-Linux]
SCRIPT command: Save your Linux sessions automatically
[2014-01-08][Open System-Linux]
SSH Login Setup
[2014-09-21][Open System-Linux]
How to define command prompt strings in Linux : what are $PS1, $PS2, $PS3 and $PS4?
[2015-07-18][Open System-Linux]
Creating and Managing Logical Volume Manager in Linux

Total comments: 0
ComForm">
avatar