The cut command in Linux is a powerful text-processing tool used to extract specific columns or fields from lines of text. Its primary function is to cut out and display specified parts of each line in a file, making it invaluable for extracting columns of data from text files, such as logs or CSV files.
Syntax
The syntax of the cut command is very simple:
cut OPTION... [FILE]...
OPTION
: customize the behavior.FILE
: Specifies the input file(s) to read data. If not specified, reads from the standard input.
Here are some commonly used options:
-b
,--bytes=LIST
: Select specific bytes or byte ranges.-c
,--characters=LIST
: Choose specific characters or character ranges.-d
,--delimiter=DELIM
: Set a custom delimiter character.-f
,--fields=LIST
: Select specific fields using a delimiter.--complement
: Display everything except what you've specified.-s
,--only-delimited
: Print lines containing the delimiter.--output-delimiter=STRING
: Specify a custom output delimiter.
Cut Command Examples
Below are detailed examples showcasing cut command with different options.
Using the -b (Bytes) Option
Extract a Single Byte:
echo "abcdefghijklmnopqrstuvwxyz" | cut -b 17
This command extracts the 17th byte ('q') from the provided string.
Extract Multiple Specific Bytes:
echo "abcdefghijklmnopqrstuvwxyz" | cut -b 1,2,3
Here, bytes 1, 2, and 3 ('abc') are selected and extracted.
Using Byte Ranges:
echo "abcdefghijklmnopqrstuvwxyz" | cut -b 1-3
This extracts a range of bytes (1 to 3, 'abc').
Multiple Byte Ranges:
It is also possible to select multiple integer ranges by separating them with commas.
echo "abcdefghijklmnopqrstuvwxyz" | cut -b 1-3,8-10
It extracts bytes in the ranges 1-3 ('abc') and 8-10 ('hij').
Selecting a Single Byte with Only Ending Position
echo "abcdefghijklmnopqrstuvwxyz" | cut -b -1
This command extracts just the first byte of the string. The -1
indicates that the range extends from the start of the line to the first byte.
Extracting from a Starting Byte to the End of the Line
echo "abcdefghijklmnopqrstuvwxyz" | cut -b 1-
This command extracts everything from the first byte to the end of the line, essentially capturing the entire string. The 1-
specifies a range starting from the first byte and extending to the end of the line.
The -c (Characters) Option
The -c option functions similarly to -b and can be used interchangeably for all the examples provided above.
Utilizing -d and -f Options
Extracting Specific Fields:
uptime | cut -d "," -f 1
Using ,
as a delimiter, this extracts the first field from the uptime
command output.
Implementing the --complement Option
Excluding Selected Fields:
uptime | cut -d "," -f 1,2 --complement
This command prints all fields except the first and second.
Using --output-delimiter Option
Changing Output Delimiter:
uptime | cut -d "," -f 1,2 --output-delimiter='~'
This example changes the input delimiter from ,
to ~
.
Additional Practical Examples
1. Extracting the Last Field of a String
echo 'example.com' | rev | cut -d'.' -f 1 | rev
Output:
com
2. Getting the First Column
echo 'landscape:x:109:115::/var/lib/landscape:/usr/sbin/nologin' | cut -d: -f1
Output:
landscape
3. Removing Multiple Columns
echo '1column 2column 3column 4column 5column 6column' | cut -d" " -f1-2,4,6-
Output:
1column 2column 4column 6colum
4. Extracting Login Shells of Users
getent passwd | rev | cut -d ":" -f 1 | rev | grep -v "nologin"
This command lists the login shells of all users, excluding those with 'nologin'.
5. Listing System Users
cat /etc/passwd | cut -d ":" -f 1
This extracts usernames from /etc/passwd.
Comments