Detailed Shell Programming Three Musketeers

Contains many text processor or text editor in Linux / UNIX systems, which grep, sed and awk text processing tools are often used in shell programming, therefore, the industry of a wide range of people called "shell programming Three Musketeers " .

grep command tool

grep command tools in their daily lives, will be frequently used, there is not much to say, if you have friends do not understand, please refer Bowen: regular expressions Shell scripts use the Detailed , which detailed the grep command related parameters and use,

sed command tool

sed is a powerful and simple text analysis and conversion tool that can read the text, and edit text according to the specified conditions, the final output all lines live output processing only certain lines, sed can be achieved in the absence of interaction situations rather complex text processing operations . It is widely used in shell scripts for automated processing to complete various tasks.

sed的工作流程主要包括:
1. 读取:sed从输入流中读取一行内容不能够存储到临时的缓冲区中;
2. 执行:默认情况下所有的sed命令都在模式空间中按顺序地执行,除非指定了行的地址,否则sed命令将会再所有行上依次执行;
3. 显示:发送修改后的内容到输出流,再发送数据后,模式空间将会被清空。
注意:在所有的文件内容都被处理完成之前,上述过程将重复执行,直至所有内容都被处理完。

1) sed command syntax and parameters:

sed [选项] '操作'  参数
或
sed [选项] -f scriptfile 参数

The main parameters common sed command options:
Detailed Shell Programming Three Musketeers
If you require to be modified between the first few lines to a few lines, etc., common operating parameters include:
Detailed Shell Programming Three Musketeers

2) sed command usage examples (note that the following actions will not change the content of the file itself, if you need to modify must take "-i" option)

1. Output matching texts
[root@localhost ~]# sed -n 'p' test.txt 
//输出所有内容,等同于“cat test.txt”

Detailed Shell Programming Three Musketeers

[root@localhost ~]# sed -n '3p' test.txt 
//输出第三行内容

Detailed Shell Programming Three Musketeers

[root@localhost ~]# sed -n '3,5p' test.txt 
//输出3~5行

Detailed Shell Programming Three Musketeers

[root@localhost ~]# sed -n 'p;n' test.txt
//输出所有奇数行,n表示读入下一行数据

Detailed Shell Programming Three Musketeers

[root@localhost ~]# sed -n 'n;p' test.txt 
//输出所有偶数行,n表示读入下一行数据

Detailed Shell Programming Three Musketeers

[root@localhost ~]# sed -n '1,5{p;n}' test.txt 
//输出第1行~第5行之间的奇数行(第1、3、5行)

Detailed Shell Programming Three Musketeers

[root@localhost ~]# sed -n '10,${n;p}' test.txt
//输出第10行至文件尾部之间的偶数行(包括空行)

Detailed Shell Programming Three Musketeers

sed command and regular expressions with case used
sed command in conjunction with a regular expression, the format is slightly different, the regular expression to "/" surrounded .

[root@localhost ~]# sed -n '/the/p' test.txt
//输出包含“the”的行

Detailed Shell Programming Three Musketeers

[root@localhost ~]# sed -n '4,/the/p' test.txt
//输出从第4行到都第一个包含“the”的行

Detailed Shell Programming Three Musketeers

[root@localhost ~]# sed -n '/the/=' test.txt
//输出包含“the”的行所在的行号(等号(=)用来输出行号)

Detailed Shell Programming Three Musketeers

[root@localhost ~]# sed -n '/^PI/p' test.txt
//输出以“PI”开头的行

Detailed Shell Programming Three Musketeers

[root@localhost ~]# sed -n '/\<wood\>/p' test.txt 
//输出包含单词wood的行,\<、\>代表单词边界

Detailed Shell Programming Three Musketeers

2. Delete the matching texts

Nl command is used to calculate the number of lines in the file

[root@localhost ~]# nl test.txt | sed '3d'
//删除第3行

Detailed Shell Programming Three Musketeers

[root@localhost ~]# nl test.txt | sed '3,5d'
//删除第3~5行

Detailed Shell Programming Three Musketeers

[root@localhost ~]# nl test.txt | sed '/cross/d'
//删除包含cross的行,原本的第8行被删除

Detailed Shell Programming Three Musketeers

[root@localhost ~]# nl test.txt | sed '/cross/! d'
//删除不包含cross的行

Detailed Shell Programming Three Musketeers

[root@localhost ~]# sed '/\.$/d' test.txt 
//删除以“.”结束的行
[root@localhost ~]# sed '/^$/d' test.txt 
//删除所有空行
[root@localhost ~]# sed -e '/^$/{n;/^$/d}' test.txt
//删除空行,连续的空行留一个
3. Replace the qualifying text

Option will need to be replaced using sed command operation: S (string replacement), c (entire row / replacement block), y (character conversion ) command option and the like. Because the test file does not meet the requirements, the following is not a screenshot.

[root@localhost ~]# sed 's/the/THE/' test.txt
//将每行中的第一个the替换为THE
[root@localhost ~]# sed 's/l/L/2' test.txt
//将每行中的第三个“l”替换为“L”
[root@localhost ~]# sed 's/the/THE/g' test.txt 
//将文件中所有的“the”替换为“THE”
[root@localhost ~]# sed 's/o//g' test.txt 
//将文件中所有的“o”删除
[root@localhost ~]# sed 's/^/#/' test.txt 
//在每行的行首插入“#”号
[root@localhost ~]# sed '/the/s/^/#/' test.txt 
//在包含“the”的每行行首插入“#”号
[root@localhost ~]# sed 's/$/EOF/' test.txt 
//在每行行尾插入字符串“EOF”
[root@localhost ~]# sed '3,5s/the/THE/g' test.txt 
//将第3~5行中的所有“the”替换为“THE”
[root@localhost ~]# sed '/the/s/o/O/g' test.txt 
//将包含“the”的所有行中的o替换为“O”
4. Migration qualifying text

使用sed命令进行迁移文本操作时需要用到的选项有:g、G将剪贴板中的数据覆盖/追加到指定行;w保存为文件;r读取指定文件;a追加指定内容。

[root@localhost ~]# sed '/the/{H;d};$G' test.txt 
//将包含“the”的行迁移到文件末尾,“;”用于多个操作
[root@localhost ~]# sed '1,5{H;d};17G' test.txt 
//将第1~5行的内容转移到第17行后
[root@localhost ~]# sed '/the/w out.file' test.txt 
//将包含“the”的行另存为文件out.file
[root@localhost ~]# sed '/the/r /etc/hostname' test.txt 
//将文件/etc/hostname的内容添加到包含“the”的每行以后
[root@localhost ~]# sed '3aNEW' test.txt 
//在第3行后面插入一个新行,内容为“NEW”
[root@localhost ~]# sed '/the/aNEW' test.txt 
//在包含“the”的每行后插入一个新行,内容为“NEW”
[root@localhost ~]# sed '3aNEW1\nNEW2' test.txt
//在第3行后面多行内容,中间的“\n”表示换行
5.使用脚本编辑文件

使用sed脚本,将编辑指令存放到文件中(每行一条标记指令),通过“-f”选项来调用。

[root@localhost ~]# sed '1,5{H;d};17G' test.txt
//将第1~5行内容转移至第17行后

Detailed Shell Programming Three Musketeers
以上操作转换为脚本文件方式:

[root@localhost ~]# vim 1.list
1,5H
1,5d
17G
[root@localhost ~]# sed -f 1.list test.txt

Detailed Shell Programming Three Musketeers

6.sed直接操作文件示例

编写一个脚本,用来调整vsftpd服务配置:禁止匿名用户,但允许本地用户(也允许写入)登录。

[root@localhost ~]# vim local_only_ftp.sh
#!/bin/bash
S="/usr/share/doc/vsftpd-3.0.2/EXAMPLE/INSERNET_SITE/vsftpd.conf"
C="/etc/vsftpd/vsftpd.conf"
#指定样本文件路径、配置文件路径
[ ! -e "$C.bak" ] && cp $C $C.bak
#备份原来的配置文件,检测(配置文件.bak)是否存在,如果不存在则使用cp命令复制
sed -e '/^anonymous_enable/s/YES/NO/g' $S > $C
sed -i -e '/^local_enable/s/NO/YES/g' -e '/^write_enable/s/NO/YES/g' $C
grep "listen" $C || sed -i '$alisten=YES' $A
#基于样本配置进行调整,覆盖现有文件
systemctl restart vsftpd
systemctl enable vsftpd
#重启ftp服务,并设置为开机自启动

awk命令工具

在Linux/UNIX系统中,awk是一个功能强大的编辑工具,逐行读取输入文本,并根据指定的匹配模式进行查找,对符合条件的内容进行格式化输出或者过滤处理,可以在无交互的情况下实现相当复杂的文本操作,被广泛应用于Shell脚本,完成各种自动化配置任务。

1.awk命令的语法及概述
awk 选项  '模式或条件 { 编辑指令 }' 文件1 文件2 …  
//过滤并输出文件符合条件的内容
awk  -f  脚本文件 文件1 文件2 …
//从脚本中调用编辑指令,过滤并输出内容

*awk执行结果可以通过print的功能将字段数据打印显示。在使用awk命令的过程中,可以使用逻辑操作符“&&”和“||”;
也可以进行简单的数学运算,如+ 、-、
、/、%、^分别表示加、减、乘、除、取余、乘方。**

awk从输入文件或者标准输入中读入信息,与sed一样,信息的读入也是逐行读取的。不同的是,awk命令将文本文件中的一行视为一个记录,而将一行中的某一部分(列)作为记录的一个字段。为了操作这些不同的字段(列),awk借用shell中类似于位置变量的方法,用$1、$2…$9顺序的表示不同列,$0表示整行。不同字段与不同字段可以通过指定的方式进行分隔,awk默认的分隔符是空格。awk命令允许使用“-F分隔符”的形式来指定分隔符。

awk命令对/etc/passwd文件的处理过程如图:
Detailed Shell Programming Three Musketeers

awk包含几个特殊的内建变量,如:
Detailed Shell Programming Three Musketeers

2.awk命令用法示例
1)按行输出文本
[root@localhost ~]# awk '{print}' test.txt 
//输出所有内容,等同于“cat test.txt”
[root@localhost ~]# awk '{print $0}' test.txt
//输出所有内容,等同于“cat test.txt”
[root@localhost ~]# awk 'NR==1,NR==3{print}' test.txt 
//输出第1~3行的内容
[root@localhost ~]# awk '(NR>=1) && (NR<=3) {print}' test.txt 
//输出第1~3行的内容
[root@localhost ~]# awk 'NR==1 || NR==3{print}' test.txt 
//输出第1行、第3行的内容
[root@localhost ~]# awk '(NR%2)==1 {print}' test.txt 
//输出所有奇数行的内容
[root@localhost ~]# awk '(NR%2)==0 {print}' test.txt 
//输出所有偶数行的内容
[root@localhost ~]# awk '/^root/{print}' /etc/passwd
//输出以“root”开头的行
[root@localhost ~]# awk '/nologin$/{print}' /etc/passwd
//输出以“nologin”结尾的行
[root@localhost ~]# awk 'BEGIN {x=0} ;/\/bin\/bash$/{x++};END {print x}' /etc/passwd
//统计以/bin/bash结尾的行数
[root@localhost ~]# grep -c "/bin/bash$" /etc/passwd
//统计以/bin/bash结尾的行数
[root@localhost ~]# awk 'BEGIN{RS=""}; END{print NR}' /etc/squid/squid.conf
//统计以空格分隔的文件段落数

注意:命令较多时,使用“BEGIN……END”

2) Press the output text field
[root@localhost ~]# awk '{print $3}' test.txt 
//输出每行中(以空格分隔)的第3个字段
[root@localhost ~]# awk '{print $1,$3}' test.txt 
//输出每行中(以空格分隔)的第1个和第3个字段
[root@localhost ~]# awk -F ":" '$2==""{print}' /etc/shadow
//输出/etc/shadow文件中(以“:”分隔)的第二个字段(密码为空的用户)
[root@localhost ~]# awk 'BEGIN {FS=":"}; $2=""{print}' /etc/shadow
//输出/etc/shadow文件中(以“:”分隔)的第二个字段(密码为空的用户)
[root@localhost ~]# awk -F ":" '$7~"/bash"{print $1}' /etc/passwd
//输出以“:”分隔且第7个字段中包含“/bash”的行的第1个字段
[root@localhost ~]# awk '($1~"nfs") && (NF==8) {print $1,$2}' /etc/services
//输出包含8个字段且第1个字段中包含“nfs”的行的第1、2个字段
[root@localhost ~]# awk -F ":" '($7!="/bin/bash") && ($7!="/sbin/nologin") {print}' /etc/passwd
//输出第7个字段既不为“/bin/bash”也不为“/bin/nologin”的所有行
3) through a pipe, call the Shell command double quotes

[root @ localhost ~] # awk -F: '/ the bash $ / Print {| "wc -l"}' / etc / the passwd
// call the number of the user "wc -l" command usage statistics "bash" of
[root ~ @localhost] -C # grep "the bash $" / etc / the passwd
// supra same effect a command
[root @ localhost ~] # awk 'BEGIN {while ( "w" | getline) n ++; {print n-2 }} '
// call the "w" command and force ah statistics online users
[root @ localhost ~] # awk' BEGIN { "hostname" | getline; Print $ 0} '
// call the "hostname" command, and the output current username

4) Use the awk command simple math
[root@localhost ~]# awk 'BEGIN{ a=6;b=3;print"(a + b)=",(a + b)}'
(a + b)= 9
[root@localhost ~]# awk 'BEGIN{ a=6;b=3;print"(a - b)=",(a - b)}'
(a - b)= 3
[root@localhost ~]# awk 'BEGIN{ a=6;b=3;print"(a / b)=",(a / b)}'
(a / b)= 2
[root@localhost ~]# awk 'BEGIN{ a=6;b=3;print"(a % b)=",(a % b)}'
(a % b)= 0

More detailed awk command, you can refer Bowen: awk learning

Guess you like

Origin blog.51cto.com/14157628/2426185