Linux shell BASICS (8) and Linux shell Basics
8. Use the stream Editor of shell script sed & awk01.sed-Steam Editor # ed/etc/passwd1, 10 p ...... 1 s/root/byf/p (replace root with byf) # sed-e's/root/byf/G'/etc/passwd (same as above) only the displayed content changes # sed-e's/root/byf/G'/etc/passwd>/tmp/a # cp/tmp/a/etc/passwd # sed -n-e '/\ <byf \>/p (print rows containing byf words) '/etc/passwd # sed-n-E'/\ <byf \>/d (delete rows containing byf words) '/etc/passwd # sed-E'/\ <byf \>/s/bash/nologin/G'/etc/passwd>/tmp/a # cp/tmp/ /etc/passwd (file replacement, is equivalent to modifying the passwd file) # vi/etc/squid. f # sed-e '/^ #/d'-e'/^ $/D'/etc/squid. Conf | wc-l (delete # comment rows, and rows starting with spaces to quickly display useful rows) # sed-e '/bash/s/byf/root/G'/etc/passwd (modify the root containing bash rows to byf) multiple replications # sed-e ''-e'' # sed-e': :( colon separation command) '# sed-e' 1 ~ 2' (execute 1, 3, 5 ,......) Step 2 # sed-e's/root/byf/'-e's/bash/nologin'/etc/passwd # vi ssss/root/byfs/bash/nologin # sed-f sss/etc/passwd (the result is the same as-e ''-e '') # vi sss #! /Bin/sed-fs/root/byfs/bash/nologin # chmod a + x sss #. /sss/etc/passwd02.awk # man sed # info sed (view sed information function) (you can view examples of sed, * click enter to jump to the specified sed instance) sed can process row content awk can process column content # chkconfig -- list | awk '{print $5}' 3: off3: off3: on ...... # Chkconfig -- list | awk '$1 = "httpd" {print $5}' 3: on # if [chkconfig -- list | awk '$1 = "httpd" {print $5}' = "3: on"]; then echo OK; fiok # chkconfig -- list | awk '{print $1 "on level" $5}' | head # chkconfig -- list | awk 'begin {iii = 0} {if ($5 = "3: on ") iii = iii + 1} END {[print iii} '# awk' BEGIN {iii = 0} {if ($7 ="/bin/bash ") iii = iii + 1} END {print iii} '</etc/passwd # awk-F (what is the separator, Here ':'): '{print $1 "UID is" $3}'/etc/passwd write script # vi aaaBEGIN {iii = 0} {if ($5 = "3: on ") iii = iii + 1} END {print iii}: wq # chkconfig -- list | awk-f may be used for log analysis