Basic use of sed and awk
In sed and awk, each directive consists of two parts: patterns and procedures. A pattern is a regular expression separated by a slash (/). The process formulates one or more actions that will be performed.
During execution, the first instruction in the script is read and the pattern of the current line is detected, and if there is no match, the process is ignored and the next instruction is read. Reads all instructions instead of reading the first instruction that matches the input line.
After all instructions are interpreted and applied to a single row, sed outputs the line and loops through each input row. awk does not automatically output lines, and the instructions in the script control what awk ultimately does.
Using SED
There are two ways to invoke sed: Specify edit instructions on the command line, or put them in a file and provide the name of the file.
Sed-e ' instruction ' file
[email protected] sedawk]# Cat list
John Daggett, 341 King Road, Plymouth MA
Alice Ford, East Broadway, Richmond VA
Orville Thomas, 11345 Oak Bridge Road, Tulsa OK
Terry Kalkas, 402 Lans Road, Beaver Falls PA
Eric Adams, Post Road, Sudbury MA
Hubert Sims, 328A Brook Road, Roanoke VA
Amy Wilde, 334 Bayshore Pkwy, Mountain View CA
Sal Carpenter, 6th Street,boston MA
[[Email protected] sedawk]# sed ' s/ma/, massachusetts/' list
To place a comma between the Boston and the MA, the instruction replaces the spaces in front of the two acronyms with a comma and a space.
There are 3 ways to specify multiple directives on the command line:
Separate the directives with semicolons.
Sed ' s/ma/, massachusetts/; s/pa/, pennsylvania/' list
Placing-e,-e before each instruction is a way for SED to recognize script commands.
Sed-e ' s/ma/, massachusetts/'-e ' s/pa/, pennsylvania/' list
Use the branch instruction function of the Bourne shell. A multiple-line input prompt (>) appears when you press return after entering a single quotation mark.
Sed '
> s/ma/, massachusetts/
>s/pa/, pennsylvania/' list
Script file:
It is impractical to enter a longer editing script on the command line. So you need to create a script file to put the SED command. Use-F to specify the name of the script file.
Sed-f scriptfile File
Prevent automatic display of input lines:
The default operation of SED is to output each input line, and-N to prevent automatic output. When you specify-N, to output a line, you need to print the command p.
Sed-n-E ' s/ma/massachusetts/p ' list
Summary of command-line options for SED:
-e Edit subsequent commands
-F Follow the file name in the script
-n block automatic output of input rows
Using awk:
Command-line Syntax:
awk ' instructions ' files
Directives must be enclosed in single quotes to differentiate them from the shell. You can enter multiple command lines in the same way as sed: separate commands with semicolons or use the multi-line input function of the borune shell.
Awk-f script files
Error message:
1. The entire process is not enclosed in curly braces
2. Do not enclose the instruction in single quotes
3. Do not enclose regular expressions in slashes//
Summary of options:
-F Follow Script text name
-F Changing field separators
-V Follow Var=vallue
Use both SED and awk:
[Email protected] sedawk]# vim namestate
s/ca/, california/
s/ma/, massachusetts/
s/ok/, oklahoma/
s/pa/, pennsylvania/
s/va/, virginia/
[Email protected] sedawk]# sed-f namestate list
John Daggett, 341 King Road, Plymouth, Massachusetts
Alice Ford, East Broadway, Richmond, Virginia
Orville Thomas, 11345 Oak Bridge Road, Tulsa, Oklahoma
Terry Kalkas, 402 Lans Road, Beaver Falls, Pennsylvania
Eric Adams, Post Road, Sudbury, Massachusetts
Hubert Sims, 328A Brook Road, Roanoke, Virginia
Amy Wilde, 334 Bayshore Pkwy, Mountain View, California
Sal Carpenter, 6th Street,boston, Massachusetts
[Email protected] sedawk]# sed-f namestate List | Awk-f, ' {print $4} '
Massachusetts
Virginia
Oklahoma
Pennsylvania
Massachusetts
Virginia
California
Massachusetts
Add:
S/regular/complex
Because no address is specified, it affects only the first occurrence on the current line. If regular is not found in the current row, an error occurs. To find multiple occurrences on the same line, you must specify G.
/regular/s/regular/complex/g
This command affects the first line in the file that matches this address. The first regular, is an address, the second is a pattern that matches the replacement command, to apply it to all rows, you must use the global command to place G before the address.
G/regular/s/regular/complex/g
The meaning of G is different, the first G is the global command, which means that all rows that match the address are changed, and the G at the end is a flag that changes all occurrences of the current row.
Sed&&awk Study notes (1)