文本處理工具作業

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 14:17
下一篇 2016-08-07 22:05

相關推薦

  • MySQL高可用架構之Galera Cluster

    MySQL高可用架構之Galera Cluster 1、實驗準備及拓撲 至少需要三個節點 node1 192.168.150.137 node2 192.168.150.138 node3 192.168.150.139 mariadb版本為mariadb的支持galera cluster的分支版本 MariaDB-Galera-server-5.5.46 …

    Linux干貨 2017-03-31
  • H3C 設備監測命令大全 (v3)

    H3C 設備監測命令大全  display aaa unsent-h323-call-record  display acl      display alarm   urgent   display…

    Linux干貨 2016-06-01
  • 重定向與管道

         本次內容    1.三種I/O設備    2.把I/O從定向入文件    3.命令tr    4.使用管道鏈接命令    5.tee   我們都知道程序是由:指令+數據    &n…

    2017-07-23
  • Linux基礎小模塊

    1.基礎知識小塊:shell 由shell程序的自帶命令:即為內置命令(builtin) 獨立的可執行程序文件、文件名 :即為外部命令  [root@localhost~]#ls      ~用戶當前所在目錄  #:命令提示符(管理員帳號root)  $:普通用戶   &nbsp…

    Linux干貨 2016-08-05
  • 創建用戶、用戶組

    創建用戶、用戶組                  1解釋Linux的安全模型   2解釋用戶帳號和組群帳號的目的   3用戶和組管理命令   4理解并設置文件權限 5默認權限 6特殊權限 …

    Linux干貨 2016-08-08
  • 馬哥教育網絡班20期+第2周課程練習

    1、Linux上的文件管理類命令都有哪些,其常用的使用方法及其相關示例演示。 常用文件管理命令有:ls、mkdir、cd、pwd、cp、rm、mv、touch、cat、more、less、head、tail等 (1)ls命令:列出目錄內容 ls [OPTION]… [FILE]…  &nbs…

    Linux干貨 2016-06-29
欧美性久久久久