After awk and sed are sorted out, sed and awk take notes for me.
Sed command:
Format: sed-e's/aaa/bbb/g'-n-e '/XXX/p'-e'/YYY/d' filename
Command parameters:
-E: execute multiple SED commands.
S replacement
G full text
-N: the filtered part. Otherwise, the original text is displayed again.
P: print
D. delete
/Convert metacharacters ($,/.,/^ ,//)
Regular table format:
/^ $/Empty rows
/^. */A whole line
/*/One or more spaces
/[A-zA-Z] [a-zA-Z] *: W [a-zA-Z0-9] [a-zA-Z0-9/.] *. */valid URL
Example:
$ Cat tt
Aaa XXX
Aaa YYY
Bbb XXX
Bbb YYY
$ Sed-e's/aaa/bbb/g'-n-e '/XXX/p'-e'/YYY/d' tt
Bbb XXX
Bbb XXX
Hos08:/u1/hos08/tmp>
Note: replace aaa with bbb, filter rows containing XXX, and delete rows containing YYY.
Awk (1)
<Less
> Greater
<= Less than or equal
> = Greater than or equal
= Equal
! = Not equal
Value ~ /Pattern/true if value matches the style
Value !~ /Pattern/true if the value does not match the style
& A and B
| A or B
You can use the parameters in the printf statement to make the paragraphs neat.
$ Cat tt
Aaa XXX 100 5
Aaa YYY 98 9
Bbb XXX 111 3
Bbb YYY 99 8
The row containing aaa is filtered and displayed in "TTT" format.
$ Awk '/aaa/{print, "TTT",;} 'TT
Xxx ttt aaa
Yyy ttt aaa
Filter the rows in which the third column is a two-digit number and the fourth column is greater than 5.
$ Awk '(~ /[0-9] [0-9]/) & (> 5) {print,;} 'TT
Aaa YYY 98 9
Bbb YYY 99 8
Add * numbers to the back of the listed rows
$ Awk '(~ /[0-9] [0-9]/) & (> 5) {printf "% s/t % s/n", "*";} 'TT
Aaa YYY 98 9 *
Bbb YYY 99 8 *
Replace the first column with nnn and add TTT at the end, and then replace the first column with uuu for direct display.
$ Awk '= "nnn" {printf "% s/t % s/n", "TTT" ;}= "uuu" {print;} 'TT
Nnn XXX 100 5 TTT
Uuu XXX 100 5
Nnn YYY 98 9 TTT
Uuu YYY 98 9
Nnn XXX 111 3 TTT
Uuu XXX 111 3
Nnn YYY 99 8 TTT
Uuu YYY 99 8
Next is added to the previous line. if the first command is true, skip the next section and start the check again.
$ Awk '= "nnn" {printf "% s/t % s/n", "TTT"; next ;}= "uuu" {print;} 'TT
Nnn XXX 100 5 TTT
Nnn YYY 98 9 TTT
Nnn XXX 111 3 TTT
Nnn YYY 99 8 TTT
Add TTT to the row whose third column is greater than or equal to 100, and then list the row whose fourth column is greater than or equal to 5 together with the result of the previous query.
$ Awk '> = 100 {printf "% s/t % s/n", "TTT" ;}>= 5 {print;} 'TT
Aaa XXX 100 5 TTT
Aaa XXX 100 5
Aaa YYY 98 9
Bbb XXX 111 3 TTT
Bbb YYY 99 8
Add TTT to the row with the third column greater than or equal to 100, and then list the rows with less than 100 together with the previous query.
$ Awk '> = 100 {printf "% s/t % s/n", "TTT" ;}< 100 {print;} 'TT
Aaa XXX 100 5 TTT
Aaa YYY 98 9
Bbb XXX 111 3 TTT
Bbb YYY 99 8
Use it with other commands.
$ W
Pm up 4 days, 30 users, load average: 0.97, 0.78, 0.80
User tty from login @ IDLE JCPU PCPU WHAT
Usr0809 pts/1 172.26.4.52 pm 0.54 s 0.00 s-bash
Usr0809 pts/2 172.26.4.49 pm 0.00 s 10.68 s 0.03 s-bash
U0809 pts/3 172.26.4.56 am 28:29 7.59 s 0.01 s-bash
U0809 pts/4 172.26.4.51 pm 55: 45 0.12 s 0.02 s-bash
Filter the rows containing pts and display the third and first columns of pts.
$ W | awk '=/pts/{printf "% s/t % s/n ",,;}'
172.26.4.52 user1
172.26.4.49 user2
172.26.4.56 user3
172.26.4.51 user4