Shell 5-6 collection
Sell 5 reviews the basic regular expressions ^ $[abc] [^]. *\ {n\}\ {n\}\ {n,\}\ (\) extend regular +? {n ·m} a | b | c ()\ bsed non-interactive editing text front command | sed option (address) instruction sed option (address character) instruction the document to be processed option:-n-r-I instruction: p d s
= =
Add line a to line I and add c to line I to replace the whole line
Find the user using bash, display the user name #! / bin/bashn= `sed-n'/ bash$/s/:.*//p' / etc/ passwd`for I in $n in the way of user name-> password, and give the user name found using bash to the for loop do p = `grep $I: / etc/ shadow` to compare the name with shadow. Find the corresponding line, p2pin ${p1% bash$/s/:.*//p' *} to remove the tail. Get pure password echo "$I-- > $p2" output final result doneawk precise search document usage: awk option (condition) instruction preinstruction for processed document | awk option (condition) instruction awk'{print} 'test output all awk' {print $2} 'test output column 2 per line awk' / hello/ {print $2} 'test find the line with hello Output his second column awk's built-in variable $1 $2 $3. 0 all columns NR line number NF column number awk default delimiter is a space, you can use-F to modify the delimiter awk-F:'{print $1} 'user defines the delimiter as a colon, outputs the first column awk-F [: /]' {print $10} 'user defines the delimiter as a colon or slash, outputs column 10 awk-F:' {print $1 "the interpreter is" $7} "user outputs a specific column Constant + variable ifconfig eth0 | awk'/ RX p / {print "Native eth0 receive traffic is" $5 "byte"} 'get Nic receive traffic ifconfig eth0 | awk' / TX p / {print "Nic send traffic is" $5 "byte"} 'get Nic send traffic df-h | awk' / vda1/ {print "current host and partition remaining space is" $4} " The ip of the person who gets the remaining space in the root partition awk'/ Failed/ {print "* * is" $11}'/ var/log/secure awk working timing BEGIN task execution 1 line-by-line task execution N times END task execution 1 time BEGIN {} END {} awk 'BEGIN {instruction} {instruction} END {instruction}' file awk 'BEGIN {print "ABCD"} {print} END {print NR } 'user tab key\ t tab awk-F:' BEGIN {print "User\ tUid\ tHome"} {print $1 "\ t" $3 "\ t" $6} END {print "Total" NR "Row"} 'user
= =
Awk processing condition 1, use regular as condition awk-F:'/ ^ root/ {print $1} 'user to find the line beginning with root, output the first column awk-F:' / ^ root | ^ bin/ {print $1} 'user to find the line beginning with root or bin, output the first column awk-F:' / ^ (root | bin) / {print $1} 'user ditto awk-F:' $1~/root/ print $1} 'user to find the first column containing root And output the first column, awk-F:'$1 alternate print root / {print $1} 'user, the first column does not contain root2, and the number and string are conditional = =! = > =
< >= <