Talking from a metamorphosis command to several common UNIX tools.

xiaoxiao2021-03-05  30

Talking from a metamorphosis command to several common UNIX tools. Flydragon http://www.cnfreeos.org

Before the beginning, introduce you to the "UNIX and Shell Program Design Authoritative Tutorial", which is a classic textbook for foreign computer science. I am from this

I have learned a lot of interesting things, I hope to learn the system management friends also look. (Non-advertising, just I personally think it is good)

The following command is not the easiest way, where you are here to introduce some of the usage of several common tools. hope to

I am aroused the inspiration of everyone, I'm going to solve the problem around you, I don't understand the shell, please don't mess with the brick.

Wine bottle.

GREP -V ^ $ A.TXT | AWK '{Print $ 1}' | cut -f1-3 -d. | SED 'S /^.*! "0 192 / .168 /. 0 / .1 / '| UNIQ

There is such a tablet file a.txt:

60.48.0.0 - 60.54.255.255 Malaysia Telecom

60.160.0.0 - 60.160.223.255 Yunnan Telecom

60.160.224.0 - 60.160.225.255 Wenshan Telecom, Yunnan Province

60.160.226.0 - 60.161.255.255 Yunnan Telecom

60.162.0 - 60.162.0.255 Taizhou Luqiao District Telecom ADSL

60.162.1.0 - 60.162.51.255 Zhejiang Telecom

60.162.52.0 - 60.162.52.255 Zhejiang Linhai Telecom

60.162.53.0 - 60.162.79.255 Zhejiang Telecom

60.162.80.0 - 60.162.91.255 Taizhou City, Zhejiang Province (Tiantai) Telecom

60.162.92.0 - 60.162.127.255 Taizhou Telecom, Zhejiang Province

60.162.128.0 - 60.163.0.255 Zhejiang Telecom

60.163.1.0 - 60.163.1.255 Jiaxing Telecom, Zhejiang Province

60.163.2.0 - 60.163.4.255 Zhejiang Telecom

60.163.5.0 - 60.163.5.255 Jiaxing Telecom, Zhejiang Province

60.163.6.0 - 60.163.13.255 Zhejiang Telecom

61.128.252.77 - 61.128.252.77 Chongqing Yongchuan Telecom non-residential broadband users

61.128.252.79 - 61.128.252.79 Chongqing Yongchuan Telecom non-residential broadband users

61.128.252.83 - 61.128.252.83 Chongqing Yongchuan Telecom non-residential broadband users

61.128.252.85 - 61.128.252.85 Chongqing Yongchuan Telecom non-residential broadband users

61.128.252.92 - 61.128.252.92 Chongqing Yongchuan Telecom non-residential broadband users

61.128.252.94 - 61.128.252.94 Chongqing Yongchuan Telecom Non-residential Broadband User 61.128.252.96 - 61.128.252.96 Chongqing Yongchuan Telecom Non-residential Broadband User

In a brief description, this is a list of IP lists from some areas from the FreeBSDCHINA Forum, so that everyone will do strategy routing, according to the purpose

The service provider of the standard network selects the path, access the telecommunications's line, and access Netcom's IP walk Netcom.

The IP in the above file is some telecommunications service providers, that is to say to make these IP sections into n static routes, need to be similar to this:

Route Add -Net 60.48.0.0 192.168.0.1

Route Add -Net 60.160.224.0 192.168.0.1

........

Let's take a step to see what the long list is done.

GREP -V ^ $ A.TXT

GREP is one of the classic orders, he means "Global Regular Expression Print" is a program family.

It can match all rows of the specified regular expression in the input file and write the output to the standard output file (display).

(General expression format See Man 7 Re_Format),

Its command format is:

GREP option regular expression file list

The common options have the following:

-i ignores case

-n print line number

-v printing mismatches (exclude RE)

-f file expression in the file

The above command, the parameter is -V regular expression is "^ $" is also empty, through this command we exclude several space lines in the middle.

.

| Is a pipe operation symbol, the pipeline is used in the UNIX system, and the pipeline will save the output of a command to the temporary buffer, then the buffer

The content is made as the input of the next command, and the pipe has a use requirement, that is, the front command must be able to send output to standard output devices, follow

The command must be able to read the required input from the standard input device. Due to various characteristics, the pipeline is usually means to avoid creating an intermediate file.

AWK '{Print $ 1}'

The AWK name comes from its founder (Alfred v.aho, perter j.weinberger and brian w.kernighan), which compared to other tools.

Point is powerful. That's right, it is originally a programming language that disguise into the usage program. AWK and SED work are similar, it is from the input file

Read, and implement an action in the entire line or part. But different from the SED unless you are clearly specified, AWK will not print.

As mentioned, AWK is too powerful, including features that are not supported by other tools such as arrays, functions, processes, we are not intended

Here more awk, learn awk's good book Oreilly's Sed & Awk. Go back, just the command is awk shell program

Typical "column print", awk looks a set of fields and records, and the AWK conventions use one or more spaces or other words defined.

The inquiry segmentation field, at the same time, AWK looks into a record, so the command just draws, IP from the last result.

Address section.

The pipe operator continues to direct our output to the input below.

Cut -f1-3 -d. (pay attention to there is a point)

CUT is a filter command, which is a type of command, and Cut deletes columns from the file, Paste connection column

Its command format is: CUT Options File List

Common options have -c (specified character position), -f specified area, -d redefines the segmentation symbol, and the output is suppressed when there is no division in the line.

After 2 operations, our IP address file has only one column left:

60.160.224.0

...

60.163.2.0

...

61.128.252.96

Our CUT command, -d specifies the use of some splitters, and divides the area of ​​a block, and we use -f to specify the contents of the output area 1 to 3.

That is, the last side of the IP address is removed, and we do this to convert the IP of the network into IP. In fact, this is enough.

The SED operations that do not do will also represent the network in the route. But we still come to a snake to add enough, taking this command that is familiar with the regular expression:

Save, rearward, repeat, anchor, point, buffer, etc., and the basic replacement of the SED, and atomic replacement operations.

Regular expressions are one of the unix, which is one of the other operating systems, it is too cool, efficient ways to express strings, is proud of the master

The capital, the character sequence of chaos, but let beginners and foreign lines are confused, even cold and chestnuts. Familiar with and use regular expressions, you need you very much

Many practices and exercise your little smart and lazy habits.

Due to the limited space, we don't know how to explain, please readers yourself Man Re_Format.

Our regular expressions supported by SED, SED supports the character, point, class, anchor, sequence, backward reference, repetition, and saving of regular expressions.

Single characters, points, classes, anchors, and backward references constitute the atoms of the regular expression, the atom is used to match the text content and discover the location of the text.

Character, it is easy to understand, any single character is a simplest RE that matches this determined character.

Point, is an English punctuation, he matches any single word except / N, which is so big, so that he can not do itself.

However, when it combines with other elements, you can play a powerful expression. Everyone is not difficult to understand A.B can replace AAB, ABB, ACB, A2B ....

Class, define an ASCII collection, as long as one of the elements matches the text, then the text satisfies this collection of RE, that is

Say [ABC] This collection contains A, B, C three, three characters are met, and there are common 2 symbols, one is a dash "-"

Indicates a continuous space, such as [A-Z], [A-Z], [0-9], etc., the other is not "^" used to represent the remaining set, such as [^ aeiou] means any non-element

Tone character

Anchor, generally used to do alternate, only 4 anchors ^ represents the head, $ line, / word end, note, anchor match is aligned,

It is not visible, generally used in combination with other atoms, ^ a (beginning with uppercase letters A), 8 $ with the number 8 end, E /> Words ended with E

.

Backward reference, the text can be saved in 9 buffers, and after being used, it provides a method of removing buffer text. Use backward

You need to "save" before you use it. Rear reference uses essential characters / and 1 to 9, which is used in conjunction with the 9 digits. For example, / 2/3

Remove the contents of the 2nd and 3 buffers.

In order to make RE more powerful, the atoms and operators can be combined. Regular expressions are sequences, replace, repeat, group, and save a total of 5 operators.

Sequence is an air operator that represents a sequence operator in the middle, such as character sequence ABC, or A4S2, where the primary sequence ABC, or A4S2 occurs.

Repeat, is a collection of parentheses / {m, n /}, which contains two numbers M and N, separated by commas, M represents at least, N represents up to, of course, can only be only

A number of numbers, or a number of numbers, such as / {3 /} Repeat 3 times, / {3, 6 /} Repeated 3 to 6 times, / {3, /} no less than 3 times, smart you may

Immediately think of / {, 4 /} to represent up to 4 times, yes, it is so simple.

Save, this operation can put the text in nine buffers, its symbol is / (/) two exact parentheses to place the content that needs to be saved, according to

According to the sequence, the first preserved text will be placed to the buffer 1, and push it according to the class.

To simplify RE, there are some special forms of abbreviation operation symbols, such as * matches the front atoms once or more, match the front atom 0 or 1 time

Assume that the file is 1.txt has the following content

1234567890

1234567890

Then following this long order

SED 'S / / ([0-9] / {3 /} / ([0-9] / {4 /} / ([0-9] / {3 /} // 1- / 3- / 2 / '1.txt

Will help you format it into this form:

123-890-4567

123-890-4567

This command shows us the basic usage of the SED's alternate command S:

S / mode / replacement /

Look at the order above

S / / ([0-9] / {3 /} / ([0-9] / {4 /} / ([0-9] / {3 /} // 1- / 2 - / 3 /

Although it is very long, it is not difficult to disassemble it according to the knowledge we have just learned.

/ ([0-9] / {3 /} / ([0-9] / {4 /} / ([0-9] / {3 /} /) This string expresses a RE, and Save to 3 buffers.

/ ([0-9] / {3 /} /) describes such a sequence "Exact repeated 3 times from 0-9 characters, which is a sequence of three numbers"

We look at the replacement, this is a string that is removed and re-composed by the buffer.

/ 1- / 3- / 2 is added by the buffer 1 - followed by the buffer 3 and then add-to the buffer 2, and the three buffers are recombined after the new replacement sequence.

Let's take a look at the commands we say:

Sed 's /^.*! # 0 192 / .168 / .0 / .1 /'

This is also an example of reformatting, and replacing. & Operation symbols will represent the "mode" in front of the "mode" ^. * $ Is all the lines

. Among the replacement, some special characters need to use escape symbols / to block the original meaning.

The above command is understood as: "At each IP address plus" Route Add -Net "and then keeps with this IP address, finally 192.168.0.1

end. So it is satisfied

Route Add -Net 60.48.0.0 192.168.0.1

Route Add -Net 60.160.224.0 192.168.0.1

Such a command format

Take a closer look, you may have a lot of repetitions, these are some of the fixed IP instead of the network filtered, we can

Use UNIQ to eliminate it, UNIQ is also a filter, which deletes the repeated row.

Finally, I finished, so tired, I hope to be a bit used for everyone.

This page uses VIM editing

Another method is to do with the reply-to / route-to-TO of PF.

转载请注明原文地址:https://www.9cbs.com/read-35387.html

New Post(0)