Cut Command examples in Linux.

What is “cut” command in Linux?

“Cut” Command is a utility for cutting sections from each line of the Files. It can be used to cut parts of a line by byte position, character and delimiter. It can also be used to cut data from file formats like CSV.

Examples of Cut Command.
You can specify byte position using the option -b, it will show only that letter, I am using the letter JustGeek here.
If you want to display only first letter i.e. J

[[email protected] ]# echo "JustGeek" | cut -b 1
J

If you want to display the letter J,S,G i.e. 1,3 and 5th Letter then you can specify using “,”

[[email protected] ]# echo "JustGeek" | cut -b 1,3,5
JsG
[[email protected] ]#

Cut based on a delimiter:-
To cut using a delimiter use the -d option. This is normally used in conjunction with the -f option to specify the field that should be cut.
Create a file called names.csv with content below

Jeffery,Smith,45,London
Anthony,Evans,33,California
Charles,Jones,28,Paris

The delimiter can be set to a comma with -d ‘,’. cut can then pull out the fields you want with the -f flag. Example

[[email protected] ]# cat names.csv | cut -d ',' -f 1
Jeffery
Anthony
Charles
[[email protected] ]#

You can comma “,” separate for multiple entries. Example.

[[email protected] ]# cat names.csv | cut -d ',' -f 1,2
Jeffery,Smith
Anthony,Evans
Charles,Jones

If you want display age then you can just specify position with -f. Example

[[email protected] ]# cat names.csv | cut -d ',' -f 3
45
33
28

And if you want to Display City, then:

[[email protected] ]# cat names.csv | cut -d ',' -f 4
London
California
Paris

If you want to Change Delimeter in display then you can use the option –output-delimiter as shown in example below.

[[email protected] ]# cat names.csv | cut -d ',' -f 1,2 --output-delimiter='---'
Jeffery---Smith
Anthony---Evans
Charles---Jones
[[email protected] ]#

How to Check “cut” command version

 [[email protected] ]# cut --version
 cut (GNU coreutils) 8.22
 Copyright (C) 2013 Free Software Foundation, Inc.
 License GPLv3+: GNU GPL version 3 or later http://gnu.org/licenses/gpl.html.
 This is free software: you are free to change and redistribute it.
 There is NO WARRANTY, to the extent permitted by law.
 Written by David M. Ihnat, David MacKenzie, and Jim Meyering.

Leave a Reply

Your email address will not be published. Required fields are marked *