8.5-文本處理工具(作業篇)

 1、統計當前連接本機的每個遠程主機IP的連接數,并按從大到小排序

[root@localhost ~]# netstat -nt
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State      
tcp        0      0 10.1.252.66:22              10.1.252.7:61217            ESTABLISHED 
tcp        0      0 10.1.252.66:22              10.1.252.7:61133            ESTABLISHED 
[root@localhost ~]# netstat -nt | tail -n +3 | tr -s ' ' | cut -d' ' -f4 | sort | uniq -c | sort -nr
      2 10.1.252.66:22

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

[root@localhost ~]# stat /tmp/ | tail -n +4 | head -1 | cut -d'/' -f1 | cut -d'(' -f2 
1777
[root@localhost ~]# stat -c %a /tmp
1777

3、查出用戶UID最大值的用戶名、 UID及shell類型

[root@localhost ~]# sort -t':' -k3 -n /etc/passwd | tail -1 | cut -d':' -f1,3,7
nfsnobody:65534:/sbin/nologin

4、取本機ip地址

[root@localhost ~]# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:0C:29:71:3A:3A  
          inet addr:10.1.252.66  Bcast:10.1.255.255  Mask:255.255.0.0
          inet6 addr: fe80::20c:29ff:fe71:3a3a/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:3567 errors:0 dropped:0 overruns:0 frame:0
          TX packets:683 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:326443 (318.7 KiB)  TX bytes:77640 (75.8 KiB)
[root@localhost ~]# ifconfig|head -2|tail -1|tr -s " "|cut -d" " -f3|cut -d: -f2
10.1.252.66

5、取各分區利用率的數值

[root@localhost ~]# df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/sda3       19276064 3716892  14573316  21% /
tmpfs             502068       0    502068   0% /dev/shm
/dev/sda1         194241   34091    149910  19% /boot
[root@localhost ~]# df | tr -s ' ' | cut -d" " -f5| tail -n +2
21%
0%
19%

6、統計/etc/init.d/functions 文件中每個單詞出現的次數,并按頻率從高到低顯示

[root@localhost ~]# cat /etc/rc.d/init.d/functions | tr -c '^[:alpha:]' '\n' | sort | uniq -c | sort -n -r
   9294 
     83 if
     77 then
     75 pid
     73 echo
     72 fi
     61 return
     57 dev
     54 file
     50 n
     46 local
     42 kill
     39 z
     36 base

7、/etc/rc.d/init.d/functions或/etc/rc.d/init.d/functions/"  取目錄名

[root@localhost ~]# echo "/etc/rc.d/init.d/functions/" |sed 's@[^/]\+/\?$@@'
/etc/rc.d/init.d/
[root@localhost ~]# echo "/etc/rc.d/init.d/functions"  |sed 
/etc/rc.d/init.d/

8、正則表達式表示身份證號

[root@localhost ~]# cat a.txt | grep -E --color "\<[0-9]{18}\>|\<[0-9]{15}\>"

001.png

9、正則表達式表示手機號

[root@localhost ~]# grep --color  -E "\<1[34578][0-9]{9}\>" a.txt

 

002.png

10、正則表達式表示郵箱

[root@localhost ~]# cat a.txt |grep -E "\<[[:alnum:]]+@[[:alnum:]]*\.[[:alpha:]]+\>"

009.png

11、正則表達式表示QQ號

cat a.txt |grep -E "[1-9][0-9]{4,11}"

003.png

12、顯示/proc/meminfo文件中以大小s開頭的行; (要求:使用兩種方式)

[root@localhost ~]# grep -i "^s" /proc/meminfo
[root@localhost ~]# grep "^[sS]" /proc/meminfo
[root@localhost ~]# grep "^s\|^S" /proc/meminfo

003.png

13、顯示/etc/passwd文件中不以/bin/bash結尾的行

[root@localhost ~]# grep -v ":/bin/bash$" /etc/passwd
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
...

14、顯示用戶rpc默認的shell程序

[root@localhost ~]# grep "^rpc:" /etc/passwd | cut -d ":" -f7
/sbin/nologin
[root@localhost ~]# getent passwd rpc |cut -d: -f7
/sbin/nologin

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

[root@localhost ~]# grep -E "\<[1-9][0-9]?[0-9]\>" /etc/passwd

004.png

16、顯示/etc/grub2.cfg文件中,至少以一個空白字符開頭的且后面存非空白字符的行

[root@localhost ~]# grep -E "^[[:space:]]+[^[:space:]]" /etc/grub2.cfg

005.png

17、找出“netstat -tan”命令的結果中以‘LISTEN’后跟任意多個空白字符結尾的行

[root@localhost ~]# netstat -tan | grep -E "LISTEN[[:space:]]+$"

007.png

18、添加用戶bash、 testbash、 basher以及nologin(其shell為/sbin/nologin),而后找出/etc/passwd文件中用戶名同shell名的行

[root@localhost ~]# useradd bash
[root@localhost ~]# useradd testbasher
[root@localhost ~]# useradd -s "/sbin/nologin" nologin

008.png

19、顯示三個用戶root、 mage、 wang的UID和默認shell

[root@localhost ~]# grep -E "^wang:|^mage:|^root:" /etc/passwd | cut -d: -f3,7
0:/bin/bash
1004:/bin/bash
1005:/bin/bash

20、找出/etc/rc.d/init.d/functions文件中行首為某單詞(包括下劃線)后面跟一個小括號的行

[root@localhost ~]# grep "^[[:alpha:]_]\+()" /etc/rc.d/init.d/functions
checkpid() {
__pids_var_run() {
__pids_pidof() {
daemon() {
killproc() {
pidfileofproc() {
pidofproc() {
status() {
echo_success() {
...

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

[root@localhost ~]# echo "/etc/rc.d/init.d/functions" | egrep -o --color "[^/]+/?$" |cut -d"/" -f1
functions
[root@localhost ~]# echo "/etc/rc.d/init.d/functions/" | egrep -o --color "[^/]+/?$" |cut -d"/" -f1
functions

23、統計以root身份登錄的每個遠程主機IP地址的登錄次數

[root@localhost ~]# last | grep -o -E "^root\>.*((25[0-4]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.){3}(25[0-4]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])"|tr -s ' '|cut -d" " -f3|sort|uniq -c
      4 192.168.33.1

24、利用擴展正則表達式分別表示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]

 

原創文章,作者:M20-1--孔祥文,如若轉載,請注明出處:http://www.www58058.com/29985

(0)
M20-1--孔祥文M20-1--孔祥文
上一篇 2016-08-07 22:06
下一篇 2016-08-07 22:06

相關推薦

  • 淺談群紅包的實現

    前言:紅包是支付的方式, 也是社交的延伸。群紅包在這兩塊領域串聯得很好, 表現尤為的濃墨重彩. 承接上兩篇技術淺談:1). 淺談接龍紅包的技術實現.2). 淺談微信紅包搖一搖的技術實現.這一次, 讓我們談談群紅包的技術實現. 一為是紅包的分配算法, 二為競搶的技術實現. 分配算法:最初玩群紅包的時候, 并沒有意識到分配算法的難度…

    Linux干貨 2015-03-10
  • 銘記今天

        2016年10月19日,我做了一個很艱難的決定,選擇了馬哥24期網絡全程班,就算是沖動也好,無論對錯都要堅持下去。 從出來工作之后,一直做銷售這塊,雖然跟電腦有關,但是其中的艱苦只有自己明白。慢慢的年級上來了,手中沒有什么技術,做人生活,總覺得少了什么,總覺得自己以后會被社會淘汰,心一天一天著急。在后選擇了一個從來都沒有涉及的運維…

    Linux干貨 2016-10-19
  • Nginx 進階 (ssl、fpm、rewrite、cache配置等)

    Nginx(與ssl結合配置https網站、rewrite,fastcgi配置詳解) 前言 前面已經介紹過Nginx的一些基礎概念,還有幾個比較重要的模塊:利用ssl給會話加密,利用rewrite功能靈活改寫訪問結果,以及利用fastcgi與php模塊結合等等。 一、配置https網站 1、自建CA (1)生成私鑰文件 mkdir -p /etc/pki/C…

    Linux干貨 2016-12-26
  • 磁盤配額實現

    磁盤配額實現 磁盤配額要求必須是獨立的分區 創建一個新的分區 #同步分區表 [root@localhost ~]# partx -a /dev/sda [root@localhost ~]# mkfs.ext4 /dev/sda6 -L /home [root@localhost ~]# blkid /dev/sda6: LABEL=”/home…

    Linux干貨 2017-12-09
  • Linux任務計劃

    Linux任務計劃主要分為分為兩種分別是一次性任務計劃和周期性任務計劃實現工具主要是at和crontab下面將詳細介紹任務計劃工具的使用。 1、at命令一次性任務計劃 at命令是由atd服務提供的其主程序包是atd在CentOS6上可以使用service atd start命令來啟動在CentOS7上需要使用systemctl start atd.servi…

    Linux干貨 2016-09-11
欧美性久久久久