Linuxテキスト処理練習問題

9620 ワード

1、表示/proc/meminfoファイルの中でサイズsで始まる行;(要件:2つの方法を使用)
1つ目:
[root@localhost ~]# grep -i "^s" /proc/meminfo 
SwapCached:            0 kB
SwapTotal:       4194300 kB
SwapFree:        4194300 kB
Shmem:              9216 kB
Slab:              78280 kB
SReclaimable:      29356 kB
SUnreclaim:        48924 kB

2つ目:
[root@localhost ~]# egrep "^(S|s)" /proc/meminfo
SwapCached:            0 kB
SwapTotal:       4194300 kB
SwapFree:        4194300 kB
Shmem:              9216 kB
Slab:              78228 kB
SReclaimable:      29380 kB
SUnreclaim:        48848 kB

2、表示/etc/passwdファイルの中で/bin/bashで終わらない行
[root@localhost ~]# grep "/bin/bash$" /etc/passwd
root:x:0:0:root:/root:/bin/bash
nieda:x:1000:1000:nieda:/home/nieda:/bin/bash
mage:x:1001:1001::/home/mage:/bin/bash
wang:x:1002:1002::/home/wang:/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

........
3、ユーザーrpcのデフォルトのshellプログラムを表示する
[root@localhost ~]# grep "^\brpc\b" /etc/passwd|cut -d: -f7
/sbin/nologin

4、/etc/passwdの2桁または3桁を探し出す
[root@localhost ~]# egrep "\b[0-9]{2,3}\b" /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
nobody:x:99:99:Nobody:/:/sbin/nologin
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin
systemd-network:x:998:996:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin

........
5、表示/etc/grub 2.cfgファイルの少なくとも1つの空白文字で始まる空白文字以外の行
[root@localhost ~]# 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"

6、「netstat-tan」コマンドの結果、「LISTEN」の後に0、1または複数の空白文字で終わる行を見つける
[root@localhost ~]# netstat -ant|grep "\bLISTEN\b[[: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     
tcp        0      0 127.0.0.1:6010          0.0.0.0:*               LISTEN     
tcp6       0      0 :::22                   :::*                    LISTEN     
tcp6       0      0 ::1:631                 :::*                    LISTEN     
tcp6       0      0 ::1:25                  :::*                    LISTEN     
tcp6       0      0 ::1:6010                :::*                    LISTEN

7、ユーザーbash、testbash、basherおよびnologin(shellは/sbin/nologin)を追加し、/etc/passwdファイルのユーザー名とshell名の行を見つけます.
[root@localhost ~]# grep "^\b\([[:alnum:]]\+\)\b.*\b\1\b$" 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
[root@localhost ~]# grep "^\([[:alnum:]]\+\).*/\1$" 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

8、現在のシステムroot、mageまたはwangユーザーのUIDとデフォルトshellを表示する
[root@localhost ~]# grep -E "^\b(root|mage|wang)\b" /etc/passwd|cut -d: -f3,7
0:/bin/bash
1001:/bin/bash
1002:/bin/bash

9、/etc/rcを探し出す.d/init.d/functionsファイルの行の先頭は、下線を含む単語の後ろに括弧の行が付いています.
[root@localhost ~]# grep -o "^[[: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()
success()
failure()
passed()
warning()
action()
strstr()
is_ignored_file()
is_true()
is_false()
apply_sysctl()
[root@localhost ~]# egrep -o "^([[: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()
success()
failure()
passed()
warning()
action()
strstr()
is_ignored_file()
is_true()
is_false()
apply_sysctl()

10、egrepで/etc/rcを取り出す.d/init.d/functionsにおけるそのベース名
[root@localhost ~]# echo "/etc/init.d/functions" |grep -E -o "[^/]+$"
functions
[root@localhost ~]# echo "/etc/rc.d/init.d/functions" |tr '/' '
'|tail -1 functions·
[root@localhost ~]# basename /etc/rc.d/init.d/functions 
functions

11、egrepで上のパスのディレクトリ名を取り出す
[root@localhost ~]# echo "/etc/rc.d/init.d/functions" |egrep -o "/.*{1,}/"
/etc/rc.d/init.d/
[root@localhost ~]# dirname /etc/rc.d/init.d/functions 
/etc/rc.d/init.d

12.rootとして登録する各リモートホストIPアドレスの登録回数を統計する
[root@localhost ~]# who |grep "^root\b"|egrep -o "([0-9]{1,3}\.){3}[0-9]{1,3}"|sort|uniq -c
      4 10.1.250.91

13、拡張正規表現でそれぞれ0-9、10-99、100-19、200-249、250-255を表す
[root@localhost ~]# egrep -o "\b[0-9]\b" test  ###0-9
6
7
9
6
7
4
5
9
[root@localhost ~]# cat test
12 23 44 6 7 9 23  6 7 2b 34 4 5  10 9 99 213 34 45 231 199 
2346 45346 249 250 299 300 123 345 56758 12423 57 547 325 23
[root@localhost ~]# egrep -o "\b[0-9]{2}\b" test   ###10-99
12
23
44
23
34
10
99
34
45
57
23
[root@localhost ~]# egrep -o "\b1[0-9][0-9]\b" test
199
123
[root@localhost ~]# egrep -o "\b2[0-4][0-9]\b" test
213
231
249
[root@localhost ~]# egrep -o "\b25[0-5]\b" test
250

14.ifconfigコマンド結果のすべてのIPv 4アドレスを表示する
[root@localhost ~]# ifconfig |egrep "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b"|tr -s ' '|cut -d ' ' -f3
10.1.70.102
127.0.0.1
192.168.122.1

15、正規表現は生活中の番号にマッチする
携帯番号に合わせる
[root@localhost ~]# cat test
13900885386
15945482358
19525235558
18985958483
[root@localhost ~]# egrep "1[34578][0-9]{9}" test
13900885386
15945482358
18985958483

qqメールボックス
[root@localhost ~]# egrep "[0-9]{5,9}\@qq\.com" test
[email protected]
[root@localhost ~]# cat test
13900885386
15945482358
19525235558
18985958483
[email protected]
124156176@@qq.com
[email protected]
75656526@qqcom

マッチ×××番号:
×××番号の数値範囲と意味
http://baike.baidu.com/view/188003.htm?fromtitle=%E8%BA%AB%E4%BB%BD%E8%AF%81%E5%8F%B7%E7%A0%81&fromid=2135487&type=syn
[root@localhost ~]# egrep -o "(1[1-5]|2[1-3]|3[1-7]|4[1-6]|5[0-5]|6[1-5]|71|8[1-2])(0[1-9]|[1-6][0-9]|70)(0[1-9]|1[0-8]|2[1-9]|[3-9][0-9])[0-9]{4}((0[1,3,5,7,8]|1[0,2])(0[1-9]|[1-2][0-9]|3[0-1])|(0[4,6,9]|11)(0[1-9]|[1-2][0-9]|3[0-1])|02(0[1-9]|[1-2][0-9]))[0-9]{3}([0-9]|x)" a
440601198503262836
440305197005186134
632721197802191453
420114198109201171
330783197804282654
210422197007178073
370282198505288034
130921197409153670
211103198909115418
130229198306267293
330381198803231054
513221198408173119
140929197205245859
654027197703105551
411328198409102659
140727197307264299
653226197706175049
370781198809165827
411502197201226129
450481199002204846
510106198005117321
230715199004274022
340303198609101566
361127197304257404
141026197904281528
130121197504125869
441623197408283529
620105198306229342
320503198706241867
130404198608268308
230506198902183921
110229198902259325
640201198202204224
653101198505184805
[root@localhost ~]# cat a
440601198503262836
440305197005186134
632721197802191453
420114198109201171
330783197804282654
210422197007178073
370282198505288034
130921197409153670
211103198909115418
130229198306267293
330381198803231054
411000198606174894
371600197701165270
513221198408173119
140929197205245859
654027197703105551
411328198409102659
140727197307264299
653226197706175049
43060019900624166
370781198809165827
411502197201226129
450481199002204846
510106198005117321
230715199004274022
340303198609101566
361127197304257404
141026197904281528
130121197504125869
441623197408283529
620105198306229342
320503198706241867
450300197301179166
130404198608268308
230506198902183921
110229198902259325
640201198202204224
653101198505184805
120312828874712461
419874914912472317
418954194812419040
098634763161415612
398575673634683471

上揃え×××号は大雑把なマッチングにすぎず、閏年2月には28日しか認識できず、3桁のシリアルコードは地域によって派出所によって割り当てられた人数が異なるため無視され、後の1桁のチェックコード計算については上の接続を見ることができます(......)
一致IPアドレス:
[root@localhost ~]# ifconfig |grep -E "\b(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\b"
        inet 10.1.70.102  netmask 255.255.0.0  broadcast 10.1.255.255
        inet 127.0.0.1  netmask 255.0.0.0
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255

転載先:https://blog.51cto.com/11551196/1834970