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: 1550 | 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:


[2015-01-18][Open System-Linux]
The JAR archiving tool in Linux
[2015-03-23][Open System-Linux]
FSCK : Checking and reparing filesystems in Linux
[2015-06-08][Open System-Linux]
CHAGE command in LINUX
[2014-09-13][Open System-Linux]
md5sum: calculate and check md5 message digest of a file in Linux
[2014-10-24][Open System-Linux]
HEXDUMP command in Linux to display the file content in decimal, hexadecimal, octal format

Total comments: 0
ComForm">
avatar