Source From Here
Preface
Linux command cut is used for text processing. You can use this command to extract portion of text from a file by selecting columns. This tutorial provides few practical examples of cut command that you can use in your day to day command line activities. For most of the example, we’ll be using the following test file.
10 Practical Linux Cut Command Examples
1. Select Column of Characters
To extract only a desired column from a file use -c option. The following example displays 2nd character from each line of a file test.txt
As seen above, the characters a, p, s are the second character from each line of the test.txt file.
2. Select Column of Characters using Range
Range of characters can also be extracted from a file by specifying start and end position delimited with -. The following example extracts first 3 characters of each line from a file called test.txt
3. Select Column of Characters using either Start or End Position
Either start position or end position can be passed to cut command with -c option. The following specifies only the start position before the ‘-‘. This example extracts from 3rd character to end of each line from test.txt file.
The following specifies only the end position after the ‘-‘. This example extracts 8 characters from the beginning of each line from test.txt file.
The entire line would get printed when you don’t specify a number before or after the ‘-‘ as shown below.
4. Select a Specific Field from a File
Instead of selecting x number of characters, if you like to extract a whole field, you can combine option -f and -d. The option -f specifies which field you want to extract, and the option -d specifies what is the field delimiter that is used in the input file. The following example displays only first field of each lines from /etc/passwd file using the field delimiter : (colon). In this case, the 1st field is the username:
5. Select Multiple Fields from a File
You can also extract more than one fields from a file or stdout. Below example displays username and home directory of users who has the login shell as “/bin/bash”.
To display the range of fields specify start field and end field as shown below. In this example, we are selecting field 1 through 4, 6 and 7:
6. Select Fields Only When a Line Contains the Delimiter
In our /etc/passwd example, if you pass a different delimiter other than : (colon), cut will just display the whole line. In the following example, we’ve specified the delimiter as | (pipe), and cut command simply displays the whole line, even when it doesn’t find any line that has | (pipe) as delimiter.
But, it is possible to filter and display only the lines that contains the specified delimiter using -s option. The following example doesn’t display any output, as the cut command didn’t find any lines that has | (pipe) as delimiter in the /etc/passwd file.
7. Select All Fields Except the Specified Fields
In order to complement the selection field list use option –complement. The following example displays all the fields from /etc/passwd file except field 7:
8. Change Output Delimiter for Display
By default the output delimiter is same as input delimiter that we specify in the cut -d option. To change the output delimiter use the option –output-delimiter as shown below. In this example, the input delimiter is : (colon), but the output delimiter is # (hash).
9. Change Output Delimiter to Newline
In this example, each and every field of the cut command output is displayed in a separate line. We still used –output-delimiter, but the value is $’\n’ which indicates that we should add a newline as the output delimiter.
10. Combine Cut with Other Unix Command Output
The power of cut command can be realized when you combine it with the stdout of some other Unix command. Once you master the basic usage of cut command that we’ve explained above, you can wisely use cut command to solve lot of your text manipulation requirements.
The following example indicates how you can extract only useful information from the ps command output. We also showed how we’ve filtered the output of ps command using grep and sed before the final output was given to cut command. Here, we’ve used cut option -d and -f which we’ve explained in the above examples.
Preface
Linux command cut is used for text processing. You can use this command to extract portion of text from a file by selecting columns. This tutorial provides few practical examples of cut command that you can use in your day to day command line activities. For most of the example, we’ll be using the following test file.
10 Practical Linux Cut Command Examples
1. Select Column of Characters
To extract only a desired column from a file use -c option. The following example displays 2nd character from each line of a file test.txt
As seen above, the characters a, p, s are the second character from each line of the test.txt file.
2. Select Column of Characters using Range
Range of characters can also be extracted from a file by specifying start and end position delimited with -. The following example extracts first 3 characters of each line from a file called test.txt
3. Select Column of Characters using either Start or End Position
Either start position or end position can be passed to cut command with -c option. The following specifies only the start position before the ‘-‘. This example extracts from 3rd character to end of each line from test.txt file.
The following specifies only the end position after the ‘-‘. This example extracts 8 characters from the beginning of each line from test.txt file.
The entire line would get printed when you don’t specify a number before or after the ‘-‘ as shown below.
4. Select a Specific Field from a File
Instead of selecting x number of characters, if you like to extract a whole field, you can combine option -f and -d. The option -f specifies which field you want to extract, and the option -d specifies what is the field delimiter that is used in the input file. The following example displays only first field of each lines from /etc/passwd file using the field delimiter : (colon). In this case, the 1st field is the username:
5. Select Multiple Fields from a File
You can also extract more than one fields from a file or stdout. Below example displays username and home directory of users who has the login shell as “/bin/bash”.
To display the range of fields specify start field and end field as shown below. In this example, we are selecting field 1 through 4, 6 and 7:
6. Select Fields Only When a Line Contains the Delimiter
In our /etc/passwd example, if you pass a different delimiter other than : (colon), cut will just display the whole line. In the following example, we’ve specified the delimiter as | (pipe), and cut command simply displays the whole line, even when it doesn’t find any line that has | (pipe) as delimiter.
But, it is possible to filter and display only the lines that contains the specified delimiter using -s option. The following example doesn’t display any output, as the cut command didn’t find any lines that has | (pipe) as delimiter in the /etc/passwd file.
7. Select All Fields Except the Specified Fields
In order to complement the selection field list use option –complement. The following example displays all the fields from /etc/passwd file except field 7:
8. Change Output Delimiter for Display
By default the output delimiter is same as input delimiter that we specify in the cut -d option. To change the output delimiter use the option –output-delimiter as shown below. In this example, the input delimiter is : (colon), but the output delimiter is # (hash).
9. Change Output Delimiter to Newline
In this example, each and every field of the cut command output is displayed in a separate line. We still used –output-delimiter, but the value is $’\n’ which indicates that we should add a newline as the output delimiter.
10. Combine Cut with Other Unix Command Output
The power of cut command can be realized when you combine it with the stdout of some other Unix command. Once you master the basic usage of cut command that we’ve explained above, you can wisely use cut command to solve lot of your text manipulation requirements.
The following example indicates how you can extract only useful information from the ps command output. We also showed how we’ve filtered the output of ps command using grep and sed before the final output was given to cut command. Here, we’ve used cut option -d and -f which we’ve explained in the above examples.