The Cut-based selection

We can also select a text from each line of single/multiple files using the cut command. The cut command allows us to select a column based on delimiters. By default, TAB is used as delimiter. We can also select a portion of the text in a line by specifying the characters or range. The syntax is as follows:

cut OPTION [FILE …]

The cut command works on the single and multiple files. By default, the output is printed on stdout.

The options for the cut command are explained in the following table:

Option

Description

-b LIST

This selects bytes that are specified in LIST.

-c LIST

This selects characters that are specified in LIST.

-d DELIM

This uses delimiter as DELIM instead of TAB. It also prints lines that don't have a delimiter.

-f LIST

This only selects fields specified in LIST.

--complement

This complements a set of selected bytes, characters, or fields.

-s

Don't print lines that don't have a delimiter.

--output-delimiter=STRING

This uses STRING as the output delimiter. By default, the input delimiter is used.

LIST is made up of a range or many ranges separated by a comma. A range is specified as follows:

Range

Meaning

N

This is the Nth byte, character, or field, counted from 1

N-

This is from the Nth byte, character, or field, to the end of the line

N-M

This is from the Nth to Mth byte (including M and N), character, or field.

-M

This is from the first to Mth (include) byte, character, or field.

Cutting across columns

A lot of Linux command outputs are formatted in such a way that the results have multiple fields and each field is separated by space or tabs. The outputs of each field can be viewed by looking down into a particular field column.

Execute the ls -l ~ command and observe the following output:

$ ls -l ~
Cutting across columns

Now, we are interested only in knowing the modification time and filename. To achieve this, we will need the column 6 to 9:

$ ls -l ~ | tr -s ' ' |cut -f 6-9 -d ' '
Cutting across columns

By default, TAB is used as a delimiter. Here, there are multiple spaces between any two columns in the ls -l output. So, first using tr -s, we will squeeze multiple whitespace into single whitespace and then we will cut the column field range 6-9 with a delimiter as whitespace.

Text selection in files

Consider the cut1.txt file as an example. The content of the file is as follows:

$ cat cut1.txt

The output will be:

Text selection in files

Now, we are interested in knowing the names of the students. We can get this by fetching the first column. Here, each column is separated by Tab. So, we will not have to specify the delimiter in our command:

$ cut -f1 cut1.txt
Name
Foo
Bar
Moo
Bleh
Worm
Lew

Another interesting thing to do is to get unique department names. We can do this by using the following set of commands on the cut1.txt file:

$ cut -f4 cut1.txt | tail -n +2 | sort -u
Civil
CSE
ECE
Mechanical

We can see that there are four unique departments mentioned in the cut1.txt file.

Another interesting thing we can do is find out who received the highest marks, as follows:

$ cut -f1,3 cut1.txt | tail -n +2 | sort -k2 -nr | head -n1
Worm    99

To find out who scored the highest mark, we first select the first and third column from the cut1.txt file. Then, we exclude the first line using tail -n +2, which tells us what this file is about, because we do not need this. After that, we do numerical sorting of the second column in reverse order, which contains the marks of all the students. Now, we know that the first column contains the details of those who scored the highest marks.

Knowing the speed of your system processor is interesting in order to know the various details of your system. Among all, one of them knows the speed of your processor. The first thing to know is that all processor details are available in the /proc/cpuinfo file. You can open this file and see what all details are available. For example, we know that the processor's speed is mentioned in the "model name" field.

The following shell script will show the speed of the processor:

#!/bin/bash
#Filename: process_speed.sh
#Description: Demonstrating how to find processor speed ofrunning system

grep -R "model name" /proc/cpuinfo | sort -u > /tmp/tmp1.txt
tr -d ' ' </tmp/tmp1.txt > /tmp/tmp2.txt
cut -d '@' -f2 /tmp/tmp2.txt

Running this script will output the processor speed of your system:

$ sh processor_speed.sh
2.80GHz

We can also do without using temporary files:

$ grep -R "model name" /proc/cpuinfo | sort -u | cut -d '@' -f2
2.80GHz
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.112.187