cut - Unix, Linux Command


Advertisements

Previous Page
Next Page  
 

NAME

cut - remove sections from each line of files

SYNOPSIS

cut OPTION... [FILE]...

DESCRIPTION

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

OPTIONS

TAG DESCRIPTION
-b, --bytes=LIST select only these bytes
-c, --characters=LIST select only these characters.

 

-d, --delimiter=DELIM use DELIM instead of TAB for field delimiter
-f, --fields=LIST select only these fields; also print any line that contains no delimiter character, unless the -s option is specified
-n with -b: don't split multibyte characters
--complement complement the set of selected bytes, characters or fields

 

-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
--help display this help and exit
--version output version information and exit

EXAMPLES

EXAMPLE-1:

To Print characters of a particular position of a file:

$ cat file1:

My name is Ram

My name is shyam

$ cut -c1 file1

output: M M

EXAMPLE-2:

To Print range of characters of a particular position of above file1:

$ cut -c1-7 file1
output: My name My name

EXAMPLE-3:

To Select Column of Characters using either Start or End Position of above file1:

here start position is 3rd character:

$ cut -c3- file1
output: name is Ram name is shyam
here end position is 3rd character:
$ cut -c-3 file1
output: My My

EXAMPLE-4:

cut command to print the fields using the delimiter

To print second field of above file1 after space:
$ cut -d " " -f2 file1

output: name name

To print second and fourth field of above file1 after space:


$ cut -d " " -f2,4 file1

output:

name Ram

name shyam

EXAMPLE-5:

To select All Fields Except the Specified Fields

In order to complement the selection field list use option –complement.

The following example displays all the fields from /etc/passwd file except field 7

$ grep "/bin/bash" /etc/passwd | cut -d':' --complement -s -f7

output: root:x:0:0:root:/root bala:x:1000:1000:bala,,,:/home/bala

EXAMPLE-6:

To Combine Cut with Other Unix Command Output:

The power of cut command can be realized when you combine it with the stdout of some other Unix command.

Once you master the basic usage of cut command that we’ve explained above, you can wisely use cut command to solve lot of your text manipulation requirements.

The following example indicates how you can extract only useful information from the ps command output. We also showed how we’ve filtered the output of ps command using grep and sed before the final output was given to cut command. Here, we’ve used cut option -d and -f which we’ve explained in the above examples.

$ ps axu | grep python | sed 's/\s\+/ /g' | cut -d' ' -f2,11-

output: 2231 /usr/bin/python /usr/lib/unity-lens-video/unity-lens-video 2311 /usr/bin/python /usr/lib/unity-scope-video-remote/unity-scope-video-remote 2414 /usr/bin/python /usr/lib/ubuntuone-client/ubuntuone-syncdaemon 2463 /usr/bin/python /usr/lib/system-service/system-service-d 3274 grep --color=auto python
Note:in above example space is delimeter, based on your file content, you can use any special characters as delimeter and print fields

Previous Page
Next Page