linux-shell脚本攻略第四章笔记

让文本飞

grep

# -o 参数只输出被匹配的行
[root@localhost ~]# echo "this is a line." | egrep -o "[a-z]+\."
line.

[root@localhost bash]# cat 1.txt 
apple
bear
ear
eye
linux
art
# -n 输出时指定行号
[root@localhost bash]# cat 1.txt | grep -n "linux"
5:linux
# -b输出匹配字符偏移,从0开始计数
[root@localhost bash]# echo "linux is fun" | grep -b -o "is"
6:is
# -e 参数匹配多个
[root@localhost bash]# echo "this is a line" | grep -o -e "this" -e "line"
this
line
# 使用--exclude排除所有README文件,--include类似
grep "main()" . -r --exclude "README"
grep "main()" . -r --include *.{c,cpp}
# 使用--exclude-dir排除目录
grep "main()" . -r --exclude-dir CVS
# 使用--exclude-from FILE排除文件列表

# -Z 使用0值字节作为文件名的终结符
# -l 只输出有匹配出现的文件名
# -q 静默输出

# -A 打印匹配结果之后的行  -B 匹配结果之前的行
[root@localhost bash]# cat test.txt | grep food -B 3
good
god
ggggoooooddddd
food
[root@localhost bash]# cat test.txt | grep food -A 2
food
abc
ABCD

cut

# -f 指定要提取的字段,多列使用,分隔  -d 设置分隔符
[root@localhost bash]# nmcli connection | cut -d " " -f 1
NAME
System
virbr0
# --complement显示没有被-f指定的字段,类似grep的-v

# -b字节 -c字符 -f定义字段
[root@localhost bash]# nmcli connection | cut -c -2 
NA
Sy
vi
[root@localhost bash]# nmcli connection | cut -c 1-3
NAM
Sys
vir
[root@localhost bash]# nmcli connection | cut -c 50-
   TYPE            DEVICE 
d  802-3-ethernet  ens33  
1  bridge          virbr0

sed

# & 指代模式所匹配到的字符串,\w\+匹配每一个单词
[root@localhost bash]# echo "hello,world" | sed 's/\w\+/++&--/g'
++hello--,++world--
# 匹配多位数字
[root@localhost bash]# echo "XXX 123"| sed '/XXX/s/\([0-9]\+\)/100/'
XXX 100
# 子串匹配标记(\1)
[root@localhost bash]# echo "SIX seven" | sed 's/\([A-Z]\+\) \([a-z]\+\)/\2 \1/'
seven SIX

awk

(1) 首先执行BEGIN { commands } 语句块中的语句,可放变量初始化、打印输出表格的表头等语句

(2) 接着从文件或stdin中读取一行,如果能够匹配pattern,则执行随后的commands语句 块。重复这个过程,直到文件全部被读取完毕;不提供默认为print,对读取到的每一行都执行该语句块。像循环体,遍历输入的每行

(3) 当读至输入流末尾时,执行END { commands } 语句块,可打印所有分析结果

[root@localhost bash]# echo -e "1 a\n2 b\n3 c" | awk 'BEGIN{ print"=======" } {print "Line no:"NR,"NO of fields:"NF,"$0="$0,"$1="$1,"$2="$2"..."} END{ print"========" }'
=======
Line no:1 NO of fields:2 $0=1 a $1=1 $2=a...
Line no:2 NO of fields:2 $0=2 b $1=2 $2=b...
Line no:3 NO of fields:2 $0=3 c $1=3 $2=c...
========

# -v 传递外部变量
[root@localhost bash]# echo | awk -v histsize=$HISTSIZE '{ print histsize }'
1000
[root@localhost bash]# var1="v1";var2="v2"
[root@localhost bash]# echo | awk '{print var1,var2}' var1=$var1 var2=$var2
v1 v2

# getline函数读取一行

# awk的for循环
[root@localhost bash]# cat /etc/passwd | awk -F ":" 'BEGIN {print "----"} {name[$1]=$5} END {for (i in name) {print i,name[i]}}'
----
adm adm
rpc Rpcbind Daemon
radvd radvd user
……

awk有很多内建的字符串处理函数。
length(string):返回字符串string的长度。
index(string, search_string):返回search_string在字符串string中出现的位置。
split(string, array, delimiter):以delimiter作为分隔符,分割字符串string, 将生成的字符串存入数组array。
substr(string, start-position, end-position):返回字符串string中以 start-position和end-position作为起止位置的子串。
sub(regex, replacement_str, string):将正则表达式regex匹配到的第一处内容 替换成replacment_str。
gsub(regex, replacement_str, string):和sub()类似。不过该函数会替换正则 表达式regex匹配到的所有内容。
match(regex, string):检查正则表达式regex是否能够在字符串string中找到匹配。 如果能够找到,返回非0值;否则,返回0。match()有两个相关的特殊变量,分别是RSTART 和RLENGTH。变量RSTART包含了匹配内容的起始位置,而变量RLENGTH包含了匹配内容 的长度

paste

按列合并多个文件

# 默认分隔符为tab,-d指定分隔符
[root@localhost bash]# cat 1
1
2
3
[root@localhost bash]# cat 2
linux
centos7
[root@localhost bash]# paste 1 2 
1	linux
2	centos7
3	

对目录中的所有文件进行文本替换

[root@localhost ~]# find  . -name *.sh -exec sed -i 's/oldstring/newstring/g' \{\} \+

文本切片与参数操作

[root@localhost bash]# var="This is a text"
[root@localhost bash]# echo ${var/text/TEXT}
This is a TEXT
[root@localhost bash]# echo ${var:3}
s is a text
[root@localhost bash]# echo ${var:3:6}
s is a
[root@localhost bash]# echo ${var:4:-2}
is a te
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值