The cut command Linux

The cut command is used if you need to cut part of the text — in this case, it may be in the file or to be printed via the standard input. In Unix systems, this command removes sections of text that were identified using bytes, characters or fields delimited by “-“ and “:”. Work cut ensures utility of the same name. It is part of the GNU Coreutils, so is available by default in all Linux distributions. Also it can be used on machines with the operating system BSD.

We will try to elaborate on how to work with cut command, using both standard input and text files. Well, as always, will give examples of use to those who first encounters with the performance of this team was something to navigate.

The syntax of the command cut

The cut command looks like the following:

$ filepath cut options

To use a file do not have. If the place its name, a dash or do not specify nothing, the command will take text from standard input. Optionally, you can specify more files than one.

Options cut

The list of options that enable you to control the behavior of the command:

  • -b (–bytes=LIST) — the number of bytes, the set or range of bytes to be cut.
  • -c (–characters=LIST) — the character that should be cut. You can also specify a set or range of characters.
  • -d (–delimiter=DELIM) — using this option the user sets a separator instead of the default TAB.
  • -f (–fields=LIST) — a list of fields to cut out.
  • -s (–only-delimited) — if you apply this option, the cut does not display rows where there is no delimiter.
  • –complement — sets the bytes, characters or fields that will remain in the file or the text from the standard input. Everything else will be cut.
  • –output-delimiter=STRING — the default output delimiter matches the input. This option allows you to specify a different output separator.
  • -z, –zero-terminated instead of newline delimiter will be NULL.

These are the basic options of cut, we need to work with the utility.

Examples of usage cut in Linux

First of all, let’s create the file cut_command_example.txt and place it in the home directory. In the body of the document will contain the text:

Winter: white: snow: frost
Spring: green: grass: warm
Summer: colorful: blossom: hot
Autumn: yellow: leaves: cool

1. Working with bytes

At first glance it seems that using bytes is most convenient to specify the coordinates of characters that need to be cut. But not so simple. In texts written in English, each character is one byte, but in other languages, some letters, numbers and special characters can take two, three, and four bytes. This feature should be considered, taking into account the coding system of the text, otherwise the command will fail as expected.

To cut out text printed in the standard input, the first character, the cut command should be:

echo "The sky was yellow as brass." | cut-b 1

If you need to cut a few characters, they specify a comma:

echo "I looked at my watch; not eight o'clock." | cut-b 5,8,17

There is nothing complicated to cut out a certain range of characters. In this case, numbers symbols indicate with a hyphen:

echo "Still I opened the gate, and put the petrol pump in readiness." | cut-b 38-43

When in a predetermined range of numeric values exceeds the number of bytes contained in the text, it will display an error message or simply an empty string (depends on the version cut, which is installed on the computer).

Now consider the use command, where you need to cut characters from a text file. A standard entry in the terminal looks like this:

cut-b 1,9 cut_command_example.txt

As you may remember, in the previously created document, it was four lines. The team in each of them cut out the characters corresponding to the number of bytes, and put them line by line.

About the same work cutting out characters in a specified range. A range is two numbers written with a hyphen:

cut-b 12-20 cut_command_example.txt

The cut command can be used by itself, but it is not forbidden to combine with other teams. Most often used sort. Try to cut the first 4 characters and arrange the string in alphabetical order:

cut-b 1-7 cut_command_example.txt | sort

To sort the sort command does not need additional options. But to place the rows in reverse order, to add to record -r:

cut-b 1-7 cut_command_example.txt | sort -r

With the help of hyphen, which stands next to the number indicating the byte number, you can specify an undefined character ranges. For example, the team cut out all characters starting from the fifth to the last, you need to put the hyphen to the right of the number:

cut-b 5 - cut_command_example.txt

And that was cut all characters starting at the first byte and ending with the sixteenth, the hyphen should be placed to the right of the number:

cut-b -25 cut_command_example.txt

Similarly, a hyphen is used if the text is not from a file, and via the standard input:

echo "The pallid sunlight through the window shone upon my hands." | cut-b -35

echo "The pallid sunlight through the window shone upon my hands." | cut-b 35-

2. Working with symbols

Those who handles huge volumes of text data, often need to use the cut command by setting the necessary parameters with characters. If the text is printed via the standard input, cut the first character can be as follows:

echo "She offered to help me, but that was not allowed." | cut-c 1

To cut to other characters, you must specify its serial number. It is important to remember that if the numeric value exceeds the number of characters in the string, the command will fail.

echo "We worked till dusk, then washed and dressed." | cut-c 12,31,45

When working with symbols, you can use ranges of values, in the same way as when working with bytes.

echo "The two cars were now racing side by side along the straight road." | cut-c 17-52

There is often a need to cut part of the text of the finished document. To do this, simply run the command

cut-18,25 c cut_command_example.txt

As you can see in the screenshot, was cut the eighteenth and twenty-fifth characters in each row.

If you cut only the first character in each line with the command

cut-c 1 cut_command_example.txt 

the result will be the same as the team

cat cut_command_example.txt | cut-c 1 

To cut from a text file a range of characters to print in the terminal the following command:

cut-c 7-34 cut_command_example.txt

The ability to sort of cut out characters persists regardless of whether in bytes or in characters the cut command, the parameters were set of the text. The sort command arranges the symbols in alphabetic order by default:

cut-c 1-9 cut_command_example.txt | sort

The balance of characters in reverse order — starting with the end of the alphabet — requires the-r option:

cut-c 1-9 cut_command_example.txt | sort -r

We already know the trick with the hyphen next to the number can be repeated in this case:

echo "We had had fights for the same reason before now." | cut-c 12-

Will cut a sequence of characters, beginning with the twelfth and ending with the last in the line.

But to cut the characters from the first to the twelfth, a hyphen should be moved to the left:

echo "We had had fights for the same reason before now." | cut-c -12

With the text contained in the file, do the same:

cut-c -8 cut_command_example.txt

cut-c 8 - cut_command_example.txt

Cuts the text in each of the rows according to the specified parameters.

3. Work with columns

Creating the file cut_command_example.txtwe have separated words from each other not only by whitespace, but also colons. It’s finally time to use them in practice. And they need to cut fragments of text from column to column and are called delimiters. Except for colons and spaces as delimiters, you can use hyphens.

But we begin, as always, with text to standard output. To cut out a word from the first column, use the command

echo "From the inn issued a smell of frying liver." | cut-d '' -f 1

In the case of a text file the result is the same — the first word of each column is printed in the terminal:

cut-d ':' -f 1 cut_command_example.txt

To cut several columns, it is necessary to specify their numbers. For text printed in the standard input, the command is:

echo "He was extraordinarily particular about politeness in others." | cut-d '' -f 1,2,3

In this example the result should be the first three words — He was extraordinarily.

When using the text file, the same command looks like this:

cut-d ':' -f 1,2,3 cut_command_example.txt

Of the file was cut the first three columns, and their contents displayed in the terminal.

Not worse than team work if you ask her a text in the form of a range of numbers:

echo "Surprised, we looked at one another." | cut-d '' -f 1-5

As expected, was cut columns (words) from first to fifth. Note the two apostrophes that from time to time appear in the record of the team when working with the text from the standard input. These signs are necessary if the delimiter is a space character. Now cut a range of fields from a file:

cut-d ':' -f 1-3 cut_command_example.txt

Exactly the same as before assorted carved symbols that we can do now is to put the names of the seasons from a file in alphabetical order:

cut-d ':' -f 1 cut_command_example.txt | awk '{print $ 1}' | sort

Changing the numeric value of the-f option, you can cut and sort the contents of other columns. To sort the names of the seasons in reverse order will help the-r option added to the sort command:

cut-d ':' -f 1 cut_command_example.txt | awk '{print $ 1}' | sort-r

Cut the contents starting from a particular column to the last in the line you get when you use this command, where the right of the number indicating the number of the first column is a hyphen:

echo "We became still more friendly." | cut-d '' -f 2-

Moving the hyphen to the left, we will make this column the last, and cut out the word team will be starting in the first column.

echo "We became still more friendly." | cut-d '' -f -2

The work of this team with text files do not have fundamental differences:

cut-d ':' -f 2 - cut_command_example.txt

cut-d ':' -f -2 cut_command_example.txt

The cut command is also useful when working with massive CSV documents. Its syntax and options in this case remain unchanged. For example, when the writing will be cut the first two columns of the file named file_example.csv:

cut-d '' - f 1,2 file_example.csv

Insights

The team cut linux is a tool that will be useful to all Linux users who work with large text documents. Its capabilities are truly fantastic and it has many advantages over other similar utilities.

As always, feel free to ask questions in the comments, if some aspects of this command remains unclear to you.

Source: losst.ru

(Visited 11 times, 1 visits today)