第一部分 Shell基础编程——第五章 文本过滤

来源:互联网 发布:netty4.0.23 java版本 编辑:程序博客网 时间:2024/06/07 06:33

笔记

#文本过滤#正则表达式#基本元字符及其含义#使用句点匹配单字符#行首以^匹配字符串或字符序列#行尾以$匹配字符串或字符#用*匹配单字符或其重复序列#用\(反斜杠)屏蔽一个特殊字符#用[]匹配一个范围或集合#用\{\}匹配模式结构出现的次数#find命令#locate亦可查找文件#locate#查看find帮助信息man find#正则表达式需要双引号引出#没写路径默认当前路径#找出txt文档并打印#[root@localhost shell]# find -name "*.txt" -print  ./0410/list.txt./0410/names.txt./0321/enerco/1.txt./0321/enerco/2.txt./0321/enerco/3.txt./0321/enerco/4.txt./0321/myfile.txt./0321/myfile_dort.txt./0323/myfile.txt./0330/mylogfile.txt./0330/dos.txt./0330/ls.txt./0330/partation.txt./0330/name.txt./0330/err_message.txt./0330/term.txt./0330/account_new.txt./0330/standard.txt./other/1.txt./other/2.txt./other/3.txt./other/4.txt#和上条命令等效[root@localhost shell]# find ./-name "*.txt" –print#在当前目录下查找(包括子目录)包含大写字母的文件并打印[root@localhost shell]# find ./ -name "[A-Z]*" –print#查找权限为755的文件并打印[root@localhost shell]# find . -perm 755 -print#验证权限是否为755[root@localhost shell]# ls -l ./0330/file_desc-rwxr-xr-x    1 root     root          100 Mar 30 20:08 ./0330/file_desc#在当前目录查找属主为root的文件并打印[root@localhost shell]# find `pwd` -user root –print#验证属主是否为root[root@localhost shell]# ls -l /home/wgb/shell/0410/whilereadline-rwxr-xr-x    1 root     root           69 Apr 10 22:24 /home/wgb/shell/0410/whilereadline#查看属主不在etc/passwd文件里的文件并打印[root@localhost shell]# find `pwd` -nouser –print#查找文件系统下所有属主不在etc/passwd文件里的文件并打印[root@localhost shell]# find / -nouser -print>nouers.out#后台执行上诉命令[root@localhost shell]# nohup find / -nouser -print>nouers.out &[1] 4752#查找所属组名为root的文件并打印[root@localhost shell]# find ./ -group root –print#验证[root@localhost shell]# ls -l ./other/1.txt-rw-r--r--    1 root     root           93 Dec  7 16:23 ./other/1.txt#查找没有组的文件并打印[root@localhost shell]# find ./ -nogroup –print#查找改变时间在5天以内的文件并打印[root@localhost shell]# find /var -mtime -5 –print#验证[root@localhost shell]# ls -l /var/spool/slrnpull/news/clari/world/europe/iberia/.overview-rw-r--r--    1 news     news            0 Apr 13 18:11 /var/spool/slrnpull/news/clari/world/europe/iberia/.overview#查看当前日期,以验证[root@localhost shell]# dateSat Apr 13 18:48:07 CST 2013#查找改变时间在3天以前的文件并打印[root@localhost shell]# find /var -mtime +3 –print#验证[root@localhost shell]# ls -l /var/www/error/contact.html.var-rw-r--r--    1 root     root         1220 Jul 16  2002 /var/www/error/contact.html.var#查找改变时间在1天以内的文件[root@localhost shell]# find /var -mtime -1 –print#查看file1的详细信息[root@localhost shell]# ls -l ./0331/test/file1-rw-rw-r--    1 root     root            0 Mar 31 11:36 ./0331/test/file1#查看myfile的详细信息[root@localhost shell]# ls -l ./0410/myfile-rw-r--r--    1 root     root           18 Apr 10 21:51 ./0410/myfile#查找比file1新比myfile旧的文件并打印[root@localhost shell]# find `pwd` -newer "./0331/test/file1" ! -newer ./0410/"myfile" –print#验证[root@localhost shell]# ls -l /home/wgb/shell/0410/forlist4-rwxr-xr-x    1 root     root           67 Apr 10 21:49 /home/wgb/shell/0410/forlist4#查找etc目录下为目录的文件夹并打印[root@localhost shell]# find /etc -type d –print#查找etc目录下类型为连接的文件并打印[root@localhost shell]# find /etc -type l –print#查找大于1M的文件,c表示字节并打印[root@localhost shell]# find . -size +1000000c –print#大于10个块的文件并打印[root@localhost shell]# find . -size +10 –print#验证[root@localhost shell]# ls -l ./0330/ls.txt-rw-r--r--    1 root     root         6021 Mar 30 19:12 ./0330/ls.txt#先匹配所有的文件,再在子目录中查找[root@localhost shell]# find / -name "CON.FILE" -depth –print#注意空格,查找etc目录下为目录的文件夹并列出详细信息[root@localhost shell]# find . -type f -exec ls -l {} \;#删除5天前的log文件[root@localhost shell]# find . -name "*.log" -mtime +5 -ok rm{} \;[root@localhost shell]# ls -l /var/log#xargs:只有一个进程减少系统消耗#查找权限为777的文件并打印[root@localhost shell]# find ./ -perm -7 -print./0321/direactory1./0321/enercolns[root@localhost shell]# ls -l ./0321/enercolnslrwxrwxrwx    1 root     root           13 Mar 21 16:15 ./0321/enercolns -> enerco.tar.gz#将查找结果改为775[root@localhost shell]# find ./ -perm -7 -print | xargs chmod o-w#查看文件类型[root@localhost shell]# find ./ -type f -print | xargs file#grep 模式查找#在当前目录下所有txt文档中查找包含jenny的文件并打印[root@localhost shell]# grep "jenny" *.txt#在当前目录下所有文件中查找包含sort it的文件并打印[root@localhost shell]# grep "sort it" *#在myfile文件中统计2004出现的次数[root@localhost shell]# grep -c "2004" myfile2#在myfile文件中统计2004出现的次数并打印行号[root@localhost shell]# grep -n "2004" myfile#在myfile文件查找JUl并打印(不区分大小写)[root@localhost 0415]# grep -i "JUl" myfile#过滤,在myfile文件查找不包含2004:22的内容[root@localhost 0415]# grep -v "2004:22" myfile#在myfile文件查找在2004:22:50--22::22:59时间段的内容[root@localhost 0415]# grep  "2004:22:5[0-9]" myfile#在myfile文件查找不是210开头的内容[root@localhost 0415]# grep  "^[^210]" myfile#在myfile文件查找以H开头,P结尾的内容[root@localhost 0415]# grep  "H*P" myfile | more#在myfile文件查找匹配正则表达式[5-8][6-9][0-3]的内容[root@localhost 0415]# grep  "[5-8][6-9][0-3]" myfile|more#在myfile文件查找匹配正则表达式4\{2\}的内容[root@localhost 0415]# grep "4\{2\}" myfile443#在myfile文件查找匹配正则表达式4\{2,4\}的内容[root@localhost 0415]# grep "4\{2,4\}" myfile#在myfile文件查找所有空行[root@localhost 0415]# grep "^$" myfile#在myfile文件查找包含问号的内容[root@localhost 0415]# grep "\?" myfile[root@localhost 0415]# ls -l>lsout.txt#在lsout.txt文件中查找为目录的文件[root@localhost 0415]# grep "^d" lsout.txt##在lsout.txt文件中查找不是目录的文件[root@localhost 0415]# grep "^[^d]" lsout.txt##在lsout.txt文件中查找不是目录、连接、管道的文件[root@localhost 0415]# grep "^[^dlp]" lsout.txt##在myfile文件中查找500-599的数字[root@localhost 0415]# grep "5[[:digit:]][[:digit:]]" myfile#在myfile文件中查找IP地址,只能是3位数的[root@localhost 0415]# grep "[0-9]\{3\}\.[[0-9]\{3\}\.[0-9]\{3\}\.[0-9]\{3\}" myfile#在myfile文件中查找所有IP地址[root@localhost 0415]# grep "[0-9]\{1,3\}\.[[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}" myfile#在myfile文件中查找包含php的内容[root@localhost 0415]# grep "php" myfile #在myfile文件中统计配php文件的个数[root@localhost 0415]# grep "php" myfile| wc -l      0#在myfile文件中统计htm/call_info.php出现的次数[root@localhost 0415]# grep "htm/call_info.php" myfile| wc –l#AWK 工具#打印score.txt第一列的内容并将此写入score.out中[root@localhost 0415]# awk '{print $0}' score.txt |tee score.out898899100998999#将默认的域分隔符(空格)改为分号,功能同上[root@localhost 0415]# awk -F : '{print $0}' score.txt |tee score.out#打印score.txt第一列、第四列的内容并将此写入score.out中,并以制表符作为间隔,但是执行出错[root@localhost 0415]# awk -F : '{print $1\t$4}' score.txt |tee score.outawk: cmd. line:1: {print $1\t$4}awk: cmd. line:1:          ^ backslash not last character on line#\t是字符,用双引号括起来,功能同上[root@localhost 0415]# awk -F : '{print $1"\t"$4}' score.txt |tee score.out#分页显示,功能同上[root@localhost 0415]# awk -F : '{print $1"\t"$4}' score.txt |more#域分隔符可以任意[root@localhost 0415]# awk -F +0800 '{print $1}' score.txt|more#BEGIN和END的使用[root@localhost 0415]# awk 'BEGIN {print "Name Maths\n--------"} {print $1"\t"$4} END { print "end-of-report"}' score.txt#功能类似上条加上分页[root@localhost 0415]# awk 'BEGIN {print "IP Date\n--------"} {print $1"\t"$4} END { print "end-of-report"}' score.txt |more#致歉:第五章文本过滤1:18:34以后出错,不能播放,所以后面的内容暂时没有。


 

附图

 

 

 

 

 

 

 

 

 

 

 

katoonSina  CSDN@Wentasy 博文仅供参考,欢迎大家来访。如有错误之处,希望批评指正。原创博文如需转载请注明出处,谢谢 :) [CSDN博客]