Awk
(1). Command Format
There are three methods:
Command Line Method:
Awk [-F domain separator] 'commands' input_file (s)
If the field separator is not specified, the default value is space. commands is an awk command statement.
- Awk Script Mode.
Awk command text:
Awk-F awk_script_file input_file (s)
(2) Composition of the awk statement
Awk statements are composed of modes and actions. The Mode determines when to perform the operation. The action is the specific content of the operation.
The mode can be any conditional statement, compound statement, or regular expression. There are two special modes: begin and end. Before any text browsing, end is used after Text Browsing.
The actual action is specified in {}, such as printing, complex, such as control and loop.
(3). domain and record
When awk is executed, Its browsing domain uses $1, $2 ,... , $ N ID. Multiple fields are separated by commas, for example, $1, $3.
$0 indicates the entire record (whole row ).
(4) Regular Expression in awk
In addition to the basic metacharacters, the following two characters can be used in the awk:
+: Matches one or more characters;
?, Occurrence Frequency of the matching mode.
(5). awk conditional Operators
Operator |
Description |
Operator |
Description |
< |
Less |
> = |
Greater than or equal |
<= |
Less than or equal |
~ |
Match Regular Expression |
= |
Equal |
!~ |
Do not match Regular Expression |
! = |
Not equal |
|
|
To match a regular expression, you can use ~ Followed by the/regular expression/, you can also use the if statement. The conditions after the if statement are enclosed.
For example:
Awk '{if ($4 ~ /Brown/) Print $0} 'grade.txt awk' $4 ~ /Brown/{print $0} 'grade.txt
(6) composite condition Operators
&: Both sides of the statement must be true.
|: The statement is true at the same time or on one side.
! : Inverse.
For example:
Awk '{if ($1 = "P. bunny "& $4 =" yellow ") Print $0} 'grade.txt awk' {if ($4 =" yellow "| $4 ~ /Brown/) Print $0} 'grade.txt
(7). awk built-in Variables
Argc |
Number of command line parameters |
Argv |
Command line parameter arrangement |
Environ |
Support the use of system environment variables in the queue |
Filename |
Name of the file awk is browsing |
FNR |
Number of browsing file records |
FS |
Set the input domain separator, which is equivalent to the command line-F Option |
NF |
Number of browsing records |
NR |
Number of read records |
OFS |
Output domain Separator |
ORS |
Output record Separator |
RS |
Control record Separator |
Method for accessing command line parameters: argv [N].
Access the system environment variable: Environ ["Editor"] = "Vi ".
FNR indicates the number of records for the current awk operation. Its value is less than or equal to Nr.
NF indicates the number of record domains. It is set after the record is read.
OFS allows you to specify the output domain separator. The default value is space.
ORS allows you to specify the output record delimiter. The default value is new (/N ).
RS is the record separator, and the default value is new line (/N ).
Example:
Number of records displayed:
Awk 'end {print Nr} 'grade.txt
The operation is performed only when the file is not empty:
Awk '{If (NR> 0 & $4 ~ /Bronw/) Print $0} 'grade.txt
Display the file name in the path:
Echo "/usr/Sybase/etc/rc. File" | awk-F/'{print $ NF }'
(8) Other awk Operators
= + =-= * =/= % = ^ = |
Value assignment operator |
? |
Conditional expression Operator |
+-*/% ^ |
Arithmetic Operators |
++ -- |
Prefix and suffix |
You can set variables in the awk. For example:
Awk '{name = $1; belts = $4;/If (belts ~ /Yellow/) Print name "is belt" belts} 'grade.txt
(9). awk built-in string functions
Gsub (R, S) |
Replace r with S in $0 |
Gsub (R, S, T) |
Replace r with S in T |
Index (S, T) |
Returns the first position of string t in S. |
Length (s) |
Returns the length of S. |
Match (S, R) |
Test whether S contains a string matching r. |
Split (s, A, FS) |
Divide s into Series A on FS |
Sprintf (FMT, exp) |
Returns the exp formatted by FMT. |
Sub (R, S) |
Replace r With the leftmost longest substring s in $0 |
Substr (S, P) |
Returns the suffix starting with P in string S. |
Substr (S, P, n) |
Returns the suffix of string s starting from P and ending with N. |
Example:
Awk 'gsub (/4842/, 4899) {print $0} 'grade.txt awk' begin {print index ("bunny", "NY ")} 'grade.txt awk' $1 = "J. troli "{print length ($1)" "$1} 'grade.txt awk' begin {print length (" A Good Man ")} 'awk' begin {print match ("ABCD",/D/)} 'awk' begin {print split ("123 #456 #678", myarray ,"#"); /print myarray [1]; print myarray [2]; print myarray [3])} 'awk "$1 =" J. troli "{sub (/26/," 29 ", $0); print $0} 'grade.txt awk '$1 =" L. tanskey "{print substr ($1, 1, 5)} 'grade.txt awk' {print substr ($1, 3)} 'grade.txt
(10). String shielding Sequence
/B Return key
/F go to the paper form
/N New Line
/R enter key
/T Tab key
/DDD octal value
/C any other special characters, such as // backslash
(11). awk output function printf
Basic Syntax: <p>
Printf ([format Control item], parameter)
Format control is similar to the printf function in C.
For example:
Awk 'begin{ printf "% F/N", 999} 'awk' begin {print "name/T/ts. number "} {printf" %-15 S % s/n ", $1, $3 }'
(12). Pass the value to awk in the command line
Format:
Awk command variable = input value.
For example:
DF-k | awk '($4 ~ /^ [0-9]/)/{if ($4 <trigger) print $6 "$4} 'trigger = 5600who | awk' {if ($1 = user) Print $1" connected to "$2} 'user = $ LOGNAME
(13). awk Array
You do not need to define arrays before using them. Generally, you can use loops to access arrays. Format:
For (element in array) print array [element]
Sed
(1). Command Format
There are three formats for calling sed:
Command line format:
Sed [Option] sed command input file
Use the SED script file:
Sed [Option]-F sed script file input file
Execute the SED script:
Sed script [Option] Input File
When no input file exists, sed receives the input from the standard input. Sed does not modify the original input file.
The SED command should be enclosed in single quotes.
Sed options:
N: No printing. Sed does not write the editing row to the standard output. By default, all rows are printed (edited and not edited ).
C: useless.
F: Call the script file.
(2). Sed text Locating Method
X |
X is a line number, such as 1 |
X, Y |
Indicates that the row number ranges from X to Y, such. |
/Pattern/ |
Query rows in the include Mode |
/Pattern/ |
Query rows that contain two modes |
Pattern/, X |
Query rows in the include mode on a given row number |
X,/pattern/ |
Query matched rows by row number and mode |
X, Y! |
Query rows that do not contain the specified row numbers x and y |
Regular expressions are used for sed mode matching.
(3). Basic sed Editing Command
P |
Print matching rows |
= |
Show file row number |
A/ |
Add new text information after locating the row number |
I/ |
Insert new text information after locating the row number |
D |
Delete row |
C/ |
Replace positioning text with new text |
S |
Replace the corresponding mode with the replacement mode. |
R |
Read text from another file |
W |
Write text to a file |
Q |
Exit or exit immediately after the first mode match is complete. |
L |
Display control characters equivalent to octal ASCII code |
{} |
Locate the Command Group that the row executes |
N |
Read the next line of text from another file and append it to the next line |
G |
Paste Mode 2 to/pattern N/ |
Y |
Transfer Character |
N |
Continues to the next input line to allow cross-row pattern matching. |
For example:
Display 1 to 3 lines of text:
Sed-n'1, 3 p 'quote.txt
Show matched rows:
Sed-n'/Neave/P' quote.txt
In the fourth row query mode:
Sed-n' 4,/the/P' quote.txt
Match metacharacters (meaning shielded by backslash ):
Sed-n' // $/P' quote.txt
Display the entire file:
Sed-N '1, $ P' quote.txt
Row number of the print mode match:
Sed-n'/music/= 'quote.txt
Delete matched text lines:
Sed '/Neave/d' quote.txt
Write/read from file:
Sed '/Neave/w dht' quote.txt sed '/Company/R sedex.txt' quote.txt
Exit after matching:
Sed '/. A. */Q' quote.txt
Display control characters in the file:
Sed-N '1, $ l' func.txt
(4) Additional Text
Operation format of the additional text ("/" in the additional text indicates the line break, and "/" is not added in the last line, indicating the end of the line ):
[Address] A/text/Text
Insert text and modify text.
(5) Replace text
Format:
[Address [, addresses] S/pattern-to_find/replace_pattern/[g p w n]
Replacement options:
G, which is the first occurrence by default. Use G to replace all occurrences.
P, print.
W file name. Write the output result to the file.
For example:
Sed-n's/night/P' quote.txt
You can use the (&) command to append or modify the original matching mode and save the discovery mode.
Change nurse to "hello" Nurse:
Sed-n's/nurse/"hello" &/P' quote.txt