Click on each book below to review & buy on Amazon.
As an Amazon Associate, I earn from qualifying purchases.
LPI Linux Essentials Exam 010-160 - Topic 3.2 - cut Command
The cut
command in Linux is a versatile utility for text processing. It serves to remove or "cut out" particular sections from each line of a file or an input stream, which can then be displayed on the standard output. This utility can be incredibly useful when you're dealing with delimited data files, extracting columns from a table, or parsing logs.
Syntax
The basic syntax of the cut
command is as follows:
cut [OPTION]... [FILE]...
If no file is specified, cut
reads from the standard input.
Key Options
-d, --delimiter=DELIM
: UseDELIM
instead of the TAB character for field delimiter.-f, --fields=LIST
: Select only these fields.-c, --characters=LIST
: Select only these characters.--complement
: Complement (invert) the set of selected bytes, characters, or fields.
Using cut
with /etc/passwd
The /etc/passwd
file is a text-based database that describes user account information. Each line in this file represents a single user account and contains various fields separated by colons :
. Below is a generic entry for illustration:
user1:x:1001:1001:User One:/home/user1:/bin/shell
Extract Usernames
In this exercise, you will use the cut
command to extract all usernames from the /etc/passwd
file.
Exercise: Extract usernames:
cut -d: -f1 /etc/passwd
-d:
specifies that the delimiter is a colon.-f1
tellscut
to display only the first field, which is the username.
Extract User Shells
To obtain a list of all the shells used by users, follow the below exercise:
Exercise: List shells in /etc/passwd
:
cut -d: -f7 /etc/passwd
-f7
denotes that we are interested in the 7th field, which is the shell assigned to the user.
Extract Multiple Fields
Suppose you want both the username and the shell fields. You can specify multiple fields like so:
Exercise: Display username and their shell:
cut -d: -f1,7 /etc/passwd
f1,7
means thatcut
will display the 1st and the 7th fields.
Complementing the Selection
The --complement
option allows you to invert the selection. For example, if you wish to display all fields except the 2nd one (which typically contains an 'x' for the password), you can follow:
Exercise: Display all fields except historic password field:
cut -d: -f2 --complement /etc/passwd
Select by Character Position
If you want to select specific characters rather than fields, you can do so using the -c
option.
Exercise: Display the first 10 characters from each line of /etc/passwd
:
cut -c1-10 /etc/passwd
- Here,
-c1-10
means you want characters from position 1 to 10.
Summary
The cut
command is an essential tool for text processing in Linux. By understanding how to use its various options, you can efficiently parse and manipulate text data, as demonstrated with exercises on the /etc/passwd
file.
Support DTV Linux
Click on each book below to review & buy on Amazon. As an Amazon Associate, I earn from qualifying purchases.
NordVPN ®: Elevate your online privacy and security. Grab our Special Offer to safeguard your data on public Wi-Fi and secure your devices. I may earn a commission on purchases made through this link.