- Linux Cut Command with Practical Examples
- Cut Command and Syntax
- Cut Options
- How to cut by delimiter
- How to complement the output selection
- How to specify an output delimiter
- How to cut by c haracters
- How to cut by bytes
- Some practical cut examples
- Conclusion
- How to cut by c haracters
- How to cut by bytes
- Some practical cut examples
- Conclusion
- Linux cut command
- Syntax
- Options
- Usage Notes
- Specifying LIST
- Specifying A Delimiter Other Than Tab
- Examples
- Related commands
- Linux Cut Command With Examples
- Syntax
- Select Column
- Select Column For Character Range
- Select Column Using Start Position
- Select Column Using End Position
- Select Single Field
- Select Multiple Fields
- Last Field
- Select Fields Include Specified Chars
- Select Fields Except for Specified Chars
- Specify Delimiter
- Print Except Fields
- Change Delimiter
Linux Cut Command with Practical Examples
The cut command is used in Linux and Unix systems to cut parts and sections from each line of a file and write the result to standard output. It can be used to cut parts of a line by byte position, character and field (delimiter).
In this tutorial, we learn Linux cut command with some practical examples you can use in your day to day command line activities.
Cut Command and Syntax
The basic syntax for the cut command is as follows:
Let’s check cut options and without any option cut command won’t work.
Cut Options
-f : Extract by specifying a field. Cut command uses ‘TAB’ as the default field delimiter.
-d : ‘Tab’ is the default delimiter and using this option you can use specific delimiter.
-b : To extract by specifying a byte. The range of bytes can also be specified.
-c : To cut by character. This can be a list of numbers separated comma or a range of numbers separated by a hyphen (-).
–complement : This will complement the selection
–output-delimiter : To change the output delimiter use the option -output-delimiter=’delimiter’.
—only-delimited : Cut will not print lines not containing delimiters
We will use the following text file named ‘content.txt’ and /etc/passwd file throughout this tutorial to illustrate our examples.
How to cut by delimiter
The most commonly used cut option is a combination of -d and -f . It will basically extract content based on specific delimiter and listed fields.
For example the following prints only the 1st field of each line from ‘/etc/passwd’ file using the delimiter (:).
The following example where we use space (» «) as delimiter and cut the 1st field from the file named ‘content.txt’.
This example, extracts more than one field from a specific file. Here we extract 1st and 6th field using colon (:) delimiter from the file ‘/etc/passwd’ which has the string ‘/bin/bash’:
To display the range of fields, specify start field and end field separated by hypen (-) as shown below:
How to complement the output selection
To complement the selection field list use —complement option. This option used where to select all fields except the specified fields.
In the following example command prints all the fields except the 2nd field in the from ‘/etc/passwd’ file:
How to specify an output delimiter
To specify the output delimiter use the —output-delimiter option. Input delimiter is specified by -d option and by default output delimiter is the same as input delimiter.
Lets first check out the output without using output delimiter as follows:
Now I have added —output-delimiter option and input delimiter colon (:) is replaced with output delimiter ‘SPACE» as follows:
Let’s check another example, here we use output delimiter to print on each field in a new line.
Here we use —output-delimiter as $’\n’ which indicates a new line.
Check the output:
How to cut by c haracters
The -c (column) option is used for cutting by character position. Remember that ‘TABS» and ‘Spaces’ are also treated as characters.
To print 1st character from each line from the file named content.txt, use below:
In the following example we will display character 1 through 7 (range) of each line from the file:
Let’s check how to select columns by either specific start or end position.
To extract columns from 2nd character to last character:
To extract columns from 1st character to 4th character:
How to cut by bytes
Use the -b option to select out a portion of a line by specifying a byte position by numbers separated by comma (,). Using hyphen you can specify a range of bytes.
The following example cut from 1st, 2nd and 3rd bytes from the file named ‘content.txt’:
We can also list with ranges using the below command:
Some practical cut examples
Cut is most practical used with a combination of different Linux or Unix commands.
For example, you want to extract ‘USER’, ‘PID’ and «COMMAND» using ps command:
Let’s take another example to extract ‘total’, ‘used’ and ‘free’ value of memory and save to a text file using multiple command:
Conclusion
The cut command can be piped with many other Linux or Unix commands. It can be piped with one or more filters for additional text processing.
One of the limitations of cut command is it doesn’t support specifying more than one character as a delimiter. Multiple blanks are counted as multiple field separators and have to use tr command before cut to get the desired output.
How to cut by c haracters
The -c (column) option is used for cutting by character position. Remember that ‘TABS» and ‘Spaces’ are also treated as characters.
To print 1st character from each line from the file named content.txt, use below:
In the following example we will display character 1 through 7 (range) of each line from the file:
Let’s check how to select columns by either specific start or end position.
To extract columns from 2nd character to last character:
To extract columns from 1st character to 4th character:
How to cut by bytes
Use the -b option to select out a portion of a line by specifying a byte position by numbers separated by comma (,). Using hyphen you can specify a range of bytes.
The following example cut from 1st, 2nd and 3rd bytes from the file named ‘content.txt’:
We can also list with ranges using the below command:
Some practical cut examples
Cut is most practical used with a combination of different Linux or Unix commands.
For example, you want to extract ‘USER’, ‘PID’ and «COMMAND» using ps command:
Let’s take another example to extract ‘total’, ‘used’ and ‘free’ value of memory and save to a text file using multiple command:
Conclusion
The cut command can be piped with many other Linux or Unix commands. It can be piped with one or more filters for additional text processing.
One of the limitations of cut command is it doesn’t support specifying more than one character as a delimiter. Multiple blanks are counted as multiple field separators and have to use tr command before cut to get the desired output.
Источник
Linux cut command
On Unix-like operating systems, the cut command removes («cuts out») sections of each line of a file or files.
This page covers the GNU/Linux version of cut.
Syntax
Options
-b, —bytes=LIST | Select only the bytes from each line as specified in LIST. LIST specifies a byte, a set of bytes, or a range of bytes; see Specifying LIST below. |
-c, —characters=LIST | Select only the characters from each line as specified in LIST. LIST specifies a character, a set of characters, or a range of characters; see Specifying LIST below. |
-d, —delimiter=DELIM | use character DELIM instead of a tab for the field delimiter. |
-f, —fields=LIST | select only these fields on each line; also print any line that contains no delimiter character, unless the -s option is specified. LIST specifies a field, a set of fields, or a range of fields; see Specifying LIST below. |
-n | This option is ignored, but is included for compatibility reasons. |
—complement | complement the set of selected bytes, characters or fields. |
-s, —only-delimited | do not print lines not containing delimiters. |
—output-delimiter=STRING | use STRING as the output delimiter string. The default is to use the input delimiter. |
—help | Display a help message and exit. |
—version | output version information and exit. |
Usage Notes
When invoking cut, use the -b, -c, or -f option, but only one of them.
If no FILE is specified, cut reads from the standard input.
Specifying LIST
Each LIST is made up of an integer, a range of integers, or multiple integer ranges separated by commas. Selected input is written in the same order that it is read, and is written to output exactly once. A range consists of:
N | the Nth byte, character, or field, counted from 1. |
N— | from the Nth byte, character, or field, to the end of the line. |
N—M | from the Nth to the Mth byte, character, or field (inclusive). |
—M | from the first to the Mth byte, character, or field. |
For example, let’s say you have a file named data.txt which contains the following text:
In this example, each of these words is separated by a tab character, not spaces. The tab character is the default delimiter of cut, so it will by default consider a field to be anything delimited by a tab.
To «cut» only the third field of each line, use the command:
. which will output the following:
If instead you want to «cut» only the second-through-fourth field of each line, use the command:
. which will output the following:
If you want to «cut» only the first-through-second and fourth-through-fifth field of each line (omitting the third field), use the command:
. which will output the following:
Or, let’s say you want the third field and every field after it, omitting the first two fields. In this case, you could use the command:
. which will output the following:
Specifying a range with LIST also applies to cutting characters (-c) or bytes (-b) from a line. For example, to output only the third-through-twelfth character of every line of data.txt, use the command:
. which will output the following:
Remember that the «space» in between each word is actually a single tab character, so both lines of output are displaying ten characters: eight alphanumeric characters and two tab characters. In other words, cut is omitting the first two characters of each line, counting tabs as one character each; outputting characters three through twelve, counting tabs as one character each; and omitting any characters after the twelfth.
Counting bytes instead of characters will result in the same output in this case, because in an ASCII-encoded text file, each character is represented by a single byte (eight bits) of data. So the command:
. will, for our file data.txt, produce exactly the same output:
Specifying A Delimiter Other Than Tab
The tab character is the default delimiter that cut uses to determine what constitutes a field. So, if your file’s fields are already delimited by tabs, you don’t need to specify a different delimiter character.
You can specify any character as the delimiter, however. For instance, the file /etc/passwd contains information about each user on the system, one user per line, and each information field is delimited by a colon («:«). For example, the line of /etc/passwd for the root user may look like this:
These fields contain the following information, in the following order, separated by a colon character:
- Username
- Password (shown as x if encrypted)
- User ID number (UID)
- Group ID number (GID)
- Comment field (used by the finger command)
- Home Directory
- Shell
The username is the first field on the line, so to display each username on the system, use the command:
. which will output, for example:
(There are many more user accounts on a typical system, including many accounts specific to system services, but for this example we will pretend there are only five users.)
The third field of each line in the /etc/passwd file is the UID (user ID number), so to display each username and user ID number, use the command:
. which will output the following, for example:
As you can see, the output will be delimited, by default, using the same delimiter character specified for the input. In this case, that’s the colon character («:«). You can specify a different delimiter for the input and output, however. So, if you wanted to run the previous command, but have the output delimited by a space, you could use the command:
But what if you want the output to be delimited by a tab? Specifying a tab character on the command line is a bit more complicated, because it is an unprintable character. To specify it on the command line, you must «protect» it from the shell. This is done differently depending on which shell you’re using, but in the Linux default shell (bash), you can specify the tab character with $’\t’. So the command:
. will output the following, for example:
Examples
Output the third character of every line of the file file.txt, omitting the others.
Output the first three characters of every line of the file file.txt, omitting the rest.
Same as the above command. Output the first three characters of every line of file.txt.
Output the third through the last characters of each line of the file file.txt, omitting the first two characters.
Output the first field of the file /etc/passwd, where fields are delimited by a colon (‘:‘). The first field of /etc/passwd is the username, so this command will output every username in the passwd file.
Output the first and sixth fields, delimited by a colon, of any entry in the /etc/passwd file which specifies /bin/bash as the login shell. This command will output the username and home directory of any user whose login shell is /bin/bash.
Related commands
grep — Filter text which matches a regular expression.
paste — Merge corresponding lines of files.
Источник
Linux Cut Command With Examples
Linux provides cut command for remove sections from each line of output in bash. cut command provides a mechanism to filter extract column/text from a file or standard output. Detailed examples can be found below. We have the following text file named fruits.txt
Example Text
Syntax
Select Column
Cut command provides the ability to select a specified column according to character number for all rows. Here we will print all rows 3 character line by line to the console. This will only print a single character.
Select Column
Select Column For Character Range
In the previous example, we have printed a single character to the terminal. We can also print multiple characters to the console by specifying the character range like below. We can see that the character index starts at 1.
Select Column For Character Range
Select Column Using Start Position
Another useful feature of cut is specifying only to start position up to end.
Select Column Using Start Position
Select Column Using End Position
We can print range from start to the specified position only specifying end position. This will assume start position as 1
Select Column Using End Position
Select Single Field
The field is some part of text delimited with specific characters like space, tab, double comma, etc. We can select text with field numbers. By default field delimiter is tab. Our text is already provided tab for delimitation. In this example, we will select 3. field.
Select Single Field
Select Multiple Fields
As we do in characters we can also specify field range. We will select fruit names and counts with the following command.
Select Multiple Fields
The following example selects more than one field one by one not using ranges.
Last Field
One of the most popular usages of cut is printing last field. The problem is that field count may change according to text and we can not specify the last field only using cut command. The following example uses rev command to get last field.
Last Field
Select Fields Include Specified Chars
By default cut do not have the ability to exclude lines according to characters but grep can be used easily to implement this feature. We will only select rows contain app
Select Fields Include Specified Chars
Select Fields Except for Specified Chars
We can reverse the previous example and remove unwanted rows. We will remove lines contains app and then print columns from 1 to 2 .
Select Fields Except for Specified Chars
Specify Delimiter
The delimiter is used to divide fields. By default, the tab is used as the delimiter. But it can be specified with -d option like below. Following the example, we use : as delimiter because the passwd file uses it. Then we only want to show user names that resides in field 1.
Specify Delimiter
Print Except Fields
As we see previously fields can be printed as ranges too. We can also print by excepting fields. This will print all renaming fields. Following the example, we can want to print all fields except 2.
Change Delimiter
Text may have delimiters by default. In some situations, we may want to change delimiter while printing to the console. —output-delimiter option can be used for this operation. The following example will change the tab delimiter to the comma.
Change Delimiter
Источник