天天看點

文本處理工具和正規表達式練習

1、在vim中設定tab縮進為4個字元

set autoindent 或 set ai

2、複制/etc/rc.d/init.d/functions檔案至/tmp目錄,替換/tmp/functions檔案中的/etc/sysconfig/init 為/var/log

cp /etc/rc.d/init.d/functions /tmp

vim /tmp/functions

%s@/etc/sysconfig/init@/var/log@g

3、删除/tmp/functions檔案中所有以#開頭,且#後面至少有一個空白字元的行的行首的#号

%s@^#([[:space:]]*)@\1@

4、找出ifconfig “網卡名” 指令結果中本機的IPv4位址

ifconfig ens160 | head -2|tail -1|tr -s ' ' :|cut -d: -f3

5、查出分區空間使用率的大百分比值

df -h| tr -s ' ' :|cut -d: -f5|tail -8|sort -nr |head -1

6、查出使用者UID大值的使用者名、UID及shell類型

cut -d: -f1,3,7 /etc/passwd |sort -t: -k2 -n | tail -1

7、查出/tmp的權限,以數字方式顯示

stat /tmp/|head -4|tail -1|cut -d/ -f1|cut -d'(' -f2

8、統計目前連接配接本機的每個遠端主機IP的連接配接數,并按從大到小排序

netstat -tunlp |grep ESTAB|tr -s ' ' |cut -d: -f5|sort -nr|uniq -c

9、顯示/proc/meminfo檔案中以大小s開頭的行(要求:使用兩種方法)

grep '^S.' /proc/meminfo 或 cat /proc/meminfo | grep '^S.'

10、顯示/etc/passwd檔案中不以/bin/bash結尾的行

grep -v '/bin/bash$' /etc/passwd

11、顯示使用者rpc預設的shell程式

grep '\<rpc\>' /etc/passwd | cut -d: -f1,7

12、找出/etc/passwd中的兩位或三位數

grep -E '\<[0-9]{2,3}\>' /etc/passwd

13、顯示CentOS7的/etc/grub2.cfg檔案中,至少以一個空白字元開頭的且後面有非空白字元的行

grep '^[[:space:]]+[^[:space:]]' /etc/grub2.cfg 或 grep -E '^[[:space:]]+[^[:space:]]' /etc/grub2.cfg

14、找出“netstat -tan”指令結果中以LISTEN後跟任意多個空白字元結尾的行

netstat -tan | grep 'LISTEN[[:space:]]*$'

15、顯示CentOS7上所有UID小于1000以内的使用者名和UID

cut -d: -f1,3 /etc/passwd | grep '\<[0-9]{1,3}\>'

16、添加使用者bash、testbash、basher、sh、nologin(其shell為/sbin/nologin),找出/etc/passwd使用者 名和shell同名的行

grep -E '^(\<[[:alpha:]]+\>).\1$' /etc/passwd 或 grep '^(\<[[:alpha:]]+\>).\1$' /etc/passwd

17、利用df和grep,取出磁盤各分區使用率,并從大到小排序

df | tr -s ' ' :|cut -d: -f5|tail -8 |sort -nr

18、顯示三個使用者root、mage、wang的UID和預設shell

cut -d: -f1,3,7 /etc/passwd | grep -E 'root|mage|wang'

19、找出/etc/rc.d/init.d/functions檔案中行首為某單詞(包括下劃線)後面跟一個小括号的行

grep -E "^\<([[:alpha:]]|_)+\>()." /etc/rc.d/init.d/functions

20、使用egrep取出/etc/rc.d/init.d/functions中其基名

echo "/etc/rc.d/init.d/functions" | egrep -o '[[:alpha:]]+$' 或echo "/etc/rc.d/init.d/functions" | egrep -o '[^/]+$'

等價于basename /etc/rc.d/init.d/functions

21、使用egrep取出上面路徑的目錄名

echo "/etc/rc.d/init.d/functions" | egrep -o '^.*/'

22、統計last指令中以root登入的每個主機IP位址登入次數

last | grep 'root' |tr -s ' '| cut -d' ' -f1,3|sort|uniq -c

23、利用擴充正規表達式分别表示0-9、10-99、100-199、200-249、250-255

[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]

24、顯示ifconfig指令結果中所有IPv4位址

ifconfig | egrep -o "\<(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4]).){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4])\>"

25、将此字元串:welcome to magedu linux 中的每個字元去重并排序,重複次數多的排到前面

echo "welcome to magedu linux"|grep -o '.'|sort|uniq -c|sort -nr

26、删除centos7系統/etc/grub2.cfg檔案中所有以空白開頭的行行首的空白字元

sed -r 's/^[[:space:]]+(.*)/\1/' /etc/grub2.cfg

27、删除/etc/fstab檔案中所有以#開頭,後面至少跟一個空白字元的行的行首的#和空白字元

sed -r 's/^#[[:space:]]+(.*)/\1/' /etc/fstab

28、在centos6系統/root/install.log每一行行首增加#号

sed -r 's/(.*)/#\1/' /root/install.log

29、在/etc/fstab檔案中不以#開頭的行的行首增加#号

sed -rn 's/^[^#]/#&/p' /etc/fstab

sed -rn 's/^([^#])/#\1/p' /etc/fstab

30、處理/etc/fstab路徑,使用sed指令取出其目錄名和基名

取目錄名:echo "/etc/fstab" | sed -r 's#(.)\/.+#\1#'

取基名:echo "/etc/fstab" | sed -r 's#.\/(.+)#\1#'

31、利用sed 取出ifconfig指令中本機的IPv4位址

ifconfig | sed -rn '/inet\b/p'|sed -r 's#.inet\>(.)\<netmask.*$#\1#'

32、統計centos安裝CD光牒中Package目錄下的所有rpm檔案的以.分隔倒數第二個字段的重複次數

ls /misc/cd/Packages/.rpm | sed -r 's@..(.*).rpm@\1@' | sort | uniq -c

33、統計/etc/init.d/functions檔案中每個單詞的出現次數,并排序(用grep和sed兩種方法分别實作)

sed -r 's@[^[:alpha:]]+@\n@g' /etc/init.d/functions |sort | uniq -c|sort -n

grep -Eo '[[:alpha:]]+' /etc/init.d/functions |sort |uniq -c|sort -n

34、将文本檔案的n和n+1行合并為一行,n為奇數行

seq 1 10|sed -r 'N;s/\n//'

35、檔案host_list.log 如下格式,請提取”.magedu.com”前面的主機名部分并寫入到回到該檔案中

1 www.magedu.com

2 blog.magedu.com

3 study.magedu.com

4 linux.magedu.com

5 python.magedu.com

......

999 study.magedu.com

36、統計/etc/fstab檔案中每個檔案系統類型出現的次數

cat /etc/fstab | awk '/^UUID/{sys[$3]++}END{for(i in sys){print i,sys[i]}}'

37、統計/etc/fstab檔案中每個單詞出現的次數

grep -Eo '\<[[:alpha:]]+\>' fstab |sort|uniq -c

38、提取出字元串Yd$C@M05MB%9&Bdh7dq+YVixp3vpw中的所有數字

echo "Yd$C@M05MB%9&Bdh7dq+YVixp3vpw"|awk 'gsub(/[^0-9]/,"")'