1、總結sed和awk的詳細用法;
sed 流編輯器,處理一行數據到模式空間(p),不匹配條件話就輸出源行,匹配條件且有處理話,就輸出處理過后的行和源行, 匹配條件且沒有處理動作的話,只輸出p空間的行;保持空間(h)用來存放模式空間的臨時處理結果 [root@centos ~]# sed '' sed.txt 1111 匹配條件沒有處理動作,直接打印p空間讀到的行 2222 3333 4444 [root@centos ~]# sed 'p' sed.txt 1111 匹配條件且有處理話,就輸出處理過后的行和源行 1111 2222 2222 3333 3333 4444 4444 [root@centos ~]# sed -n 'p' sed.txt 1111 -n 匹配條件和處理后的行 2222 3333 4444 [root@centos ~]# sed '2p' sed.txt 1111 1,3,4行為不匹配條件,輸出源行 2222 2222 匹配條件且有處理話,就輸出處理過后的行和源行 3333 4444 [root@centos ~]# sed '2G' sed.txt 1111 1,3,4行為不匹配條件,輸出源行 2222 ---- 匹配條件且沒有處理動作的話,只輸出p的行(p的第二行變成了2222和h的空行) 3333 4444 G:從保持空間取出內容追加至模式空間 [root@centos ~]# sed '2g' sed.txt 1111 1,3,4行為不匹配條件,輸出源行 ---- 匹配條件且沒有處理動作的話,只輸出p的行(p的第二行2222被保持空間的空行覆蓋) 3333 g: 從保持空間取出數據覆蓋至模式空間; 4444 [root@centos ~]# sed '2h' sed.txt 1111 1,3,4行為不匹配條件,輸出源行 2222 匹配條件且沒有處理動作的話,只輸出p的行(p的第二行2222覆蓋了h的空行,此時h的行為2222) 3333 h: 把模式空間中的內容覆蓋至保持空間中; 4444 [root@centos ~]# sed '2H' sed.txt 1111 1,3,4行為不匹配條件,輸出源行 2222 匹配條件且沒有處理動作的話,只輸出p的行(p的第二行2222追加到了h的空行,此時h的行為空行和2222) 3333 H:把模式空間中的內容追加至保持空間中 4444 [root@centos ~]# sed '2x' sed.txt 1111 1,3,4行為不匹配條件,輸出源行 ---- 匹配條件且沒有處理動作的話,只輸出p的行(p的第二行2222變成了h的空行,此時h的行為2222) 3333 x: 把模式空間中的內容與保持空間中的內容進行互換; 4444 [root@centos ~]# sed 'n;p' sed.txt 1111 第一行執行n,匹配條件且沒有處理動作的話,只輸出p空間的行 1111和2222(本來只有1111) 2222 2222 第一行的p命令打印剛讀取的下一行2222, 如此循環,下一次該從3333起 3333 4444 4444 n: 讀取匹配到的行的下一行至模式空間,相當于每次處理兩行數據; [root@centos ~]# sed 'N;p' sed.txt 1111 第一行執行N,匹配條件且沒有處理動作的話,只輸出p空間的行 1111和2222 2222 1111 第一行的p命令,則打印了p空間的行1111和2222 如此循環,下一次從3333起 2222 3333 4444 3333 4444 N:追加匹配到的行的下一行至模式空間,相當于每次處理兩行數據; [root@centos ~]# sed '$!N;$!D' sed.txt 3333 4444 匹配條件且沒有處理動作的話,只輸出p空間的行,首先1111,2222不是最后一行,則$!D從p空間刪掉1111和2222, 然后3333,4444中的4444是最后一行,$!D沒有起作用,最后p空間的3333,4444輸出到屏幕 以上是自己個人理解,不保證正確,,,更多用法請man sed
awk awk [options] 'program' FILE ... options:-F:指明輸入時用到的字段分隔符;-v var=value: 自定義變量; program: PATTERN{ACTION STATEMENTS} 此處大括號一定不能省去,要和statement的大括號區分開 輸出:print $0(全部), $1,$2, ...$NF 格式化輸出:printf FORMAT, item1, item2, ...幾個item就幾個格式化符號,%d,%f,%s,-左對齊 內建變量:FS,OFS,RS,ORS,NF,NR,FNR,FILENAME,ARGC,ARGV 操作符:運算+-*/%^ 賦值,= += ,,++ -- 模式匹配 !~ ~ 邏輯 && || ! if-else: if(condition) statement else statement 多if-else:if(condition) statement elif(condition) statement ,,else statement while循環:while (condition) statement;控制器(i++,i--) do-while 循環: do statement while(condition) for循環:for(variable assignment;condition;iteration process) {for-body} switch語句:switch(expression) {case VALUE1 or /REGEXP/: statement; case VALUE2 or /REGEXP2/: statement;..;default: statement} 控制語句關鍵字:break,continue,next 內嵌函數:rand(),lenght(),sub,gsub,split(),,,, 數組: [root@centos ~]# awk 'BEGIN{wkd[0]="Monday";wkd[1]="Tuseday";wkd[2]="Wednesday";for (i in wkd) {print wkd[i]}}' Monday Tuseday Wednesday [root@centos ~]# awk 'BEGIN{wkd["Mon"]="Monday";wkd["Tue"]="Tuseday";wkd["Wed"]="Wednesday";for (i in wkd) {print wkd[i]}}' Wednesday Tuseday Monday 更多awk請參考awk書籍,或man awk
2、刪除/boot/grub/grub.conf文件中所有行的行首的空白字符;
[root@centos ~]# sed 's@^[[:space:]]@@g' /boot/grub/grub.conf
3、刪除/etc/fstab文件中所有以#開頭,后跟至少一個空白字符的行的行首的#和空白字符;
[root@centos ~]# sed 's@^[#][[:space:]]\+@@g' /etc/fstab
4、把/etc/fstab文件的奇數行另存為/tmp/fstab.3;
[root@centos ~]# cat -n /etc/fstab | sed -n '1~2 !p' 2# 4# Created by anaconda on Thu Jun 2 18:06:52 2016 6# Accessible filesystems, by reference, are maintained under '/dev/disk' 8# 10UUID=19bb5e83-9587-4a9a-b2e9-e4a26fdb9e29 /boot ext4 defaults 1 2 12tmpfs /dev/shm tmpfs defaults 0 0 14sysfs /sys sysfs defaults 0 0 [root@centos ~]# sed -n '1~2 !p' /etc/fstab # # Created by anaconda on Thu Jun 2 18:06:52 2016 # Accessible filesystems, by reference, are maintained under '/dev/disk' # UUID=19bb5e83-9587-4a9a-b2e9-e4a26fdb9e29 /boot ext4 defaults 1 2 tmpfs /dev/shm tmpfs defaults 0 0 sysfs /sys sysfs defaults 0 0 [root@centos ~]# sed -n '1~2 !p' /etc/fstab >/tmp/fstab.3
5、echo一個文件路徑給sed命令,取出其基名;進一步地,取出其路徑名;
[root@centos ~]# echo "/etc/sysconfig/atd" | sed 's@[^/]\+/\?$@@' /etc/sysconfig/ [root@centos ~]# echo "/var/log/messages" | sed -r 's@(/.*/)@@g' messages
6、統計指定文件中所有行中每個單詞出現的次數;
[root@centos ~]# awk '{for(i=1;i<=NF;i++){count[$i]++}}END{for(i in count) {print i,count[i]}}' sed.txt 4444 1 1111 1 2222 1 3333 1
7、統計當前系統上所有tcp連接的各種狀態的個數;
[root@centos ~]# netstat -tan | awk '/^tcp\>/{state[$NF]++}END{for(i in state) { print i,state[i]}}' ESTABLISHED 2 LISTEN 10
8、統計指定的web訪問日志中各ip的資源訪問次數:
[root@centos ~]# awk '{ip[$1]++}END{for(i in ip) {print i,ip[i]}}' /var/log/httpd/access_log 192.168.40.128 2 127.0.0.1 2
9、寫一個腳本:定義一個數組,數組元素為/var/log目錄下所有以.log結尾的文件的名字;顯示每個文件的行數;
[root@centos test]# cat exercise1.sh #!/bin/bash declare -a dir dir=$(ls /var/log/*.log) for i in $(seq 0 $[${#dir[*]}-1]);do wc -l ${dir[$i]} done
10、寫一個腳本,能從所有同學中隨機挑選一個同學回答問題;進一步地:可接受一個參數,做為要挑選的同學的個數;
[root@centos test]# ./exercise2.sh Now ,We have 99 students that you can pick up! How many students do you want:hehe Must be a number! [root@centos test]# ./exercise2.sh Now ,We have 99 students that you can pick up! How many students do you want:2 stu97, You answer my questions Please! stu51, You answer my questions Please! [root@centos test]# ./exercise2.sh Now ,We have 99 students that you can pick up! How many students do you want:1 stu67, You answer my questions Please! [root@centos test]# cat exercise2.sh #!/bin/bash declare -a stu for i in $(seq 1 99);do k=$[ $i - 1 ] stu[ $k ]=stu$i done echo "Now ,We have $i students that you can pick up!" read -p "How many students do you want:" count ! let count++ &>/dev/null && echo "Must be a number!" && exit 13 [ $count -eq 0 -o $count -gt 99 ] && echo "Please selcet a number between 1 and 99!" && exit 12 m=1 while [ $m -lt $count ];do rand=${RANDOM:0-2} if echo $rand | grep "^0" &>/dev/null;then index=${rand:0-1} echo "${stu[ $index ]}, You answer my questions Please!" else echo "${stu[ $rand ]}, You answer my questions Please!" fi let m++ done
11、授權centos用戶可以運行fdisk命令完成磁盤管理,以及使用mkfs或mke2fs實現文件系統管理;
[root@centos ~]# vim /etc/sudoers centos 192.168.40.128=/sbin/mkfs, /sbin/mke2fs, /sbin/ifconfig [root@centos ~]# su centos [centos@centos root]$ ifconfig eth0 Link encap:Ethernet HWaddr 00:0C:29:E7:51:E1 [centos@centos root]$ shutdown -h now shutdown: Need to be root
12、授權gentoo用戶可以運行邏輯卷管理的相關命令;
[root@centos ~]# vim /etc/sudoers gentoo 192.168.40.128=/sbin/*create, /sbin/*reduce,/sbin/*scan, /sbin/*display, /sbin/fsck, /sbin/resize2fs 可以考慮使用別名形式
13、基于pam_time.so模塊,限制用戶通過sshd服務遠程登錄只能在工作時間進行;
[root@centos ~]# grep -i "usepam" /etc/ssh/sshd_config #UsePAM no UsePAM yes 確保sshd開啟Pam模塊認證 [root@centos ~]# ls /lib64/security/pam_time.so 保證pam_time.so 存在 [root@centos ~]# ls /etc/pam.d/sshd /etc/security/time.conf 保證Pam模塊配置文件存在 /etc/pam.d/sshd 添加 session required pam_time.so /etc/security/time.conf 添加 sshd;ttyp*;root;!ALSa0000-2400 工作日時寫成 !ALWd0000-2400 Connecting to 192.168.40.128:22... Connection established. To escape to local shell, press 'Ctrl+Alt+]'. Last login: Sun Jun 5 03:56:43 2016 from 192.168.40.1 Connection closed by foreign host. Disconnected from remote host(192.168.40.) at 08:32:07.
14、基于pam_listfile.so模塊,定義僅某些用戶,或某些組內的用戶可登錄系統;
此題按linux_pam_SAG文檔的 pam_listfile.so 在/etc/pam.d/login 的使用禁止用戶登錄系統, 會出現了所有普通用戶都無法登錄,自己沒有確定原因,下面是禁止用戶通過ssh登錄系統 [root@centos ~]# grep "pam_listfile" /etc/pam.d/sshd auth required pam_listfile.so item=user sense=deny file=/etc/nossh onerr=fail [root@centos ~]# cat /etc/nossh derulo
原創文章,作者:Snoo,如若轉載,請注明出處:http://www.www58058.com/40349
非常的棒,總結的非常的好,一題多解的方法,值得表揚。