Linux Command: cut and tr

An overview of “cut” and “tr” command line utilities

Md Shamim
Geek Culture

--

Photo by Andre Taissin on Unsplash

Linux Command: cut

The cut command is a command-line utility that allows us to cut out sections of a specified file or piped data and print the result to standard output.

>> man cut

cut OPTION... [FILE]...

# Options
-c, --characters=LIST
-d, --delimiter=DELIM
-f, --fields=LIST
..
..

Following is a dummy text file: Let’s see how we can manipulate the following text file to print out the output as per our needs.

# dummy.txt

06:38:28.077150666: Error Package management process launched in container 06:38:28.077150666,090aad374a0a,nginx,root
06:38:33.058263010: Error Package management process launched in container 06:38:33.058263010,090aad374a0a,nginx,root
06:38:38.068693625: Error Package management process launched in container 06:38:38.068693625,090aad374a0a,nginx,root
06:38:43.066159360: Error Package management process launched in container 06:38:43.066159360,090aad374a0a,nginx,root
06:38:48.059792139: Error Package management process launched in container 06:38:48.059792139,090aad374a0a,nginx,root
06:38:53.063328933: Error Package management process launched in container 06:38:53.063328933,090aad374a0a,nginx,root

Print by Character Range

Print out the output within a range of characters :

# range 1-5
>> cut -c 1-5 dummy.txt

06:38
06:38
06:38
06:38
06:38
06:38

# range 20-40
>> cut -c 20-40 dummy.txt

Error Package manage
Error Package manage
Error Package manage
Error Package manage
Error Package manage
Error Package manage

# range 76-end
>> cut -c 76- dummy.txt

06:38:28.077150666,090aad374a0a,nginx,root
06:38:33.058263010,090aad374a0a,nginx,root
06:38:38.068693625,090aad374a0a,nginx,root
06:38:43.066159360,090aad374a0a,nginx,root
06:38:48.059792139,090aad374a0a,nginx,root
06:38:53.063328933,090aad374a0a,nginx,root

Print by field

Suppose, we want to extract data from the following file based on the fields.

# dummy1.txt 
NAME EMAIL PHONE ADDRESS
devid devid@xyz.com 0897663232 tokyo,japan
harry harry@xyz.com 0232323232 miyazaki,japan
jane jane@xyz.com 0323213122 osaka,japan

We have to separate each field using “-d = delimiter” option (can be a character, by default TAB). And then we have to specify the field number we want to print.

-d, --delimiter=DELIM   
-f, --fields=LIST

>> cut -d ' ' -f1

In the following demonstration, we have used space(‘ ’) as a delimiter.

>> cut -d ' ' -f1 dummy1.txt 
NAME
devid
harry
jane

>> cut -d ' ' -f2 dummy1.txt
EMAIL
devid@xyz.com
harry@xyz.com
jane@xyz.com

# Print out multiple field

>> cut -d ' ' -f1,3 dummy1.txt
NAME PHONE
devid 0897663232
harry 0232323232
jane 0323213122

As we discussed earlier, a delimiter can be a character, In the following demonstration we have used a comma (, ) as a delimiter:

>> echo "jane,jane@xyz,236473264,osaka" | cut -d ','  -f1
jane

>> echo "jane,jane@xyz,236473264,osaka" | cut -d ',' -f2
jane@xyz

>> echo "jane,jane@xyz,236473264,osaka" | cut -d ',' -f3
236473264

Linux Command: tr

tr command — Translate, squeeze, and/or delete characters from standard input, writing to standard output.

>> man tr

tr [OPTION]... SET1 [SET2]

# Options
-c, --complement -- use the complement of SET1
-d, --delete -- delete characters in SET1, do not translate
-s, --squeeze-repeats -- replace each sequence of a repeated character
that is listed in the last specified SET,
with a single occurrence of that character
..
..

Replace characters:

>> echo "Hello World" | tr 'H' 'h'
hello World

>> echo "Hello World" | tr 'Ho' 'KK'
KellK WKrld

Delete characters:

>> echo "Hello World" | tr -d 'Ho'  
ell Wrld

# complement the delete
>> echo "Hello World" | tr -cd 'Hd\n'
Hd

>> echo "Hello World 12345 " | tr -cd [:digit:]
12345

>> echo "Hello World 12345 " | tr -cd [:alpha:]
HelloWorld

Squeeze characters

>> echo "HHHHHHHHello Worrrrrrrrrldddddddddddddddddd" | tr -s 'Hord' 
Hello World

>> echo "Hello World" | tr -s [:lower:] [:upper:]
HELO WORLD

🖥 All Articles on Linux 👇

All Articles on Linux

12 stories

--

--

Md Shamim
Geek Culture

Cloud Infrastructure Engineer | AWS Community Builder | AWS | Kubernetes | GitHub Actions | Terraform | 👇👉 linkedin.com/in/shamimice03 github.com/shamimice03