老李分享:《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 ];

then

echo "Usage: $0 filename"

exit -1

fi

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

//5.实战举例

获取ntp同步的错误信息(假定当前设备没有联网)

举例一:ntpdate 8.8.8.8

15 Jan 07:28:26 ntpdate[7137]: bind() fails: Permission denied

举例二:ntpdate google.com

[root@localhost cutDemo]# ntpdate msf22.com
Error resolving msf22.com: Name or service not known (-2)
15 Jan 07:30:54 ntpdate[7169]: Can't find host msf22.com: Name or service not known (-2)
15 Jan 07:30:54 ntpdate[7169]: no servers can be used, exiting

想获取[71**]:后的出错信息,之前的删除。脚本如下:

[root@localhost cutDemo]# ntpdate msft22.com 2>&1  | sed 's/.*\]\: //g' 
Error resolving msft22.com: Name or service not known (-2)
Can't find host msft22.com: Name or service not known (-2)
no servers can be used, exiting

解释: ntpdate  msft22.com 2>&1  //2>&1 标准输错重定向到标准输出。

sed 's/.*\]\: //g'  //删除文件中"]:"前面的字符串。

5、awk工具,用于数据流,对列、行进行操作。

//1)、awk的实现方式

[root@localhost program_test]# echo -e "line1\nline2" | awk 'BEGIN { print "begin...\n" } { print } END { print "end...\n" }'
begin...

line1
line2
end...

//2)、awk实现累加求和

[root@localhost program_test]# seq 5 | awk 'BEGIN { sum=0; print "summary:" } { print $1"+"; sum+=$1; } END { print "=="sum }'

summary:
1+
2+
3+
4+
5+
==15

//3)、awk 设定定界符.

//-F 定界符  $NF 一行中的最后一个字段

[root@localhost program_test]# awk -F: '{ print $1 "\t" $NF }' /etc/passwd
root /bin/bash
bin /sbin/nologin
daemon /sbin/nologin

//4)、打印文件中的每个字母

[root@localhost program_test]# cat read_each_word.sh

cat hello.c | \

( while read line;

do

#echo $line;

for word in $line;

do

#echo $word;

for((i=0;i<${#word};i++))

do

echo ${word:i:1} ;

done

done

done )

//5)、打印第4-6行内容

[root@localhost program_test]# seq 100 | awk 'NR==4, NR==6'
4
5
6

//6)、awk实现类似tac逆序的功能.

[root@localhost program_test]# seq 9 | awk '{ lifo[NR]=$0; lno=NR } END { print "NR = " NR; for(;lno>-1;lno--) { print lifo[lno]; } }'

NR = 9

9

8

7

6

5

4

3

2

1

老李分享:《Linux Shell脚本攻略》 要点(四)的更多相关文章

  1. Linux Shell 脚本攻略学习--四

    linux中(chattr)创建不可修改文件的方法 在常见的linux扩展文件系统中(如ext2.ext3.ext4等),可以将文件设置为不可修改(immutable).某些文件属性可帮助我们将文件设 ...

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

    老李分享:<Linux Shell脚本攻略> 要点(八)   1.打印进程 [root@localhost program_test]# ps -e | head  PID TTY     ...

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

    老李分享:<Linux Shell脚本攻略> 要点(七)   1.显示给定文件夹下的文件的磁盘适用情况 [root@localhost program_test]# du -a -h ./ ...

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

    老李分享:<Linux Shell脚本攻略> 要点(六)   1.打印网络接口列表 [root@localhost touch_more]# ifconfig | cut -c-10 | ...

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

    老李分享:<Linux Shell脚本攻略> 要点(五)   //1.打包.解包 [root@localhost program_test]# tar -cf output.tar 11. ...

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

    老李分享:<Linux Shell脚本攻略> 要点(三)   1.生产任意大小的文件 [root@localhost dd_test]#[root@localhost dd_test]# ...

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

    老李分享:<Linux Shell脚本攻略> 要点(二)   poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课 ...

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

    老李分享:<Linux Shell脚本攻略> 要点(一)   第一章:Shell起步基础 1.变量:在bash中,每一个变量的值都是字符串.无论你给变量赋值时,有没有使用引号,值都会以字符 ...

  9. 读《Linux Shell脚本攻略》(第2版) 总结

    前段时间读完了<Linux Shell脚本攻略>(第2版)这本书,给部分想读这本书的人分享下个人感受. 说下这本书的难度吧.纯新手或者只懂少部分编程知识的人,读起来还是有很大难度的.以我为 ...

随机推荐

  1. 常用php时间函数用法汇总

    1.设置时区的方法: php5后都要自己设置时区,要么修改php.ini的设置,要么在代码里修改. 在PHP.INI中设置时区 date.timezone = PRC 在代码中设置时区 1 date_ ...

  2. 如何通过Visual Studio来管理我们的数据库项目

    某日的一个早晨,产品早上来告诉我说要把之前变更的一个功能更改回原来的设计内容,作为程序员大家都最讨厌需求来回反复变更,但是没有办法,苦逼的程序员最终还是继续要改,毕竟是给老板打工的,但是发现我们之前的 ...

  3. c#基础语句——循环语句(for、while、foreach)

    循环类型:for.while.foreach 循环四要素:初始条件-->循环条件-->循环体-->状态改变 1.for 格式: for(初始条件:循环条件:状态改变) {循环体(br ...

  4. failed (1113: No mapping for the Unicode character exists in the target multi-byte code page), client: 127.0.0.1...

    nginx部署网站后,访问域名,网页显示  500 Internal Server Error ,经查看发现nginx的error.log中有报错: failed (1113: No mapping ...

  5. select函数的用法

    首先介绍阻塞方式与非阻塞方式: 阻塞方式(block),就是进程或是线程执行到这些函数时必须等待某个事件的发生.如果事件没有发生,进程或线程就被阻塞,函数不能立即返回. 非阻塞方式(non-block ...

  6. 《深入理解Java虚拟机》学习笔记之工具

    善于利用工具,不仅可以加快我们分析数据,还可以快速定位和解决问题.现在我们就来看看虚拟机性能监控和故障处理工具. 在JDK的bin目录可以看到sun免费送给了我们很多小工具,这些工具虽然小巧但功能强大 ...

  7. 删除bin后,Eclipse重新编译项目

    今天做"用java.util.Properties类读写配置文件"Demo时,在编译项目时由于配置资源文件一起写入bin了.而Demo修改了配置文件,从新运行时配置文件不再更新,于 ...

  8. 学习HTML5一周的收获4

    /* [CSS常用文本属性]  * 1.字体.字号: font-weight:字体的粗细,可选属性值:bold加粗  lighter细体  100~900数值(400正常,700 bold)   fo ...

  9. Objective-C日记-之类别Category

    类别Category 1,概述 为现有类添加新的方法,这些新方法的Objective-C的术语为“类别”. 2,用法 a,声明类别 @interface NSString(NumberConvenie ...

  10. 使用Three.js网页引擎创建酷炫的3D效果的标签墙

    使用Three.js引擎(这是开源的webgl三维引擎,gitgub)进行一个简单应用. 做一个酷炫的3d效果的标签墙(已经放在我的博客首页,大屏幕可见), 去我的博客首页看看实际效果 www.son ...