Regular four

==============================

00. Course Review described
trio command dick: Sed
. 1) Overview Description
a good on the row
b good edit modified file (backward reference forward)

command execution process 2)
1. Press line reading file content
2. each line is read out, the storage mode space
3. Verify that matches the needs of
the matches: CRUD processing operation
does not match: the default output line content, and then read the next line
PS: I do not want information about the default output -n cancel the default output
4. When a processing operation is completed, to the next line

3) demonstration on the command
query matching
a line number to match
sed -n '3p' information file
1) for matching a plurality of rows (continuous)
Sed -n '3,5p' information file
2) for matching a plurality of rows (not continuous)
Sed -n '3P; 5P' document information
b are matched according to the character
sed -n '/ oldboy / p' file information
1) for matching a plurality of rows (continuous)
Sed -n '/ Oldboy /, / oldgirl / p 'file information
2) a multiple-row match (discontinuous)
Sed -n' / Oldboy / P; / oldgirl / p 'file information
PS: when query information If positive, the symbol, for an extended regular symbol (-r)

add the information
later a further row add information
sed '3a oldboy' file information
sed '/ oldboy / a oldboy100' document information
preceding b further line to add information
sed '3i oldboy' file information
sed '/ oldboy / i oldboy100' file information
PS: If you want to add a multi-line
sed '3a oldboy03; 4a oldboy04'Wrong information file
sed -e '3a oldboy03' -e ' 4a oldboy04' correct file information

Removal information
by deleting the row data
'3d' sed information file
to delete data in the character information
sed '/ oldboy / d' file information
PS: At this deletion is only simulated for deletion -i required parameter

modification information
a whole row single line edit
sed '3c oldboy01' document information

b modified by the line part of line
sed '3s # # <\ 1 > #g (*.)' file information
PS: in this case only the modified analog modifications, changes need the real use -i parameter

to really need to edit the file using the command sed -i parameter
Caution:
when a re-use -i parameter, the parameters on all parameters back again after -i parameter to continue writing the backup .bak
b when re-using the -i parameter, be sure not to use in conjunction with the -n option

4) sed command Exercises:
1) How to modify the batch file name extension
2) How to batch create users and set random passwords

01. enterprise needs test:
1) how to remove the IP address information (grep sed)
Method One: Using sed
first course: how to get the IP address
ip address show eth0

Second course: the line where the IP address filtering out
ip address show eth0 | sed -n ' 3p'

third course: no information line for deletion
ip address show eth0 | sed -n ' 3p' | sed -r 's # ^ * net # \ 1 # g. (*.)' | sed -r '(. *). s # / 24 * $ # \ 1 # g'
taken before the IP address information of the IP address taken after the information
command to optimize
ip address show eth0 | sed -n ' 3p' | sed -r '. (. *). s # ^ * net / 24 * $ # \ 1 # g'
ultimate optimization
ip address show eth0 | sed -NR '3S # ^. * NET (. *) / 24. * $ # \. 1 # GP'

Sed -n '. 3 P'
Sed -R & lt 'S # ^. * NET (. *) / 24. * $ # \ # 1 G '
.. sed -nr' 3S # ^ * NET (. *) / 24-* $ # \ # 1 gp '


method 2: Using grep
The first course: how to get the IP address
ip address show eth0

first two course: the line where the IP address filtering out
ip address show eth0 | grep "inet "

third course:Remove the IP address
ip address show eth0 | grep "inet " | grep -E ". [0-9] + \ [0-9] + \ [0-9] + \ [0-9] +.."

Command to optimize the
ip address show eth0 | grep "inet " | grep -E "([0-9] + \.) {3} [0-9] +"
ultimate optimization
ip address show eth0 | grep "inet " | grep - E "([0-9] + \.?). 3 {}"
IP address Show eth0 | grep "inet" | grep -E "([0-9] + \.?). 4 {}" -o | head -1

method three: using the IP address extracted awk

02. trio command - awk boss
concept Description:
a good operation process of the column
b good statistical analysis of the data file

03. the method of awk command processing contents of the file operation
1) query operation
2) excluding the operation
3) replace operation
4) statistical operations

04. awk command format and operating principle
Format:
Sed [parameters] 'instruction information' file information
awk [Option] 'operation mode {}' [File Information]
Mode: matching condition / oldboy /

05. awk during practical operation:
generating a simulation environment
CAT >> ~ / reg.txt the EOF <<
Zhang Dandan, 41,117,397: 250: 100: 175
Zhang Xiaoyu 390 320 151: 155: 90: 201
Meng Feixue 80,042,789: 250: 60: 50
Wu Waiwai 70,271,111: 250: 80: 75
Liu Bingbing 41,117,483: 250: 100: 175
Wang Xiaoai 3515064655: 50: 95: 135
Zi Gege 1,986,787,350: 250: 168: 200 is
of Li Youjiu 918 391 635: 175: 75: 300
Lao Nanhai 918 391 635: 250: 100 : 175
the EOF
taken to complete the test mode needs
01. xiaoyu displayed last name and ID number of
the first course: information on demand to find qualified
lookup row
awk 'NR 2 ==' ~ / reg.txt
[@ Oldboy the root-Xiaodao ~. Com.cn] # awk 'NR 2 ==' ~ / reg.txt
Zhang Xiaoyu 390 320 151: 155: 90: 201
[[email protected] ~] # awk '{Print 2 NR == $ 0 } '~ / reg.txt
Zhang Xiaoyu 390320151: 155: 90: 201

characters to find
~ representation in columns match
~ case represents exclude columns match!
Awk '/ Xiaoyu /' ~ / reg.txt error
awk '$ 2 ~ / Xiaoyu / ' ~ / reg.txt columns match

Second Process: processing information satisfying the condition
[[email protected] ~] # awk 'NR == 2. 1 {Print $, $. 3}' ~ / reg.txt
Zhang 390 320 151
[@ Oldboy-Xiaodao the root. ~ com.cn] # awk 'NR == {Print $ 2. 1 "" $. 3}' ~ / reg.txt
Zhang 390 320 151

[[email protected] ~] # awk '$ 2 ~ / Xiaoyu / {Print $. 1 "" $ 3} '~ / reg.txt
Zhang 390 320 151

02. surnames zhang man, showing his second donation amount and the name of his
first course: on demand to find qualified information
to locate the character
[root @ oldboy- ~ xiaodao.com.cn] # awk '$ ~. 1 / Zhang /' reg.txt
Zhang Dandan, 41,117,397: 250: 100: 175
Zhang Xiaoyu 390 320 151: 155: 90: 201
[[email protected] ~] # awk '/ ^ Zhang /' reg.txt
Zhang Dandan, 41,117,397: 250: 100: 175
Zhang Xiaoyu 390 320 151: 155: 90: 201

second Process:Processed information satisfies the condition
[[email protected] ~] # awk '/ ^ Zhang / {Print $ 2, $ 4}' reg.txt
Dandan,: 250: 100: 175
Xiaoyu: 155: 90: 201
can not be removed in the fourth column part
[[email protected] ~] # awk -F "[:] +" '/ ^ Zhang / {Print $ 2, $}. 5' reg.txt
Dandan, 100
Xiaoyu 90
[@ Oldboy the root-Xiaodao. ~ com.cn] # awk -F "[:] +" '/ ^ Zhang / {Print $ 2, $ (of NF-. 1)}' reg.txt
Dandan, 100
Xiaoyu 90

awk parameters:
the condition information for designating awk -F cut column
using a [] + cut may be a plurality of successive characters as a whole integrated

03. show all 41 human begin full name and ID number of the ID number
of the first course : demand information to find qualified
[[email protected] ~] # awk '$ ~. 3 / ^ 41 is /' reg.txt
Zhang Dandan, 41,117,397: 250: 100: 175
Liu Bingbing 41,117,483: 250: 100: 175

second course: processing information satisfying the condition
[[email protected] ~] # awk '$ ~. 3 / ^ 41 is / {. 1 Print $, $ 2, $}. 3' reg.txt
Zhang Dandan, 41,117,397
Liu Bingbing 41,117,483


04. Display all last digit ID number is the full name of a person or 5
first course: find information on qualified according to the requirements
[[email protected] ~] # awk '$ 3 ~ / [15 ] $ / 'reg.txt
Zhang Xiaoyu 390 320 151: 155: 90: 201
Wu Waiwai 70,271,111: 250: 80: 75
Wang Xiaoai 3515064655: 50: 95: 135
of Li Youjiu 918 391 635: 175: 75: 300
Lao Nanhai 918 391 635: 250: 100 : 175

second process: processing information satisfying the condition
[[email protected] ~] # awk '$ ~. 3 / [15] $ / $ {Print. 1, 2} $' reg.txt
Zhang Xiaoyu
Wu Waiwai
Wang Xiaoai
of Li Youjiu
Lao Nanhai

05. contributions Xiaoyu the display, each time there begins with $, such as $ 110 $ 220 $ 330
the first course: find qualified information on demand
[[email protected] ~] # awk '$ 2 ~ / Xiaoyu /' reg.txt
Zhang Xiaoyu 390 320 151: 155: 90: 201

Second Process: processing information satisfying the condition
[[email protected] ~] # awk '$ 2 ~ / Xiaoyu / Print $ {}. 4' reg.txt
: 155: 90: 201

[@ Oldboy the root-Xiaodao ~. Com.cn] # awk '$ 2 ~ / Xiaoyu / {gsub (/: /, "$", $. 4);}. 4 Print $' reg.txt
$ 155 $ 201 $ 90

awk alternative test command
[[email protected] ~] # echo: 155: 90: 201 | awk '{gsub (/: /, "$", $. 1); Print. 1} $'
$ 155 $ 201 $ 90

Added: replacement information syntax awk
awk '{gsub (//, "", $ n-); n-Print $}'
/ modified information to be replaced /, 'modified to what information', the information needs to be modified in the column
/: /, "$",

06. awk special mode described
the BEGIN {}
. 1) prior to re-file operation, perform a corresponding operation action
practical application: an output header information of each column
2) calculates the values
[[email protected]. ~ CN] # awk 'the BEGIN {Print. 3 + 2}'
. 5
[[email protected] ~] # awk 'Print the BEGIN {3-2}'
. 1
[[email protected] ~ ] # awk '. 3 * Print the BEGIN {2}'
. 6
[[email protected] ~] # awk 'Print the BEGIN {}] 3/2'
for 1.5
[[email protected] ~] # awk 'Print the BEGIN {2}. 3 ^'
. 9
[[email protected] ~] # awk 'Print the BEGIN {2}. 3%'
. 1
[[email protected] ~]# awk 'BEGIN{print 5%3}'
2
[[email protected] ~] # awk '. 5 ** Print the BEGIN {}. 3'
125
[[email protected] ~] # awk '2 ** Print the BEGIN {}. 3'
. 8
[[email protected] ~] # awk 'Print the BEGIN {2}. 3 ^'
. 8

. 3) built-in variable-modified ???


the END {}
. 1) again after the file operation, perform a corresponding action operation
practical application: using The final result to the output information ???


07. awk analysis processing of data file information exercise
1) statistics / etc / services file hollow number of rows
of a process: how to accumulate operations
[[email protected] ~] = I. 1 #
[[email protected] ~] # awk 'the BEGIN {I = I +. 1;} I Print'
. 1
[[email protected] ~] # = awk. 1 -vi 'the BEGIN {I = I +. 1;} I Print'
2
[[email protected] ~] -vi awk # 2 = 'the BEGIN {I = I +. 1;} I Print'
. 3
Description:
A awk -v parameter can be set variables
c awk no need to include the transfer of the variable $
D awk will default variable character as you do not want retrieved is identified as the variable double quotes for
e awk accumulates calculation equation i = i + 1

The second process: The number of empty find out, accumulate operation
[[email protected] ~] # awk '/ ^ $ / {I = I +. 1; Print I}' / etc / Services
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
. 17
just want to see the final result
[[email protected] ~] # awk '/ ^ $ / {i = i + 1} END {print i } '/ etc / Services
. 17

02. statistics number of virtual users / etc / passwd file has
the statistics file nologin all ending in a row, total number of
[[email protected] ~] # awk '/ nologin $ / {i = i Print the END {I} + 1'd} '/ etc / the passwd
18 is
[[email protected] ~] # awk' / nologin $ / {I} ++ Print the END {I} '/ etc / the passwd
18 is

03. Direct complete summation

[[email protected] ~]# seq 10
1
2
3
4
5
6
7
8
9
10
[root@oldboy-xiaodao.com.cn ~]# seq 10|awk '{i=i+$1;print i}'
1
3
6
10
15
21
28
36
45
55
[root@oldboy-xiaodao.com.cn ~]# seq 10|awk '{i=i+$1}END{print i}'
55
[root@oldboy-xiaodao.com.cn ~]# seq 10|awk '{i+=$1}END{print i}'
55

 

Homework:
01. According secure file, if the IP address from which the statistics the number of failures up to
show the failed IP address show how many times fail
to display the order before 10
(awk array)

Guess you like

Origin www.cnblogs.com/nsh123/p/12158727.html