9 Practical Examples of the cut Command in Linux

9 Practical Examples of the cut Command in Linux

Linux-based working techniques supply many command-line textual content processing utilities you need to use in your day-to-day routine. The lower command is one such textual content manipulation utility that makes use of delimiters, bytes, fields, and columns to fetch a required string from a flat-file database or a line.

Since a flat-file database doesn’t have a structural relationship and the information are separated with the assistance of delimiters, lower is a perfect software to extract characters or bytes of knowledge. This text discusses the lower command intimately and demonstrates some sensible examples with totally different filter choices to get you began with the software.


Overview of the lower Command

lower makes use of the next primary syntax:

lower <choice> <file>

It’s also possible to pipe it with different instructions as follows:

echo “string_of_characters” | lower <choices>

Please observe that the absence of any choice generates an error. Listed here are some flags you need to use with lower to invoke its varied features:

Choice Description
-b Cuts by byte place
-c Cuts by character place
-d Extracts string utilizing the delimiter specified with (-f) area choice
-f Extracts by a specified area
-s Extracts for less than delimiters
–complement Print the output apart from the sphere which you will have specified
–output-delimiter Substitute the prevailing delimiter with the one you will have specified

MAKEUSEOF VIDEO OF THE DAY

Listed here are some examples of the lower command that may allow you to get a greater understanding of the software and its features.

Use the -b choice to fetch strings of characters by their byte depend, as follows:

echo "Good day World" | lower -b 1,2,3,5,8,9

Output:

Heloor

It’s also possible to specify a file path as a substitute of a string:

lower -b 1,2,3,4,5 filename.txt

Or you’ll be able to extract characters as per their place within the textual content. Use the -c choice adopted by the sequence/order of characters you want to fetch as follows:

echo "Good day World" | lower -c 1,2,3,5,6,8,10,11

Output:

Helo previous

Equally, to fetch fields or columns from a flat-file database, use the lower command with the -f flag choice:

lower -f 3 database.txt

Cut by Field

2. Lower Vary of Bytes or Characters

It’s also possible to use the -b flag with the lower command to extract and print a number of ranges of bytes as follows:

lower -b 1-7,16-23 database.txt

Cut by Range of Bytes

You’ll be able to outline the vary to fetch characters utilizing the -c flag as follows:

lower -c 1-7,16-23 database.txt

Cut by Range of Characters

Use the -cn- choice to extract textual content from nth character onwards to the tip of the road, the place n is the index of a personality within the string.

For instance, to extract the string ranging from the sixth character to the tip of the road:

lower -c6- database.txt

Cut Start Till End

Or use the next command to fetch from begin until the final m characters from the file:

lower -c-8 database.txt

4. Lower A number of Fields From a File

It’s also possible to print a number of fields from a file utilizing the -f flag.

lower -f 1,2 database.txt

Output:

NAME AGE 
Jhon 20
Lisa 24
Jack 18
Bruce 23
Nancy 19

You should utilize the -d flag to specify the delimiter with the -f choice. The delimiter specifies the character used to separate fields in a textual content file.

As an illustration, to extract the primary column of the /and so on/passwd file, use a colon (:) because the delimiter:

lower -d ‘:’ -f 1 /and so on/passwd

Cut With Delimiter And Field

Equally, the delimiter worth within the given stream of textual content is a single house:

echo "The 1% of the 1%" | lower -d ' ' -f 2,3

Output:

1% of

6. Change Output Delimiter for Show

The –output-delimiter flag provides flexibility to the lower command output. As an illustration, you need to use the flag to show the output in a separate line utilizing the $’n’ (newline) worth, as follows:

grep alice /and so on/passwd | lower -d ':' -f 1,5,7 --output-delimiter=$'⁠n'

Output:

alice
Alice
/bin/sh

Furthermore, you too can exchange a delimiter of an enter file with the delimiter of your alternative within the output by utilizing the –output-delimiter flag:

grep root /and so on/passwd | lower -d ':' -f 1,6,7 --output-delimiter=@

Output:

root@/root@/bin/bash
nm-openvpn@/var/lib/openvpn/chroot@/usr/sbin/nologin

7. Lower Fields Solely When a Line Accommodates the Delimiter

Use the -s flag to extract fields solely when the strains include a delimiter. As an illustration, the next command is not going to fetch the sphere -f 1 until it has an area delimiter:

echo "HelloWorld" | lower -d " " -f 1 -s

Quite the opposite, the below-mentioned command will generate an output because the string comprises the required delimiter:

echo "Good day World" | lower -d " " -f 1 -s

Output:

Good day

Equally, you too can use another character because the delimiter:

echo "Good day:World" | lower -d ":" -f 2 -s

Output:

World

8. Complement the Command Output

The lower utility additionally permits you to print all of the fields besides the required area. Use the –complement choice as follows to fetch all the main points of the basis consumer account besides the fifth column:


grep "root" /and so on/passwd | lower -d ':' --complement -s -f 5

Output:

root:x:0:0:/root:/bin/bash

Equally, within the instance under, the -f 1 parameter should show Good day textual content nonetheless, because of the –complement choice, it solely prints the remainder of it.

echo "Good day to the entire World" | lower -d " " -f 1 --complement

Output:

to the entire World

9. Pipe lower With the sed Utility

The very best half is you’ll be able to mix the lower command with the usual output of different Linux/Unix instructions.

sed is one such textual content modifying software you need to use to delete, insert, and exchange your textual content. You’ll be able to simply pipe its output to the lower command.

Within the instance under, the grep command output is piped to the sed utility which replaces colon (:) with a hyphen (-) after which, lower shows fields 1, 6, and seven as follows:

grep alice /and so on/passwd | sed 's/:/-/g' | lower -d ' ' -f 1,5,7

Output:

alice-x-1005-1008-Alice-/house/new/alice-/bin/sh

Working With Textual content on Linux Utilizing lower

The lower command is a versatile and environment friendly command-line utility that you need to use in varied use circumstances of textual content manipulation. It makes use of operations to filter out textual content from information or commonplace enter information. The utility additionally has a bonus enabling customers to restrict the textual content they wish to show and add delimiters of their very own alternative.

Nonetheless, this command has some limitations as nicely. You can not use common expressions to specify delimiter or use concurrent a number of lower command choices. This text has coated primary to advance stage sensible examples of this utility to assist newcomers with a transparent understanding of text-manipulation instructions on Linux.


best linux text manipulation commands
The 5 Most Helpful Linux Textual content-Manipulation Instructions

Need to up your Linux sport and study extra about text-manipulation? Listed here are the highest 5 Linux command-line instruments to start your journey.

Learn Subsequent


About The Creator

Leave a Comment

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