Languages
[Edit]
EN

Bash - cut examples

8 points
Created by:
Marcin
2477

Using cat method we are able to split rows to columns and select some of them.

Examples:
1. Opening passwd file and cutting first column by colon delimiter:

$ cut -d ':' -f 1 /etc/passwd

Note: columns counting from 1.

2. Example 1st with output redirection:

cat /etc/passwd | cut -d ':' -f 1

3. It is possible to select columns (1st and 2nd):

cat /etc/passwd | cut -d ':' -f 1,2

4. We can select column range (from 1st to 3rd):

cat /etc/passwd | cut -d ':' -f 1-3

5. We can select column range (from beginning to 3rd):

cat /etc/passwd | cut -d ':' -f -3

6. We can select column range (from 2nd to end):

cat /etc/passwd | cut -d ':' -f 2-

7. We can precise that we want to interpret only characters:

cut -c 2-7 /etc/passwd
cut -c -7 /etc/passwd
cut -c 2- /etc/passwd

 8. We can precise that we want to interpret bytes:

cut -b -10 /etc/passwd

 References:

  1. https://ss64.com/bash/cut.html
  2. https://en.wikipedia.org/wiki/Cut_(Unix)
Hey 👋
Would you like to know what we do?
  • Dirask is a friendly IT community for learners, professionals and hobbyists to share their knowledge and help each other in extraordinary easy way.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.