Linux Chinese text processing tool cut command details

  • 2020-05-30 21:44:04
  • OfStack

preface

The field or range specified in the Cut input character. If you are dealing with fields, the fields are separated by the delimiter, and the fields are separated by the given delimiter on output. The default delimiter is the TAB character (TAB). Let's take a look at the details.

1. cut actual combat training

cut txt file


root,x,0,0,root,/root,/bin/bash
daemon,x,1,1,daemon,/usr/sbin,/usr/sbin/nologin
bin,x,2,2,bin,/bin,/usr/sbin/nologin
sys,x,3,3,sys,/dev,/usr/sbin/nologin

We practice

Output the second byte content per line


> root@wing:~/wing # cut -b 2 cut.txt 
> o
> a
> i
> y
>
>

Output the second character content for each line


> root@wing:~/wing # cut -c 2 cut.txt 
> o
> a
> i
> y
>
>

Output the contents of column 1 for each row


> root@wing:~/wing # cut -d , -f 1 cut.txt 
> root
> daemon
> bin
> sys
>
>

Output the contents of column 1.2 for each row


> root@wing:~/wing # cut -d , -f 1,2 cut.txt 
> root,x
> daemon,x
> bin,x
> sys,x
>

2. Details of cut common parameters

- b � bytes

Select the list of bytes, that is, select the N byte of each line.

- c � characters

Select a list of characters, i.e. select the N character for each. (there is no difference between English characters and -b, but in Chinese characters, a Chinese character takes up 2-3 bytes, so -c is more likely to be used when there is a Chinese character).

- d � delimiter

Delimiter, TAB by default.

- f � field

Select the list of columns, that is, select column N for each row.

conclusion


Related articles: