文本處理課后小練習

1、找出ifconfig命令結果中本機的所有IPv4地址
[root@wzc date]# 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])'
10.1.253.22
255.255.0.0
10.1.255.255
127.0.0.1
255.0.0.0
192.168.122.1
255.255.255.0
192.168.122.255
2、查出分區空間使用率的最大百分比值
[root@wzc date]# df | tr -s ' '|cut -d' ' -f5|sort -n|tail -1|tr -d %
51
3、查出用戶UID最大值的用戶名、 UID及shell類型
[root@wzc date]# getent passwd |cut -d: -f1,3,7|sort -rnt: -k 2|head -n1
nfsnobody:65534:/sbin/nologin
4、查出/tmp的權限,以數字方式顯示
[root@wzc date]# stat /tmp/|head -n4|tail -n1|cut -d/ -f1|cut -d\( -f2
1777
5、統計當前連接本機的每個遠程主機IP的連接數,并按從大到小排序
[root@wzc date]# netstat -tn |cut -d: -f2|tr -s ' ' ':' |cut -d: -f2| sort |uniq -c|head -n1
      1 10.1.250.38
6、顯示/proc/meminfo文件中以大小s開頭的行;(要求:使用兩種方式)
[root@wzc date]# grep -E '^(S|s)' /proc/meminfo ;grep '^[sS]' /proc/meminfo
SwapCached:          100 kB
SwapTotal:       3905532 kB
SwapFree:        3905116 kB
Shmem:             10160 kB
Slab:             140636 kB
SReclaimable:      79632 kB
SUnreclaim:        61004 kB
SwapCached:          100 kB
SwapTotal:       3905532 kB
SwapFree:        3905116 kB
Shmem:             10160 kB
Slab:             140636 kB
SReclaimable:      79632 kB
SUnreclaim:        61004 kB
7、顯示/etc/passwd文件中不以/bin/bash結尾的行
[root@wzc date]# 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
8、顯示用戶rpc默認的shell程序
[root@wzc date]# getent passwd |grep '^rpc\b'|cut -d: -f7
/sbin/nologin
9、找出/etc/passwd中的兩位或三位數
[root@wzc date]# grep '\<[[:digit:]]\{2,3\}\>' /etc/passwd
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
10、顯示/etc/grub2.cfg文件中,至少以一個空白字符開頭的且后面存非空白字符的行
[root@wzc date]# grep "^[[:space:]]\+[^[:space:]].*" /etc/grub2.cfg 
  load_env
   set default="${next_entry}"
   set next_entry=
   save_env next_entry
   set boot_once=true
   set default="${saved_entry}"
  menuentry_id_option="--id"
  menuentry_id_option=""
  set saved_entry="${prev_saved_entry}"
11、找出"netstat -tan"命令的結果中以'LISTEN'后跟0、 1或多個空白字符結尾的行
[root@wzc date]# netstat -tan |grep "\bLISTEN[[: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
12、添加用戶bash、 testbash、 basher以及nologin(其shell為/sbin/nologin),而后找出/etc/passwd文件中用戶名同shell名的行
[root@wzc date]# useradd bash
[root@wzc date]# useradd testbash
[root@wzc date]# useradd basher
[root@wzc date]# useradd nologin -s /sbin/nologin 
[root@wzc date]#  getent passwd |grep "^\(\b.*\b\).*\<\1\>$"
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:1017:1026::/home/bash:/bin/bash
nologin:x:1020:1029::/home/nologin:/sbin/nologin
13、顯示當前系統root、 mage或wang用戶的UID和默認shell
[root@wzc date]# getent passwd|grep -e "^root\b" -e "^mage\b" -e "^wang\b"|cut -d: -f3,7
0:/bin/bash
1009:/bin/bash
1010:/bin/bash
14、找出/etc/rc.d/init.d/functions文件中行首為某單詞(包括下劃線)后面跟一個小括號的行
[root@wzc date]# grep -E "^([[:alpha:]_])+\(\).*" /etc/rc.d/init.d/functions 
checkpid() {
__pids_var_run() {
__pids_pidof() {
daemon() {
killproc() {
pidfileofproc() {
pidofproc() {
status() {
echo_success() {
echo_failure() {
echo_passed() {
echo_warning() {
update_boot_stage() {
15、使用egrep取出/etc/rc.d/init.d/functions中其基名
[root@wzc date]# echo /etc/rc.d/init.d/functions |egrep -o "[^/]+$"
functions
16、使用egrep取出上面路徑的目錄名
[root@wzc date]# echo "/etc/rc.d/init.d/functions" | egrep -o "^.*/"
/etc/rc.d/init.d/
17、統計以root身份登錄的每個遠程主機IP地址的登錄次數
[root@wzc date]# last |grep root|tr -s " "|cut -d" " -f3|grep -v ":"|uniq -c|tail -n1
     23 10.1.250.38
18、利用擴展正則表達式分別表示0-9、 10-99、 100-199、200-249、 250-255
[root@localhost ~]# 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]
19、用正則表達式表示手機號(11 13 17 15  18 )
[root@wzc date]# cat xiaomi 
11568973427
13546789524
15789342569
17789828269
18217968392
[root@wzc date]# egrep -o "1[13578][0-9]{9}" xiaomi
11568973427
13546789524
15789342569
17789828269
18217968392
22、使用cut把本機ip地址提取出來
  centos7版本
[root@wzc date]# ifconfig |head -n2 |tail -n1|cut -d. -f1-4|cut -dt -f2|cut -dn -f1
 10.1.253.22
  centos6版本
[root@localhost ~]# ifconfig |head -n 2|tail -n 1|cut -d: -f2|cut -dB -f1
10.1.253.24

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

(0)
RecallWzcRecallWzc
上一篇 2016-08-08
下一篇 2016-08-08

相關推薦

  • Linux基礎之計算機的組成及其功能

    1、描述計算機的組成及其功能: 答:計算機由 1.1、CPU(運算器、控制器)進行邏輯運算跟控制運算,寄存器、緩存 1.2、輸入單元(鍵盤、鼠標、手寫板等)、輸出單元(屏幕、打印機) 1.3、存儲器(內存、硬盤) 2、按系列羅列linux的發行版,并描述不同發行版之間的聯系與區別。 答:debian(ubuntu、mint、knopix)、slackware…

    2017-09-11
  • 2

    2

    Linux干貨 2018-03-26
  • grep,find用法-2

    1、顯示當前系統上root、fedora或user1用戶的默認shell; grep -E “^(root|fedora|user1)>” /etc/passwd | cut -d: -f1,7 [root@bogon Desktop]# grep -E “^(root|fedora|user1)\>” /etc/pass…

    Linux干貨 2017-08-04
  • Linux基本操作:關機 重啟

        Linux系統安裝完成以后,就要學會一些Linux的基本操作命令,首先是關機和重啟命令,如果進入Linux系統之后連關機重啟都不會是不是太不應該了,總不能關機或重啟的時候直接關閉電源在開機吧,這就顯得太不專業了,這樣也會對Linux系統主機不太好,一些進程還沒有關閉就直接斷電,會導致Linux系統的不穩定出現問題,何況也…

    Linux干貨 2016-08-04
  • Vim 末行模式 & crontab & scripts 練習

    1、復制/etc/rc.d/rc.sysinit文件至/tmp目錄,將/tmp/rc.sysinit文件中的以至少一個空白字符開頭的行的行首加#;   ~]# cp /etc/rc.d/rc.sysinit /tmp     %s@^[[:space:]]\+\*@#&@g     (使用元字符 有幾…

    Linux干貨 2016-10-31
欧美性久久久久