《Linux Shell脚本攻略》 笔记 第四章:高效文本处理

简介: 《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

作者:铭毅天下

转载请标明出处,原文地址:http://blog.csdn.net/laoyang360/article/details/42364823

相关文章
|
1天前
|
网络协议 Shell Linux
LabVIEW 在NI Linux实时设备上访问Shell
LabVIEW 在NI Linux实时设备上访问Shell
|
1天前
|
分布式计算 Hadoop Shell
使用shell脚本实现自动SSH互信功能
使用shell脚本实现自动SSH互信功能
10 1
|
2天前
|
Unix Shell Linux
轻松编写 AIX Shell 脚本
轻松编写 AIX Shell 脚本
10 1
|
2天前
|
Shell Linux
【Linux】进程实践项目(更新中) — 自主shell编写
前几篇文章,我们学习进程的相关知识:进程概念,进程替换,进程控制。熟悉了进程到底是个什么事情,接下来我们来做一个实践,来运用我们所学的相关知识。这个项目就是手搓一个shell模块,模拟实现Xshell中的命令行输入。
9 1
|
3天前
|
监控 关系型数据库 Shell
Shell脚本入门:从基础到实践,轻松掌握Shell编程
Shell脚本入门:从基础到实践,轻松掌握Shell编程
|
3天前
|
关系型数据库 MySQL Shell
在Centos7中利用Shell脚本:实现MySQL的数据备份
在Centos7中利用Shell脚本:实现MySQL的数据备份
|
3天前
|
Shell Linux 信息无障碍
5 个有用的 Linux Shell 转义序列
5 个有用的 Linux Shell 转义序列
|
4天前
|
Shell Linux 编译器
C语言,Linux,静态库编写方法,makefile与shell脚本的关系。
总结:C语言在Linux上编写静态库时,通常会使用Makefile来管理编译和链接过程,以及Shell脚本来自动化构建任务。Makefile包含了编译规则和链接信息,而Shell脚本可以调用Makefile以及其他构建工具来构建项目。这种组合可以大大简化编译和构建过程,使代码更易于维护和分发。
21 5
|
Shell Linux
Linux中常用的文本处理命令(echo、sort、uniq、tr、cut、split、eval)(上)
1、echo命令——输出 echo 命令主要用来显示字符串信息。
328 0
|
机器学习/深度学习 Linux Shell
Linux 基础-文本处理命令
Linux 基础-文本处理命令
147 0