+5 votes
281 views
How to use CUT command in Linux

in Linux / Unix by (551k points)
reopened | 281 views

1 Answer

+3 votes
Best answer

1. Linux cut command
2. How to use the cut command in Linux
3. How to use the -b (byte) Linux parameter
4. How to use the -c (column) Linux parameter
5. How to use the -f (field) Linux parameter
6. How to use the –complement Linux parameter
7. How to use the –output-delimiter Linux parameter
8. How to delimit values ​​from other Linux directories

The use of commands in Linux is one of the most practical ways to manage many tasks either at the level of files, services or processes, and the best thing about this is that we have several commands each with its own particular function..

One of these commands that we can use frequently is the cut command, which is implemented in Unix operating systems , and the purpose of this command is to remove or trim sections of each line of a specified file.

The use of this command can be used to cut parts of a line either by position of byte, character or field as needed, so that with the cut command it will be possible to extract part of a text based on some variables to use ..

TechnoWikis will explain in detail how you can use the cut command and give it proper use in Linux environments, in this case we work in Ubuntu 19.04 .


1. Linux cut command


The basic syntax for using this command is as follows:
 cut OPTION ... [FILE] ... 
Among the options available for this command we find the following and you decide which one to use:
This option lists and selects only the bytes of each line based on those indicated in LIST. LIST can refer to a byte, a set of bytes or a range of bytes
 -b, --bytes = LIST 

This option only selects the characters of each line based on LIST
 -c, --characters = LIST 

It is possible to use the DELIM character to be used as a field delimiter
 -d, --delimiter = DELIM 

By using this option we will select only the fields on each line or we can print the lines that do not contain delimiter characters
 -f, --fields = LIST 

It is a compatibility option
 -n 

This option complements the set of selected bytes, characters or fields
 --complement 

Does not print lines that do not contain delimiters
 -s, --only-delimited 

It is possible to use STRING as the output output delimiter string
 --output-delimiter = STRING 

Deploy the help of the cut command
 --help 

We can see the version of the cut command used
 --version 

2. How to use the cut command in Linux

Step 1

To start we can visualize the content of a text file located on the desktop and with which we will see the work of the cut command, this content can be visualized using the cat command:

image

Step 2

Each list in Linux consists of an integer, a range of integers or various ranges of integers which are separated by commas, with cut you can use the following reading options.
  • N the byte, character or Nth field, counted from 1.
  • N- from the Nth byte, character or field, to the end of the line.
  • NM from Nth to Mth byte, character or field (inclusive).
  • -M from first byte to mth byte, character or field

If we execute cut without a parameter we will see the following result:

image


3. How to use the -b (byte) Linux parameter


The first use we will make of cut is with the -b (byte) parameter which extracts the bytes that we indicate, for this the -b option must be with the list of numbers of bytes separated by commas.
Note
The byte range can be indicated with a hyphen (-)

Something important to keep in mind is that both the tabs and the blanks are considered as a 1-byte character to keep it in mind when defining the range or limit..

Step 1

For example, we will execute the following:
 cut -b 1,2,3 TechnoWikis 

image

Step 2

There is a special way that allows us to select the bytes from the beginning to the end of the line, for this we can execute the following:
 cut -b 1- TechnoWikis 

image

Step 3

Here the value 1- indicates the selection from the first byte to the end of the byte of a line in the file, now, we can execute the following:
 cut -b -4 TechnoWikis 

image

In this case we have extracted the first 4 bytes of the line.


4. How to use the -c (column) Linux parameter


Another of the parameters of use is the column selection ©, this allows us to select a set of characters according to the need, its use syntax is as follows:
 cut -c [(k) - (n) / (k), (n) / (n)] file 
Here the values ​​can be a list of numbers separated by commas or a range of numbers separated by a hyphen.

In the syntax given the letter k indicates the initial position of the character and the letter n refers to the final position of the character on each given line.

Step 1

We will execute the following and we will see what happens:
 cut -c 2,4,6 TechnoWikis 

image

Step 2

In this case we see that the characters are extracted in the positions that we have indicated, but it is possible to display a range of characters as follows:
 cut -c 2-5 TechnoWikis 

image

Step 3

Like the previous parameter we can display all the bytes of the line using the following option:
 cut -c 1- TechnoWikis 
Or we can extract a defined range as follows:
 cut -c -3 TechnoWikis 

5. How to use the -f (field) Linux parameter


When we use the -c parameter it is used for lines with an already defined extension, but as in Linux systems there are files without a length limitation.
Step 1

We must cut the data by fields instead of using the columns, there comes into play the -f parameter which uses the following syntax:
 cut -d "delimiter" -f (field #) file 
We can execute the following:
 cut -f 1 TechnoWikis 

image

Step 2

There we can add the -d parameter, which takes the space as a field separator or delimiter:
 cut -d "" -f 1 TechnoWikis 

image

Step 3

Now we can display more fields if it is the case using the range with a script:
 cut -d "" -f 1-3 TechnoWikis 

image


6. How to use the –complement Linux parameter

Step 1

This value complements the output of the cut results and can be used with -fo with -c without problems, for example, let's run:
 cut --complement -d "" -f 1 TechnoWikis 

image

Step 2

Or we can execute:
 cut --complement -c 4 TechnoWikis 
This will cut the fourth character in the lines of the selected file:

image


7. How to use the –output-delimiter Linux parameter


It fulfills the same function as the input delimiter used with the -d option, this delimiter can be edited using the syntax –output-delimiter = ”delimiter”.
 cut -d "" -f 1,2 TechnoWikis --output-delimiter = '%' 

image

There the spaces are complemented with the% sign.


8. How to delimit values ​​from other Linux directories


The cut command allows us to delimit parameters in other directories or Linux files, an example of this is the / etc / passwd directory in which the information of each user in the system is hosted, one user per line, and there each of These fields are delimited by a colon (":"), the syntax of a user is as follows:
 root: x: 0: 0: root: / root: / bin / bash 
The data represented here are
  • Username
  • Password (hidden with an x ​​if it has been encrypted)
  • User ID number (UID)
  • Group ID number (GID)
  • Comments
  • Home Directory
  • Shell
Step 1

Since the username is the first field on the line, if we want to display only this field we will execute:
 cut -f 1 -d ':' / etc / passwd 

image

Step 2

Now, to see additional fields, such as the GUI we can add the respective column:
 cut -f 1,4 -d ':' / etc / passwd 

image

Step 3

In both cases the output is limited by the two points (:), but it is possible to assign a different delimiter for both the input and output of the results, for example, if the output is delimited by a space we must execute the following:
 cut -f 1,4 -d ':' --output-delimiter = '' / etc / passwd 

image

With the cut command in Linux it is possible to carry out various activities that allow us to visualize the contents of a file in a much more practical way.


by (3.5m points)
edited

Related questions

+3 votes
1 answer
asked Oct 20, 2020 in Linux / Unix by backtothefuture (551k points) | 219 views
+4 votes
1 answer
asked Sep 22, 2020 in Linux / Unix by backtothefuture (551k points) | 310 views
+3 votes
1 answer
asked Nov 9, 2019 in Linux / Unix by backtothefuture (551k points) | 287 views
+5 votes
1 answer
asked Oct 24, 2019 in Linux / Unix by backtothefuture (551k points) | 212 views
+5 votes
1 answer
asked Sep 19, 2019 in Linux / Unix by backtothefuture (551k points) | 296 views
Sponsored articles cost $40 per post. You can contact us via Feedback
10,634 questions
10,766 answers
510 comments
3 users