[Reprint] Linux under the cut command usage Detailed

 

The following welcome to reprint, but please keep the author name and source, thank you! 

Original: frozen_sucker 

link: HTTP: // blog.csdn.net/Frozen_fish/archive/2008/04/08/2260804.aspx 

write their own good before now only used by others to build their own I tried it turned out very simple. very good.

 


Linux under the cut command usage Detailed
                                                           Original: frozen_sucker (popsicles)

Sometimes we often encounter such problems: there is a telephone directory, the above sequence regularly written names, home addresses, telephone numbers, notes, etc., at this time we just want to remove all the names and their corresponding phone numbers, there are several ways you can achieve it?




 

Indeed, it is difficult to achieve longitudinal positioning manner with a conventional approach, this time, it can flourish Cut.

 


 

What’s cut?

Master said: cut command extracts the text from a text file or column of the text stream.


 


 

Command usage:

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

       cut -c list [file ...]

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

 


 

l above -b, -c, -f respectively represent bytes, characters, fields (i.e., byte, character, field);

l list represents -b, -c, -f operating range, -n often expressed specific figures;

l file indicated by the name of the text file is natural to be operated;

l delim (all written in English: delimiter) representing the separator, by default is the TAB;

l -s indicates not include those rows contain delimiters (Notes This facilitates removing and title)

 


 

The above three methods, the extraction represents bytes (-b), or characters from the specified range (-C), or fields (-f).


 


 

Range representation:

N

Only item N

N-

ITEM N until the end of the line

N-M

From N to M Item items (including M)

-M

From the beginning of the line to the M-th item (including M)

-

All items from the beginning to the end of the line

 


 
Below is an example, the second to a relatively simple "command usage" mentioned start with:

 

 

interrupts file characters are arranged very neat, just right for us tofu.

 

 

 

But here we are only interested in two columns of numbers, is used as follows:

 

 


 


 


There are some unwanted content inside, streamlined look:

 


About the use of regular expressions, please refer to the relevant information on their own.

 

 

 

Fit together:

 

Wow, really enough handsome!


 


 

Do not cut adjacent columns of the election and how?

 

 

This requires that you determine how many characters occupy the position, not only cumbersome and error-prone.


 


 

The following questions how to do?

 

 

 

 

This is the second lecture: Use cut -f extract text field.

 

cut -c mainly used in a fixed location or number of characters in a text file to extract, for the above example becomes helpless. Careful observation, found passwd file has a law, that is a colon to distinguish between different sections of text, then. . .

 

 

How kind, fun it ~!


 


 

Continue, create a text file named a.txt, the name a little soil, make do with it.

 

 

TAB are separated between A1, B1, the line represents the characters C1, D1 but are separated by a space.

 

 

 

 

 

-S see the role of it? (Because the first row does not contain any TAB characters, directly excluded), and the last row (i.e., row D1), the pitch is separated by a space, it does not fit the requirements.

 

 

 

 


More parameters, which I did not say, as long as your IQ a little higher than Fan Wei, it certainly can guess it. ^ _ ^

 


 


Well, here is the last usage explained:

 

 

 

 

Because the virtual terminal can not display Chinese characters, so I had to return to the graphics, the screenshots will not become the white trash like you, Ren Ren, soon finished.


 

 

In this file, each with a half-width characters are separated by a space.

 

 

 

 

 

 

 

With cut -c has been successful, try the following how to cut -b?

 


No response, why?


   

The reason is that the characters themselves are double-byte, cut -c Chinese characters of "I" as a character to deal with, and cut -b in bytes to deal with, the "I" split into two bytes, the result is a character been "cut in half" and therefore can not be displayed properly.

 


 

The reason is found, to how do we do?

 

 

 

 

Yeah !!! get.

 


 

OK, all is done. Right here, rest, rest. . .
----------------
Disclaimer: This article is the original article CSDN bloggers "Frozen_fish", and follow CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement. .
Original link: https: //blog.csdn.net/frozen_fish/article/details/2260804

Guess you like

Origin www.cnblogs.com/jinanxiaolaohu/p/12409446.html