馬哥教育網絡19期+第十五周課程練習

1、總結sed和awk的詳細用法;

  a).sed命令

sed可以實現grep的大部分功能,而且還可以查找替換
[root@localhost ~]# sed '10'p -n 1.txt
[root@localhost ~]# sed '1,4'p -n 1.txt
[root@localhost ~]# sed '5,$'p -n 1.txt
說明:這里的p是print的意思,加上-n后就可以只打印符合規則的行,如果不加則會把1.txt從頭到尾打印一遍。
    '10'p 打印第 10 行
    '1,4'p 打印 1 到 4 行
    '5,$'p 打印 5 到末行
    
打印包 含某個字符串的行
[root@localhost ~]# sed -n '/root/'p 1.txt

可以使用 ^ . * $ 等特殊符號
[root@localhost ~]# sed -n '/ro.t/'p 1.txt
[root@localhost ~]# sed -n '/^roo/'p 1.txt

[root@localhost ~]# sed -n -r '/ro+/'p 1.txt
[root@localhost ~]# sed -n '/ro\+/'p 1.txt
上面兩個命令的效果是一樣的。

“-e”可以實現同時進行多個任務、也可以用“;”實現
[root@localhost ~]# sed -e '/root/p' -e '/body/p' -n 1.txt
[root@localhost ~]# sed '/root/p; /body/p' -n 1.txt

刪除指定行
[root@localhost ~]# sed '/root/d' 1.txt; sed '1d' 1.txt; sed '1,10d' 1.txt
說明:'/root/d' 刪除包含 root 的行;'1d'或者'1'd 刪除第一行;'1,10'd 刪除 1 到 10 行
替換功能

[root@localhost ~]# sed '1,2s/ot/to/g' 1.txt
說明:s就是替換的意思,g為全局替換,否則只替換第一次的,/也可以為# @等

[root@localhost ~]# sed '1,2s@ot@to@g' 1.txt
刪除所有數字

[root@localhost ~]# sed 's/[0-9]//g' 1.txt
說明: 其實就是把所有數字替換為空字符

刪除所有非數字
[root@localhost ~]# sed 's/[^0-9]//g' 1.txt

調換兩個字符串位置
[root@localhost ~]# head -n2 1.txt |sed -r 's/(root)(.*)(bash)/\3\2\1/'
說明:在 sed 中可以用()去表示一個整體,本例中把 root 和 bash 調換位置,后面的\1\2\3 分別表示第一個小括號里面的,第二個小括號里面的以及第三個小括號里面的內容。

-i 選項可 以直接修改文件內容
[root@localhost ~]# sed -i 's/ot/to/g' 1.txt

  b).awk命令

截取文檔中的某段
[root@localhost ~]# awk -F ':' '{print $1}' 1.txt
說明: -F 指定分隔符號為:
也可以使用自定義字符連接每個段
[root@localhost ~]# awk -F':' '{print $1"#"$2"#"$3"#"$4}' 1.txt
或者使用 awk 內部變量 OFS,格式如下:
# awk -F ':' '{OFS="#"} {print $1,$2,$3,$4}' 1.txt

匹配字符或字符串
[root@localhost ~]# awk '/oo/' 1.txt
針對某個段匹配
[root@localhost ~]# awk -F ':' '$1 ~/oo/' 1.txt
多次匹配
[root@localhost ~]# awk -F ':' '/root/ {print $1,$3}; $1 ~/test/; $3 ~/20/' 1.txt
條件操作符==, >,<,!=,>=;<=
第三段為 0
[root@localhost ~]# awk -F ':' '$3=="0"' 1.txt;

第三段大于等于 500
[root@localhost ~]# awk -F ':' '$3>=500' 1.txt;
說明:當比較數字時,不能加雙引號,如果寫成$3>="500"就不符合我們的需求了。
第七段不是'/sbin/nologin'
[root@localhost ~]# awk -F ':' '$7!="/sbin/nologin"' 1.txt;
第三段小于第四段
[root@localhost ~]# awk -F ':' '$3<$4' 1.txt ;
第三段大于 5,并且第三段小于 7
[root@localhost ~]# awk -F ':' '$3>5 && $3<7' 1.txt
第三段大于 5 或者第七段為'/bin/bash'
[root@localhost ~]# awk -F ':' '$3>"5" || $7=="/bin/bash"' 1.txt

awk  內置變量 NF( 段數) NR( 行數)
[root@localhost ~]# head -n3 1.txt | awk -F ':' '{print NF}'
[root@localhost ~]# head -n3 1.txt | awk -F ':' '{print $NF}'
[root@localhost ~]# head -n3 1.txt | awk -F ':' '{print NR}'

打印 20 行以后的行
[root@localhost ~]# awk 'NR>20' 1.txt
打印 20 行以后并且第一段包含'ssh'的行
[root@localhost ~]# awk -F ':' 'NR>20 && $1 ~ /ssh/' 1.txt

更改某個段的值
[root@localhost ~]# awk -F ':' '$1="root"' 1.txt

數學計算, 把第三段和第四段值相加,并賦予第七段
[root@localhost ~]# awk -F ':' '{$7=$3+$4; print $0}' 1.txt
但是這樣的話,相當于改變了原來文本的結構,所以 print $0 的時候就不再有分隔符顯示。如果想顯
示分隔符需要借助 OFS
[root@localhost ~]# awk -F ':' '{OFS=":"} {$7=$3+$4; print $0}' 1.txt
計算第三段的總和
[root@localhost ~]# awk -F ':' '{(tot=tot+$3)}; END {print tot}' 1.txt
awk 中也可以使用 if  關鍵詞
[root@localhost ~]# awk -F ':' '{if ($1=="root") print $0}' 1.txt

2、刪除/boot/grub/grub.conf文件中所有行的行首的空白字符;

sed 's@^[[:space:]]\+@@' /boot/grub/grub.conf

3、刪除/etc/fstab文件中所有以#開頭,后跟至少一個空白字符的行的行首的#和空白字符;

sed 's@^#[[:space:]]\+@@' /etc/fstab

4、把/etc/fstab文件的奇數行另存為/tmp/fstab.3;

sed 'n;d' /etc/fstab >> /tmp/fstab.3

5、echo一個文件路徑給sed命令,取出其基名;進一步地,取出其路徑名;

echo "/etc/fstab" | sed 's@[^/]\+/\?$@@'

6、統計指定文件中所有行中每個單詞出現的次數;

awk '{for(i=1;i<=NF;i++){count[$i]++}}END{for(i in count) {print i,count[i]}}' /etc/fstab

7、統計當前系統上所有tcp連接的各種狀態的個數;

netstat -nat | awk 'FNR>2{print $NF}' | sort | uniq -c

8、統計指定的web訪問日志中各ip的資源訪問次數:

# vim test.sh 
#!/bin/bash
#
cat access.log |sed -rn '/16\/Aug\/2015/p' > a.txt 
#統計test.txt里面有多少個ip訪問
cat test.txt |awk '{print $1}'|sort |uniq > ipnum.txt
#通過shell統計每個ip訪問次數
for i in `cat ipnum.txt`
do 
iptj=`cat  access.log |grep $i | grep -v 400 |wc -l`
echo "ip地址"$i"在2016-08-15日當天累計請求次數為"$iptj"次,平均每分鐘請求次數為:"$(($iptj/1440)) >> result.txt
done

9、寫一個腳本:定義一個數組,數組元素為/var/log目錄下所有以.log結尾的文件的名字;顯示每個文件的行數;

#!/bin/bash
#
Arraywc=(/var/log/*.log)
for i in $(seq 0 $[${#Arraywc[@]}-1])
  #do wc -l ${Arraywc[i]}
    do  wc -l ${Arraywc[i]} >> /tmp/log.wc 
done
awk '{printf "%-32s%s\n",$2,$1}' /tmp/log.wc
rm /tmp/log.wc

10、寫一個腳本,能從所有同學中隨機挑選一個同學回答問題;進一步地:可接受一個參數,做為要挑選的同學的個數;

#!/bin/bash
student=(a b c d e f g h i j k)
i=$[$RANDOM % ${#student[@]}]
echo ${student[i]}

進一步地:

#!/bin/bash
#
students=(a b c d e f g h i j k)
read -t 5 -p "Please input the number of students: " num
if [[ $num -le ${#students[@]} ]]
then 
     for ((i=0;i<num;i++))
     do x=$[$RANDOM % ${#students[@]}]
        echo  ${students[$x]}
        students[$x]=${students[${#students[@]}-1]}
        unset students[${#students[@]}-1]     #這樣刪就不會再選到這個索引號
       #unset students[$x]  這個刪除只刪除了元素的值,但索引號仍在值為空
     done  
else echo "Error";exit
fi

11、授權centos用戶可以運行fdisk命令完成磁盤管理,以及使用mkfs或mke2fs實現文件系統管理;

centos  ALL=(root)   NOPASSWD: /sbin/fdisk, /sbin/mke2fs, /sbin/mkfs

12、授權gentoo用戶可以運行邏輯卷管理的相關命令;

gentoo  ALL=(root)   lvm

13、基于pam_time.so模塊,限制用戶通過sshd服務遠程登錄只能在工作時間進行;

(1).# vim /etc/pam.d/sshd
在account required pam_nologin.so上插入一行:
account required pam_time.so
(2).編輯pam_time.so模塊的配置文件
# vim /etc/security/time.conf
*;*;*;MoTuWeThFr0900-1800
上面表示工作時間的9點到下午6點允許訪問ssh

14、基于pam_listfile.so模塊,定義僅某些用戶,或某些組內的用戶可登錄系統;

創建一個用戶的列表文件,例如/etc/sshd_userlist,然后編輯文件
root 
centos
gentoo
然后修改文件的權限和屬主
# chmod 600 /etc/sshd_userlist 
# chown root /etc/sshd_userlist
再編輯/etc/pam.d/sshd文件,加入以下一行內容:
auth required pam_listfile.so item=user sense=allow file=/etc/sshd_userlist onerr=succeed

然后再次登錄時,只允許文件中定義的root、centos和gentoo用戶登錄。

原創文章,作者:Net19_口香糖,如若轉載,請注明出處:http://www.www58058.com/36381

(0)
Net19_口香糖Net19_口香糖
上一篇 2016-08-22
下一篇 2016-08-22

相關推薦

  • 無網不利

                    網絡是主機之間交互的必備條件。聯網就是將主機互聯來形成網絡,使得網絡中的主機得以交換信息。       &n…

    2017-08-19
  • Nginx 負載均衡多個nmmp主機

    設計拓撲圖 一、Memcache簡介 cache(緩存系統) 高性能分布式緩存服務器,緩存所有的“可流式化”數據:livejournal旗下Danga Interacive公司 程序=指令+數據 指令:程序 數據:IO操作 文件系統: 特征:協議簡單  基于libevent的事件處理  內置內存存儲方式  memcached 不…

    2016-06-14
  • M22 Centos設置yum源方法

    實驗目的: 使用虛擬機模擬建立yum源服務器,在客戶機上指定yum源為該服務器。 實驗器材: WMware12安裝Centos6.8虛擬機作為yum服務源 WMware12安裝Centos7.3 作為客戶端訪問yum源服務器 實驗原理圖: 實驗過程: 服務器端: 1、  顯示已經掛載的光盤 #cd /misc/cd #先執行此命令才可以看到光盤掛載…

    2017-03-06
  • DHCP

        動態主機設置協議(Dynamic Host Configuration Protocol,DHCP)是一個局域網的網絡協議,使用UDP協議工作,主要有兩個用途: 用于內部網絡或網絡服務供應商自動分配IP地址給用戶 用于內部網絡管理員作為對所有電腦作中央管理的手段     …

    Linux干貨 2017-07-24
  • net25-第14周作業

    系統的INPUT和OUTPUT默認策略為DROP; ~]# iptables -P INPUT DROP ~]# iptables -P OUTPUT DROP 1、限制本地主機的web服務器在周一不允許訪問;新請求的速率不能超過100個每秒;web服務器包含了admin字符串的頁面不允許訪問;web服務器僅允許響應報文離開本機; ~]#iptables -…

    Linux干貨 2017-05-15
  • 網絡基礎知識子網劃分

    一、某公司申請到一個C 類IP 地址,但要連接6 個的子公司,最大的一個子 公司有26 臺計算機,每個子公司在一個網段中,則子網掩碼應設為?  根據要劃分的子網數量來算子網掩碼的。2的M次方大于等于N。其中M是劃分子網時要借的位數。N是要劃分的子網數。 將需求代入公式,M=3,C類IP默認的掩碼為24位,再從主機位中借來3位,就是27位。二進制表示…

    Linux干貨 2016-09-06

評論列表(1條)

  • 馬哥教育
    馬哥教育 2016-08-22 15:12

    寫的很好,排版也很棒,加油

欧美性久久久久