sed, awk learning

One.n, N

next(n) output the contents of the pattern space (if there is -n when running sed, it will not be output), and then read the new input content.

Next(N) creates a multi-line pattern space by reading a new input line and adding it to the existing content of the pattern space. The initial content of the pattern space and the new input line are separated by a newline character. Newline characters embedded in the pattern space can be matched with the escape sequence'\n'. In the multi-line mode space, the metacharacter'^' matches the first note in the space, but not the character after the newline character. Similarly,'$' only matches the last newline character in the pattern space, not any embedded newline characters.

Control flow: continue to execute the actions after n/N after n/N, instead of going back to the beginning

Two.d,D

d Delete the contents of the pattern space and cause the newly read input line to re-execute the command at the top of the script.

D deletes this part of the pattern space up to the first embedded newline character. He does not cause a new input line to be read, he rematches the remaining content of the pattern space from the top of the script.

Control flow: back to the top of the script

Three.p,P

After the last command of the script is executed, the contents of the pattern space are automatically output (-n option or #n suppresses this default action) . Therefore, when the default output is suppressed or the control flow of the script is changed, and the bottom of the script has not been reached, the print command (P or p) needs to be used. Note that P or p does not change the contents of the pattern space.

p: print the contents of the pattern space

P: Output the first part of the multi-line pattern space until the first embedded newline character.

note:

For the last line to execute the N/n command, the command after N/n in the script will not be executed, so when the total number of lines is odd, the last line will not execute the command after N, but if you do not use -n to prohibit printing The contents of the pattern space will still print the contents of the last line. You can use $! N command, this command is meant , among other lines are the last line of execution N .

 

Awk has many built-in variables for setting environment information. These variables can be changed. Here are some of the most commonly used variables:

Number of ARGC command line parameters

ARGV command line parameter arrangement

ENVIRON supports the use of system environment variables in the queue

FILENAME The file name viewed by awk

FNR browse file number of records

FS sets the input domain separator, which is equivalent to the number of domains in the command line -F option NF browse record

NR number of records read

OFS output field separator

ORS output record separator

RS control record separator

Use space as a paragraph break:

awk 'BEGIN {RS=" ";}{print $0}' link.txt

Awk realizes that the line break in the text is a separator, and the output becomes a comma: (refer to https://blog.csdn.net/weixin_30817749/article/details/99331674 )

awk 'BEGIN{ORS=","}{print $0}' ldap_member.log

 

 

Guess you like

Origin blog.csdn.net/kh815/article/details/103973928