文本處理工具

1、grep

2、正則表達式

3、擴展正則表達式

4、vim

###########################

cat -A

cat -ns

cat -b

tac

less : pgup; pgdown

? n/N

/

openssl rand -base64 100 |tr -dc "[:alnum:]"|head -c12

[root@centos6 /app]#openssl rand -base64 100 |tr -dc "[:alnum:]"|head -c12
4oa9aPP1tr9I


tail -f 類似:tailf 

tail -F 跟蹤文件名

date -d "-1day"

[root@centos6 /app]#cut -d : -f 1,3 /etc/passwd 

df |grep ^/dev/sd |sed -r "s/.*[ ]+([0-9]{1,3})%.*/\1/"
df -i  |grep ^/dev/sd |sed -r "s/.*[ ]+([0-9]{1,3})%.*/\1/"
[root@node4~]#ifconfig  eth0  |head -n2 |tail -n1 |tr -s " "  |cut -d " " -f3
192.168.137.47
[root@centos6 ~]#ifconfig  eth0  |head -n2 |tail -n1 |cut -d ":" -f2 |cut -d " " -f1
192.168.137.6

[root@node4/app]#cut -d: -f1,3 /etc/passwd  |sort -t:  -k2 -n

root:0

bin:1

daemon:2

adm:3

lp:4


uniq -c 

[root@node4/app]#cat /var/log/httpd/access_log |cut -d " " -f1  |sort |uniq -c |sort -nr 

      7 192.168.137.1

      2 192.168.0.118


diff 

patch 

[root@node4/app]#cat f1 
whereis
[root@node4/app]#cat f2
whosi
[root@node4/app]#diff -u f1 f2 > diff.log
[root@node4/app]#rm -f f1
[root@node4/app]#patch -b f2  diff.log 
patching file f2
Reversed (or previously applied) patch detected!  Assume -R? [n] y
[root@node4/app]#mv f2 f1 
[root@node4/app]#mv f2.orig  f2 
[root@node4/app]#cat f1 f2
whereis
whosi


linux  文本處理三劍客:

grep :

man grep  


grep -nA3 root /etc/passwd

grep -c 

[root@node4/app]#grep -nA3 root /etc/passwd


1:root:x:0:0:root:/root:/bin/bash
2-bin:x:1:1:bin:/bin:/sbin/nologin
3-daemon:x:2:2:daemon:/sbin:/sbin/nologin
4-adm:x:3:4:adm:/var/adm:/sbin/nologin
--
10:operator:x:11:0:operator:/root:/sbin/nologin
11-games:x:12:100:games:/usr/games:/sbin/nologin
12-ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
13-nobody:x:99:99:Nobody:/:/sbin/nologin
[root@node4/app]#nmap -v -sP 192.168.137.0/24  |grep  -B1 'up '  
Nmap scan report for 192.168.137.1 (192.168.137.1)
Host is up (0.00089s latency).
--
Nmap scan report for 192.168.137.6 (192.168.137.6)
Host is up (0.00062s latency).
[root@node4/app]#cat nmap.log   |grep report  |awk '{print $5}' 
192.168.137.1
192.168.137.6


grep -e 表示或的關係:

grep -e root -e bash

過濾單詞: 

[root@node4/app]#echo "x-abc-y" |grep abc  

x-abc-y

man 7 regex 


字符匹配:
. 匹配任意單個字符
[] 匹配指定範圍內的任意單個字符
[^] 匹配指定範圍外的任意單個字符
[:alnum:] 字母和數字
[:alpha:] 代表任何英文大小寫字符,亦即 A-Z, a-z
[:lower:] 小寫字母 [:upper:] 大寫字母
[:blank:] 空白字符(空格和製表符)
[:space:] 水平和垂直的空白字符(比[:blank:]包含的範圍廣)
[:cntrl:] 不可打印的控制字符(退格、刪除、警鈴...)
[:digit:] 十進制數字 [:xdigit:]十六進制數字
[:graph:] 可打印的非空白字符
[:print:] 可打印字符
[:punct:] 標點符號

匹配次數:用在要指定次數的字符後面,用於指定前面的字符要出現的次數
* 匹配前面的字符任意次,包括0次
貪婪模式:儘可能長的匹配
.* 任意長度的任意字符
\? 匹配其前面的字符0或1次 
\+ 匹配其前面的字符至少1次 
\{n\} 匹配前面的字符n次 
\{m,n\} 匹配前面的字符至少m次,至多n次 
\{,n\} 匹配前面的字符至多n次 
\{n,\} 匹配前面的字符至少n次

位置錨定:定位出現的位置
^ 行首錨定,用於模式的最左側
$ 行尾錨定,用於模式的最右側
^PATTERN$ 用於模式匹配整行
^$ 空行
^[[:space:]]*$ 空白行
\< 或 \b 詞首錨定,用於單詞模式的左側
\> 或 \b 詞尾錨定;用於單詞模式的右側
\<PATTERN\> 匹配整個單詞


[root@node4~]#df |grep /dev/sd  |grep  -o "[0-9]\+%" |grep -o "[0-9]\+" |sort -nr |head -n1

29


[root@node4~]#echo rootrbbt |grep "\([r..t]\).*\1"

rootrbbt

[root@node4~]#echo rootrbbt |grep "\(r..t\).*\1"  

[root@node4~]#echo rootroot |grep "\(r..t\).*\1"  

rootroot

[root@node4~]#cat /etc/passwd |grep "^\(.*\):.*/\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

[root@node4~]#cat /etc/passwd |egrep "^(.*):.*/\1$" 

axy

[root@node4~]#echo axy |egrep "(a|b)xy"

axy

[root@node4~]#cat /etc/redhat-release|grep -o " [0-9]\+." |grep -o "[0-9]\+"                 

7

[root@node4~]#cat /etc/centos-release |grep -o "[0-9]\+"  |head -n1

7


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章