您的位置:首页 > 运维架构 > Linux

老李分享:《Linux Shell脚本攻略》 要点(四)

2015-12-15 18:00 537 查看

老李分享:《Linux Shell脚本攻略》 要点(四)

1、IP地址的正则表达式: [0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}2、grep用法//在多级目录中对文本进行递归检索[root@localhost program_test]# grep "yang" ./ -Rn./test.txt:6:laoyang
./right.txt:1:1 yang man//忽略大小写匹配[root@localhost program_test]# echo hello world | grep -i "HELLO"
hello world//递归搜索所有.c和.cpp文件[root@localhost program_test]# grep "main()" . -r --include *.{c,cpp}
./hello.c:int main()
sin.c:int main()
hello.cpp:int main() //匹配某个结果之后的几行[root@localhost program_test]# echo -e "a\nb\nc\na\nb\nc"| grep a -A 1
a
b
--
a
b 3、cut命令cut,将文本按照列进行切割的小工具。//-d分界符; -f要提取的列[root@localhost program_test]# cut -d ":" -f5 --complement passwd_yang
root:x:0:0:/root:/bin/bash
bin:x:1:1:/bin:/sbin/nologin [root@localhost program_test]# cut -c1-5 passwd_yang
root:
bin:x
daemo
adm:x //统计特定文件中的词频[root@localhost program_test]# cat word_freq.sh#!/bin/bash if [ $# -ne 1 ];thenecho "Usage: $0 filename"exit -1fi filename=$1 egrep -o "\b[[:alpha:]]+\b" $filename | \awk '{ count[$0]++ } \END { printf("%-14s%s\n","word","Count");\for(ind in count) \{ printf("%-14s%d\n",ind,count[ind]); } }'4、sed命令(stream editor 流编辑器)适用文本处理.//1.替换,从第3个开始替换[root@localhost program_test]# echo this thisthisthis | sed 's/this/THIS/3g'
this thisTHISTHIS//2.删掉空白行[root@localhost program_test]# sed '/^$/d' choice.sh //3.已匹配的字符串标记&[root@localhost program_test]# echo this is an example | sed 's/\w\+/[&]/g'
[this] [is] [an] [example] //4.替换举例.[root@localhost program_test]# cat sed_data.txt
11 abc 111 this 9 file contains 111 11 88 numbers 0000
[root@localhost program_test]# cat sed_data.txt | sed 's/\b[0-9]\{3\}\b/NUMBER3/g'
11 abc NUMBER3 this 9 file contains NUMBER3 11 88 numbers 0000
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  软件测试开发