Understanding the `cut` Command in Linux
The cut
command in Linux is a straightforward utility that is used to extract
sections from each line of files. It can be used to cut parts of a line by byte
position, character and field. In other words, cut
command is useful for
slicing out columns and fields from a file or a stream of text data.
Syntax of cut
Command
The basic syntax of the cut
command is as follows:
cut OPTION... [FILE]...
OPTION
... - One or more options that control the operation of the command.[FILE]...
- One or more files to process with the command. If no file is specified, or if the file is-
,cut
reads from the standard input.
Options for cut
Command
The following table lists the common options available for the cut
command:
Option | Shorthand | Description |
---|---|---|
--bytes=LIST | -b LIST | Select only these bytes |
--characters=LIST | -c LIST | Select only these characters |
--fields=LIST | -f LIST | Select only these fields; also print any line that contains no delimiter character, unless the --only-delimited option is specified |
--delimiter=DELIM | -d DELIM | Use DELIM instead of TAB for field delimiter |
--only-delimited | -s | Do not print lines not containing delimiters |
--output-delimiter=STRING | --output-delimiter=STRING | Use STRING as the output delimiter the default is to use the input delimiter |
--complement | --complement | Complement the set of selected bytes, characters or fields |
--help | Display help and exit | |
--version | Output version information and exit |
Creating a Sample Text File Using vim
Before we proceed with examples, let's create a sample text file
named data.txt
using vim
:
vim data.txt
In vim
, press i
to go into insert mode and type or paste the following
content:
Name:Age:City
Alice:21:New York
Bob:30:San Francisco
Charlie:25:Los Angeles
To save and exit vim
, press Esc
, then type :wq
and press Enter
.
Examples of Using cut
Example 1: Cutting by Field
To cut out the first field of each line, using :
as a delimiter:
cut -d ':' -f 1 data.txt
This command will output:
Name
Alice
Bob
Charlie
Example 2: Cutting by Character Range
To cut characters from 1 to 5 on each line:
cut -c 1-5 data.txt
Output:
Name:
Alice
Bob:3
Charl
Example 3: Cutting by Byte Position
To cut by bytes, you would use -b
followed by the byte positions:
cut -b 1-5 data.txt
Example 4: Cutting Multiple Fields
To cut the first and third fields out of each line:
cut -d ':' -f 1,3 data.txt
Output:
Name:City
Alice:New York
Bob:San Francisco
Charlie:Los Angeles
Example 5: Complementing the Selection
To cut everything except the first field:
cut -d ':' --complement -f 1 data.txt
Output:
Age:City
21:New York
30:San Francisco
25:Los Angeles
Example 6: Cutting with a Different Output Delimiter
To cut the first field from each line and change the output delimiter to a comma:
cut -d ':' --output-delimiter=',' -f 1 data.txt
Output:
Name,Alice,Bob,Charlie
Example 7: Cutting Based on Character Range
To cut the second to fourth character on each line:
cut -c 2-4 data.txt
Output:
ame
lic
ob:
har
Example 8: Selecting Non-Delimited Lines
By default, cut
will print lines even if they don't contain the delimiter. If
you want to only show lines that contain the delimiter:
cut -d ':' -s -f 2 data.txt
Output:
Age
21
30
25
The cut
command can
be very powerful when used in conjunction with other text processing tools such
as grep
, sort
, uniq
, and awk
. By mastering cut
, you can effectively
manipulate textual data in Linux, extracting and rearranging data as required.
What Can You Do Next 🙏😊
If you liked the article, consider subscribing to Cloudaffle, my YouTube Channel, where I keep posting in-depth tutorials and all edutainment stuff for software developers.