Usage Guide for Extracting Text Using the Cut Command in Linux

  

Sometimes we often encounter problems such as having a page number and a regular, regular name, home address, phone number, notes, etc. We only want to take out the names of everyone and their corresponding phone numbers. How many ways can you do this?

It is true that this vertical positioning method is difficult to implement in the conventional way. At this time, cut can be used. It is.

What’s cut?

Child: The cut command extracts text columns from a text file or text stream.

Command Usage:

cut -b list [-n] [file ...]

cut -c list [file ...]

cut -f list [-d delim][-s][file ...]

Parameters:

-b, --bytes=LIST

Output these Bytes

-c, --characters=LIST

Output these characters

-d, --delimiter=DELIM

Use DELIM instead of TAB Field separator

-f, --fields=LIST

Output these fields

-n

(ignore)

-s, --only-delimited

Do not display rows without separators

--output-delimiter=STRING

Use STRING as output separator, missing (output separator) is the input separator

--help

Display help information, then end

--version

Display version information , then end

using and only use one of -b, -c or -f. LIST is separated by a range or comma The range is composed. The range is one of the following forms:

N

The Nth byte, character or field, starting from 1 count

N-

From Nth byte, character or field up to end of line

NM

From Nth to Mth (and including Mth byte), character or field

-M

From 1st to the Mth (and including the Mth) bytes, characters or fields

If the file FILE is not specified, or FILE is -, it is from the standard Enter the read data.

Example:

The code is as follows:

[root@localhost ~]# cat /etc/passwd |  Cut -b 1 | Head -5 # Output the first byte of the file

r

b

d

a

l

The code is as follows:

[root@localhost ~]# cat /etc/passwd |  Cut -c 1-4 | Head -5 # Output the first four characters of the file

root

bin:

daem

adm:

lp:x

The code is as follows:

[root@localhost ~]# cat /etc/passwd |  Cut -f1 -d ':' | Head -5 #:: split the file, output the first field

root

bin

daemon

adm

lp< Br>

The code is as follows:

# cat a.txt

ssss affff dddd fe fsc

rrr f adfa eer ddd

The code is as follows:

# cat a.txt | Cut -f1,3 -d $'\\t' #1,3 columns

ssss dddd

rrr adfa

Copyright © Windows knowledge All Rights Reserved