LINUX文本批处理工具(正则、grep、awk、sed)
命令行快捷键
- 通配符扩展
* – 匹配0或者多个字符
? – 匹配任意单个字符
[0-9] – 匹配0-9范围内的数字
[abc] – 匹配该列表内的任意字符
[^abc] – 匹配除列表内字符外的所有字符
[root@localhost test]# ll total 0 [root@localhost test]# touch {1..5}.txt [root@localhost test]# ll *.txt -rw-r--r--. 1 root root 0 Jun 27 21:36 1.txt -rw-r--r--. 1 root root 0 Jun 27 21:36 2.txt -rw-r--r--. 1 root root 0 Jun 27 21:36 3.txt -rw-r--r--. 1 root root 0 Jun 27 21:36 4.txt -rw-r--r--. 1 root root 0 Jun 27 21:36 5.txt [root@localhost test]# rm -f {1,5}.txt [root@localhost test]# ll total 0 -rw-r--r--. 1 root root 0 Jun 27 21:36 2.txt -rw-r--r--. 1 root root 0 Jun 27 21:36 3.txt -rw-r--r--. 1 root root 0 Jun 27 21:36 4.txt
命令行扩展
history
~
$()
[root@localhost test]# hostname localhost.localdomain [root@localhost test]# echo This system’s name is $(hostname) This system’s name is localhost.localdomain•Ctrl-a:移动到行的开始位置
•Ctrl-e:移动到行的最后位置
•Ctrl-u:删除到行的开始位置
•Ctrl-k:删除到行的最后位置
•Ctrl-方向键:逐个字符移动到左边或者右边
正则表达式
/ 定界符,标示正则表达式开始和结束的字符
. 匹配任意单个字符
[] 匹配方括号内的任意一个字符 [a-z] [a-zA-Z][0-9][0..3]
[^]标示匹配除了括号内的内容
* 匹配0个或者多个字符
\ 转移字符,把后面的第一个字符做普通字符用 \*=*
\<\> 匹配括号里的单词,\<with\>指包括with的行
^ 匹配一行的开始
$ 匹配一行的结束
x{m,n\} 匹配包括m--n个x的行
\( \) 转义圆括号,可以把正则表达式包括进来
\{10\} 表示通配10位
文本搜索工具grep
[root@www ~]# grep [-acinv] [--color=auto] '
搜寻字符串' filename 选项与参数:
-a :将 binary 文件以 text 文件的方式搜寻数据
-c :计算找到 '搜寻字符串' 的次数
-i :忽略大小写的不同,所以大小写视为相同
-n :顺便输出行号
-v :反向选择,亦即显示出没有 '搜寻字符串' 内容的那一行!
--color=auto :可以将找到的关键词部分加上颜色的显示喔!
(1)多个文件查询
grep "sort" *.doc #见文件名的匹配
(2)行匹配:输出匹配行的计数
grep -c "48" data.doc #输出文档中含有48字符的行数
(3)显示匹配行和行数
grep -n "48" data.doc #显示所有匹配48的行和行号
(4)显示非匹配的行
grep -vn "48" data.doc #输出所有不包含48的行
(5)大小写敏感
grep -i "ab" data.doc #输出所有含有ab或Ab的字符串的行
grep -i "^root" words //root开头的行
grep -i "root$" words //root结尾的行
grep -i "^a.\{5\}*" words //a开头有五个字符的行
grep -i "^a.\{5\}$" words //a开头长度为5的行
grep "^.\{9\}z$" words //总共10位,z结尾的行
文本批处理工具awk
在awk执行之前设定分界符:,awk执行结束打印search over [root@sky dict]# awk 'BEGIN {FS=":"} {print $1 "\t" $5} END {print "search over"}' /etc/passwd root root bin bin daemon daemon adm adm lp lp sync sync shutdown shutdown [root@sky dict]# awk -F: '{print $1}' /etc/passwd root bin daemon adm lp sync shutdown [root@sky etc]# ll | awk '{print $9}' abrt adjtime aliases aliases.db alsa alternatives anacrontab [root@sky etc]# awk '$1 ~/root/{print $1}' passwd root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin [root@sky etc]# awk -F: '$7 !~/sbin/{print $7}' passwd /bin/bash /bin/sync /bin/bash [root@sky etc]# awk -F: '$7 !~/sbin/{print $7 "\t" length($7)}' passwd /bin/bash 9 /bin/sync 9 /bin/bash 9 [root@sky etc]# awk -F: '$7 !~/sbin/{print toupper($7)}' passwd /BIN/BASH /BIN/SYNC /BIN/BASH [root@sky etc]# ll | awk '$5>5000 {print $5 "\t" $9}' 2231525 333.txt 12288 aliases.db 12288 brltty 21929 brltty.conf 5090 DIR_COLORS 5725 DIR_COLORS.256color 25213 dnsmasq.conf 6088 kdump.conf 71492 ld.so.cache 5122 makedumpfile.conf.sample 5171 man_db.conf [root@sky etc]# ll | awk '$5>5000 {print length($9) "\t" $9}' 7 333.txt 10 aliases.db 6 brltty 11 brltty.conf 10 DIR_COLORS 19 DIR_COLORS.256color 12 dnsmasq.conf 10 kdump.conf [root@sky etc]# ll | awk 'NR==200,NR==210 {print}' -rw-r--r--. 1 root root 458 2月 12 2014 rsyncd.conf -rw-r--r--. 1 root root 3232 3月 26 2014 rsyslog.conf drwxr-xr-x. 2 root root 77 11月 30 22:39 rsyslog.d -rw-r--r--. 1 root root 927 4月 2 2014 rwtab drwxr-xr-x. 2 root root 6 4月 2 2014 rwtab.d drwxr-xr-x. 2 root root 35 11月 30 22:32 samba drwxr-xr-x. 3 root root 4096 11月 30 22:36 sane.d drwxr-xr-x. 2 root root 62 11月 30 22:40 sasl2 drwxr-xr-x. 3 root root 32 11月 30 22:41 scl -rw-------. 1 root root 233 12月 1 10:20 securetty drwxr-xr-x. 6 root root 4096 11月 30 22:29 security [root@sky etc]# cat test.txt 1 2 3 4 5 6 7 8 9 [root@sky etc]# cat 111 { if ($1<5) {print $1*10000} else {print $1*$1} } [root@sky etc]# awk -f 111 test.txt 10000 20000 30000 40000 25 36 49 64 81 0 [root@sky etc]# [root@sky etc]# cat 222 BEGIN{ n=1 while(n<10) {print n"*"n"="n*n n++} } [root@sky etc]# awk -f 222 1*1=1 2*2=4 3*3=9 4*4=16 5*5=25 6*6=36 7*7=49 8*8=64 9*9=81 [root@sky etc]# [root@sky ~]# cat 333 BEGIN{ for(i=1;i<10;i++) { if(i%3==0) continue print i } } [root@sky ~]# awk -f 333 1 2 4 5 7 8 [root@sky ~]# cat 333 BEGIN{ for(i=1;i<10;i++) { if(i%3==0) print i } } [root@sky ~]# awk -f 333 3 6 9
文本批处理工具sed
sed选项
-e 表示用command命令来处理输入文件
-f 表示用command文件来处理输入文件
-n 表示不使用默认打印,打印的行由command命令决定
sed打印操作p:
[root@sky ~]# sed -n '1,5p' axa.txt //显示前5行 1*1=1 2*2=4 3*3=9 4*4=16 5*5=25 [root@sky ~]# sed -n '6,/00/p' axa.txt //显示第6行到包括00的行 6*6=36 7*7=49 8*8=64 9*9=81 10*10=100 [root@sky ~]# sed -n '6,/^10/p' axa.txt //显示第6行到10开头的行 6*6=36 7*7=49 8*8=64 9*9=81 10*10=100
sed删除操作d:
[root@sky ~]# sed -e '90,$d' axa.txt > axa90.txt //删除90行到最后一行 [root@sky ~]# tail -3 axa98.txt 96*96=9216 97*97=9409 98*98=9604 [root@sky ~]# sed -e '99d' axa.txt > axa98.txt //删除第99行 [root@sky ~]# tail -3 axa98.txt 96*96=9216 97*97=9409 98*98=9604 [root@sky ~]# sed -e '90,$d' axa.txt > axa90.txt [root@sky ~]# tail -3 axa90.txt 87*87=7569 88*88=7744 89*89=7921 [root@sky ~]# sed -e '/^[1-8]/d' axa.txt > ax9.txt //删除开头是1---8的行 [root@sky ~]# cat ax9.txt 9*9=81 90*90=8100 91*91=8281 92*92=8464 93*93=8649 94*94=8836 95*95=9025 96*96=9216 97*97=9409 98*98=9604 99*99=9801
sed替换命令s:
替换的3种方式(1、n --替换第n个 2、g--全局替换 3、p将所有替换操作的行输出到标准输出)
[root@sky ~]# sed -n -e 's/*/ X /p' ax9.txt //替换*为 X 9 X 9=81 90 X 90=8100 91 X 91=8281 92 X 92=8464 93 X 93=8649 94 X 94=8836 95 X 95=9025 96 X 96=9216 97 X 97=9409 98 X 98=9604 99 X 99=9801 [root@sky ~]# sed -e 's/\<te\>/tete/g' aaa.txt //替换单词te为tete tete a a tea a a is tete [root@sky ~]# sed -e 's/\<a\>/aaa/g' aaa.txt //替换单词a为aaa tea aaa aaa is te [root@sky ~]# cat aaa.txt te a a tea a a is te [root@sky ~]# [root@sky ~]# sed -e ' /tea/ s/\<a\>/aaa/g' aaa.txt //在包括tea的行,替换a为aaa te a a tea aaa aaa is te [root@sky ~]#
sed插入命令 i a
i 在指定行前面插入
a 在指定行后面插入
[root@sky ~]# sed -e '5,10 i\this is sed command' ax9.txt //第5行--第10行,行前插入... 9*9=81 90*90=8100 91*91=8281 92*92=8464 this is sed command 93*93=8649 this is sed command 94*94=8836 this is sed command 95*95=9025 this is sed command 96*96=9216 this is sed command 97*97=9409 this is sed command 98*98=9604 99*99=9801 [root@sky ~]# sed -e '/=8/ i\this is sed command' ax9.txt //在包含=8的行,行前插入.... this is sed command 9*9=81 this is sed command 90*90=8100 this is sed command 91*91=8281 this is sed command 92*92=8464 this is sed command 93*93=8649 this is sed command 94*94=8836 95*95=9025 96*96=9216 97*97=9409 98*98=9604 99*99=9801 [root@sky ~]#