老李分享:《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. 安卓的UI界面开发(初学者推荐)

    一  随便扯扯 用户界面设计是程序开发的一项重要内容.在界面设计的过程中,需要考虑如何制作出UI界面,怎么样控制UI界面两大块. 任何有编程常识的人都知道:软件界面是开发者添加控件,编写控件控制逻辑, ...

  2. 2017-3-5 C#基础 函数

    函数/方法:非常抽象独立完成某项功能的一个个体 函数的作用: 提高代码的重用性提高功能开发的效率提高程序代码的可维护性 函数分为: 固定功能函数高度抽象函数 函数四要素:输入,输出,函数体,函数名 p ...

  3. Android网络开发实例(基于抓包实现的网络模拟登录,登出和强制登出)

    学习Android有几个月了,最近喜欢上了网络编程,于是想通过Android写一些一个小程序用于连接外网.在这里非常感谢雪夜圣诞的支持,非常感谢,给我打开新的一扇门. 1.声明,本程序只能用于西南大学 ...

  4. Maven 使用笔记

    一:手动添加包进本地仓库 Maven 安装 JAR 包的命令是: mvn install:install-file -Dfile=jar包的位置 -DgroupId=[groupId] -Dartif ...

  5. 读书笔记 effective c++ Item 31 把文件之间的编译依赖降到最低

    1. 牵一发而动全身 现在开始进入你的C++程序,你对你的类实现做了一个很小的改动.注意,不是接口,只是实现:一个私有的stuff.然后你需要rebuild你的程序,计算着这个build应该几秒钟就足 ...

  6. 基于 Koa平台Node.js开发的KoaHub.js连接打印机的代码

    最近好多小伙伴都在做微信商城的项目,那就给大家分享一个基于 Koa.js 平台的 Node.js web 开发的框架连接微信易联云打印机接口的代码,供大家学习.koahub-yilianyun 微信易 ...

  7. ConfigParser-- 读取写入配置文件

    基础读取配置文件   -read(filename)               直接读取文件内容 -sections()                      得到所有的section,并以列表 ...

  8. Thrift序列化与反序列化的实现机制分析

    Thrift是如何实现序死化与反序列化的,在IDL文件中,更改IDL文件中的变量序号或者[使用默认序号的情况下,新增变量时,将新增的变量不放在IDL文件的结尾,均会导致Thrift文件的反序列后无法做 ...

  9. Ext JS 6学习文档–第1章–ExtJS入门指南

    Ext JS 入门指南 前言 本来我是打算自己写一个系列的 ExtJS 6 学习笔记的,因为 ExtJS 6 目前的中文学习资料还很少.google 搜索资料时找到了一本国外牛人写的关于 ExtJS ...

  10. apply()

    apply() 1.apply和call的区别在哪里 2.什么情况下用apply,什么情况下用call 3.apply的其他巧妙用法(一般在什么情况下可以使用apply) apply:方法能劫持另外一 ...