文本處理工具作業

1、找出ifconfig命令結果中本機的所有IPv4地址

root@cenots6.8  ~ #  ifconfig | tr -cs '[0-9]\.' '\n' |sort -u -t'.' -k3n

127.0.0.1
192.168.1.103
192.168.1.255
255.255.255.0

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

root@cenots6.8  ~ # df | tr -s ' '|cut -d' ' -f5|sort -n|tail -1|tr -d %
19

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

root@cenots6.8  ~ # cat /etc/passwd|cut -d: -f1,3,7|sort -n -t: -k2 |tail -1
nfsnobody:65534:/sbin/nologin

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

root@cenots6.8  ~ # stat /tmp|head -4|tail -1 | tr -cs [0-9] '\n'|head -2|tail -1
1777

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

root@cenots6.8  ~ # netstat -nt|tail -3|tr -s " "|cut -d" " -f5|cut -d: -f1|sort -n|uniq -c
      1 111.108.54.43
      2 192.168.1.102

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

root@cenots6.8  ~ # grep '^[sS]' /proc/meminfo;grep -i '^s' /proc/meminfo
SwapCached:            0 kB
SwapTotal:       2097148 kB
SwapFree:        2097148 kB
Shmem:              3300 kB
Slab:             112308 kB
SReclaimable:      42212 kB
SUnreclaim:        70096 kB
SwapCached:            0 kB
SwapTotal:       2097148 kB
SwapFree:        2097148 kB
Shmem:              3300 kB
Slab:             112308 kB
SReclaimable:      42212 kB
SUnreclaim:        70096 kB

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

root@cenots6.8  ~ # 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

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

root@cenots6.8  ~ # grep 'rpc\>' /etc/passwd |cut -d: -f7
/sbin/nologin

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

root@cenots6.8  ~ #  grep -o  "\<[0-9]\{2,3\}\>" /etc/passwd

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

root@cenots6.8  ~ # grep "^[[:space:]]\+[^[:space:]]\+" /etc/grub.conf
    root (hd0,0)
    kernel /vmlinuz-2.6.32-642.el6.x86_64 ro root=UUID=8ecfb3ed-37d8-43cd-a1ec-8a4be6fa5973 nomodeset rd_NO_LUKS  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_MD crashkernel=auto.UTF-8 rd_NO_LVM rd_NO_DM rhgb quiet
    initrd /initramfs-2.6.32-642.el6.x86_64.img

6、找出"netstat -tan"命令的結果中以'LISTEN'后跟0、 1或多個空白字符結尾的行

[root@localhost ~]# netstat -tan | grep "LISTEN[[:space:]]*$"
tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN     
tcp6       0      0 :::22                   :::*                    LISTEN     
tcp6       0      0 ::1:631                 :::*                    LISTEN     
tcp6       0      0 ::1:25                  :::*                    LISTEN

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

[root@localhost ~]# grep "^\(\<[[:alnum:]]\+\)\>.*\1$" /etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
bash:x:3011:3016::/home/bash:/bin/bash
nologin:x:3014:3019::/home/nologin:/sbin/nologin

1、顯示當前系統root、 mage或wang用戶的UID和默認shell

root@cenots6.8  ~ # egrep "^(root|mage|wang)\>" /etc/passwd |cut -d: -f3,7
0:/bin/bash
503:/bin/bash
504:/bin/bash

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

root@cenots6.8  ~ # egrep "^[[:alpha:]]+\(\)|^\_+\(\)" /etc/rc.d/init.d/functions
checkpid() {
daemon() {
killproc() {
pidfileofproc() {
pidofproc() {
status() {
success() {
failure() {
passed() {
warning() {
action() {
strstr() {
confirm() {

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

root@cenots6.8  ~ # echo "/etc/rc.d/init.d/functions" | egrep -o  "\<[[:alpha:]]+$"
functions

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

root@cenots6.8  ~ # echo "/etc/rc.d/init.d/functions" | egrep -o "^/.*/"
/etc/rc.d/init.d/

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

root@cenots6.8  ~ #  last | egrep -o "^root\>.*[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"|tr -s " "|cut -d" " -f3|sort|uniq -c
      4 10.1.250.89
      3 192.168.1.102
      5 192.168.1.103
      1 192.168.1.104

6、利用擴展正則表達式分別表示0-9、 10-99、 100-199、200-249、 250-255

0-9:[0-9]   10-99:[1-9][0-9]  100-199:[1-9][0-9]{2}   200-249:2[0-4][0-9]   250-255:25[0-5]

7、顯示ifconfig命令結果中所有IPv4地址

ifconfig |grep -E -o  '(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])'
192.168.1.103
192.168.1.255
255.255.255.0
127.0.0.1
255.0.0.0

1、取本機ip地址

root@cenots6.8  ~ # ifconfig|egrep -o  "inet addr:[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}" |cut -d: -f2|head -1
192.168.1.103

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

root@cenots6.8  ~ # df | tr -s " "|cut -d" " -f5
Use%
4%
1%
19%

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

cat /etc/init.d/functions | tr -cs "[:alpha:]" "\n"|sort|uniq -c|sort -n

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

root@cenots6.8  ~ # echo "/etc/rc.d/init.d/functions" | egrep -o "^/.*/"
/etc/rc.d/init.d/

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

root@cenots6.8  /testdir # egrep -io "[0-9]{17,18}x?" card
421081199402080612
350722198609015732
440202198004250317
420321198709043810

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

root@cenots6.8  /testdir # egrep -o "1[34578][0-9]{9}" iphone
13901212122
13718063333
13611111919
13021218888
13051688888
13552709999

7、正則表達式表示郵箱

root@cenots6.8  /testdir # egrep "^[^@]+@.*(com|cn)$" mail 
530180782@qq.com
243678025@qq.com
398018489@qq.com
595064131@qq.com
362483245@qq.com

8、正則表達式表示QQ號

root@cenots6.8  /testdir # egrep "[1-9][0-9]{4,10}" mail -o

原創文章,作者:Naruto,如若轉載,請注明出處:http://www.www58058.com/30809

(0)
NarutoNaruto
上一篇 2016-08-07
下一篇 2016-08-07

相關推薦

  • 硬鏈接和軟鏈接的區別

    硬鏈接和軟鏈接在文件系統實現上的區別決定了兩者之間功能上的不同,也就是說硬鏈接和軟鏈在文件系統上兩者實現的方式不同,讓其各自具有不同的功能和特色,也決定了各自的優點和缺點。接下同文件系統上創建硬鏈接和軟鏈接看各自的特點。 一文件系統上創建硬鏈接 下面命令是在文件系統上創建一個硬鏈接: ~]# ln test /testdir/t…

    Linux干貨 2016-10-29
  • vim編輯器的常見使用

    1、vim介紹 Vim是從 vi 發展出來的一個文本編輯器,處理文本文件功能強大。 三種主要模式: Vim打開文件,默認命令(Normal)模式,可以移動光標,剪切和粘貼。 按鍵i 進入插入(Insert)模式,用戶可以編輯文本。 擴展(Extended)命令模式(或末行模式)用戶:wq可保存退出,或者:q!強制退出。 Exc按鍵可退出當前模式。 2、文本操…

    Linux干貨 2017-07-30
  • 話說CentOS6的啟動流程

    1.按下開機按鈕,電腦的主板通電,電腦開始加電自檢(POST,Power On and Self Test),測試主機的硬件是否滿足開機的要求. 2.加載主板上的BIOS(Base Input/Output System,基本輸入輸出系統),BIOS會加載用戶設定的第一個可開機設備.,這里我們只說硬盤的加載. 可以按下鍵盤上的Esc或者是F2或者是Del鍵,…

    Linux干貨 2017-05-14
  • Linux-文件系統掛載:mount的用法

     文件系統掛載  本章詳細介紹文件系統掛載、卸載(mount,umount);  如何通過mount命令掛載使用文件系統、掛載常用參數、以及開機自動掛載(/etc/fstab)。    掛載描述:             掛載:將額外文件系…

    2017-06-19
  • Linux終端類型

    Linux終端類型 Linux的終端類型包括控制臺終端、虛擬終端、偽終端、串行終端、控制終端、圖形終端。   終端 要理解偽終端(Pseudo Terminal),先來看看什么是“終端”(Terminal)。 終端是一種字符型設備,它有多種類型,通常使用tty來簡稱各種類型的終端設備。 1、串行端口終端(/dev/ttySx) 串行端口終端(Ser…

    Linux干貨 2016-10-14
  • linux用戶和組

    *** Linux用戶和組: 1.用戶:Username/UID     管理員:root, 0     系統用戶:1-499(CENTOS6), 1-999(CENTOS7)     對守護進程獲取資源進行權限分配  &nbsp…

    Linux干貨 2016-08-04
欧美性久久久久