Linux cut all fields

10 Practical Linux Cut Command Examples to Select File Columns

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.

$ cat test.txt cat command for file oriented operations. cp command for copy files or directories. ls command to list out files and directories with its attributes.

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

$ cut -c1-3 test.txt cat cp ls

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.

$ cut -c3- test.txt t command for file oriented operations. command for copy files or directories. command to list out files and directories with its attributes.

The following specifies only the end position after the ‘-‘. This example extracts 8 characters from the beginning of each line from test.txt file.

$ cut -c-8 test.txt cat comm cp comma ls comma

The entire line would get printed when you don’t specify a number before or after the ‘-‘ as shown below.

$ cut -c- test.txt cat command for file oriented operations. cp command for copy files or directories. ls command to list out files and directories with its attributes.

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.

Читайте также:  Stmicroelectronics virtual com port linux

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. The file

$ cut -d':' -f1 /etc/passwd root daemon bin sys sync games bala

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”.

$ grep "/bin/bash" /etc/passwd | cut -d':' -f1,6 root:/root bala:/home/bala

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

$ grep "/bin/bash" /etc/passwd | cut -d':' -f1-4,6,7 root:x:0:0:/root:/bin/bash bala:x:1000:1000:/home/bala:/bin/bash

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.

$ grep "/bin/bash" /etc/passwd | cut -d'|' -f1 root:x:0:0:root:/root:/bin/bash bala:x:1000:1000:bala. /home/bala:/bin/bash

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.

$ grep "/bin/bash" /etc/passwd | cut -d'|' -s -f1

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

$ grep "/bin/bash" /etc/passwd | cut -d':' --complement -s -f7 root:x:0:0:root:/root bala:x:1000:1000:bala. /home/bala

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).

$ grep "/bin/bash" /etc/passwd | cut -d':' -s -f1,6,7 --output-delimiter='#' root#/root#/bin/bash bala#/home/bala#/bin/bash

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.

$ grep bala /etc/passwd | cut -d':' -f1,6,7 --output-delimiter=$'\n' bala /home/bala /bin/bash

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.

Читайте также:  Linux mount write access

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.

$ ps axu | grep python | sed 's/\s\+/ /g' | cut -d' ' -f2,11- 2231 /usr/bin/python /usr/lib/unity-lens-video/unity-lens-video 2311 /usr/bin/python /usr/lib/unity-scope-video-remote/unity-scope-video-remote 2414 /usr/bin/python /usr/lib/ubuntuone-client/ubuntuone-syncdaemon 2463 /usr/bin/python /usr/lib/system-service/system-service-d 3274 grep --color=auto python

Linux cut Command with Useful Examples

Linux cut command is used for extracting file content on fields basis. text files do not have row/column like databases and some times we need the data of single column only. Cut identified columns on basis of separator (eg: colon ‘:’, semicolon ‘;’, comma ‘,’ etc).

For this example we are taking /etc/passwd file. All the rows are stored as below format with colon (:) separated like below. We use -f to specify field number and -d for delimiter (separator).

Linux-cut-command-example

As per above screen-cast this file has 7 fields. Cut also support to fetch values on character basis suing -c command line switch. Lets read below examples, for which I am using/etc/passwd file.

1. Select Single Field from File –

For example we need the list of usernames from our /etc/passwd file. We know that first column stored username, Entire file is separated by colon (:).

We can also use pipeline ” | ” for passing the file content as input to cut command, like below –

2. Select Multiple Columns from File –

We can specify multiple field names with command separated, like below example will show the 1’st, 2’nd and 7’th fields only.

We can also specify the range of columns with hyphen (-) on fields as well as both option’s together like below example commands.

    • Here first command will select 1’st, 2’nd,3’rd and 4’th fields.
    • Second command will select 3’rd, 4’th and 5’th fields.
    • Last command will show 2’nd, 3’rd, 4’th, 6’th and 10’th fields.

    To get values of all columns except one use following command. For example if we need to select all columns but not 6.

    3. Selecting Single Character’s from File –

    Except fields we can also select values from file on basis of single characters, while using characters we don’t need to specify separator.

    Similarly fields we can also specify multiple comma separated characters or range of characters.

    Источник

    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

    apple 1 good grape 5 bad banana 2 not bad

    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.

    Select Multiple Fields

    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.

    $ cat fruits.txt | rev | cut -f1 | rev

    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

    $ grep 'app' fruits.txt | cut -f1-2

    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 .

    $ grep -v 'app' fruits.txt | cut -f1-2

    Select Fields Except 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

    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.

    $ cut -f1,2,3 --output-delimiter=',' fruits.txt

    Источник

Оцените статью
Adblock
detector