@文本处理之三剑客的简单使用


三剑客的简单使用


1、grep命令&正则表达式---》过滤

2、sed—》非交互式编辑文本

3、awk-》格式化有规律的文本

三剑客命令的共性:


​	1、都支持正则表达式

​	2、都支持管道
echo 123 | passwd egon --stdin
4、expect:交互的命令变为非交互

一 grep命令

grep的执行原理

========用法1
grep "root" /etc/passwd

# 打开文件,每读一行,都用正则表达式去匹配一下,但凡匹配成功一次,该行就被过滤出来的
root:x:0:0:root:/root:/bin/bash
                     root
                     
========用法2
cat test.txt | grep "root"
ps aux | grep ssh

grep的选项

-n 过滤结果带行号
-q 静默输出
--color
-i 忽略大小写
-A 2 后两行
-B 2 前两行
-C 2 前后两行
-c 统计过滤成功的行数
-v 取反
-w
grep -rl "location" /etc/

正则表达式

案例1

^
$

grep -n "^root" /etc/passwd
grep -n "bash$" /etc/passwd

案例2

.    匹配任意一个字符
*    左边那个字符出现0次或无穷次
.*   匹配没有或者所有   
.*?  非贪婪匹配(需要配合grep -P选项才可以使用)


# 例1
[root@www1 ~]# cat c.txt 
abc
a+c
a-c
a1c
aaaaaaaaaaaaaaaac
bbbbbbbbc
cccc
dddd
[root@www1 ~]# grep "a.c" c.txt 
abc
a+c
a-c
a1c
aaaaaaaaaaaaaaaac

# 例2
[root@www1 ~]# cat d.txt 
a
ab
abbcccc
abbbbbbbbbbbbbbbbccc
bbbbbbbbbbbbb
aaaaaaaaaaaaaaaab
[root@www1 ~]# 
[root@www1 ~]# grep "ab*" d.txt 
a
ab
abbcccc
abbbbbbbbbbbbbbbbccc
aaaaaaaaaaaaaaaab

# 例3:
[root@www1 ~]# cat e.txt 
a123123213c3123123c
ac
a=-*(0c
cccccca123123c
[root@www1 ~]# grep "a.*c" e.txt 
a123123213c3123123c
ac
a=-*(0c
cccccca123123c
[root@www1 ~]# 

# 例4:
[root@www1 ~]# cat f.txt 
<a href="http://www.baidu.com">我是百度</a><a href="http://www.sina.com.cn">新浪</a>
1111
2222
3333
[root@www1 ~]# grep '".*"' f.txt 
<a href="http://www.baidu.com">我是百度</a><a href="http://www.sina.com.cn">新浪</a>
[root@www1 ~]# grep -P '".*?"' f.txt 
<a href="http://www.baidu.com">我是百度</a><a href="http://www.sina.com.cn">新浪</a>
[root@www1 ~]# grep -oP '".*?"' f.txt 
"http://www.baidu.com"
"http://www.sina.com.cn"
[root@www1 ~]# 

案例3

[] 匹配指定范围的任意一个字符

[a1cd2]
[0-9]
[a-z]

# 例1
[root@www1 ~]# cat h.txt 
abc
aBc
a1c
a2c
[root@www1 ~]# grep 'a[a-z]c' h.txt 
abc
[root@www1 ~]# grep 'a[A-Z]c' h.txt 
aBc
[root@www1 ~]# grep 'a[a-zA-Z]c' h.txt 
abc
aBc
[root@www1 ~]# grep 'a[0-9]c' h.txt 
a1c
a2c
[root@www1 ~]# 


# 例2
[root@www1 ~]# cat i.txt 
a+c
a-c
a*c
a/c
a1c
a2c
[root@www1 ~]# grep 'a[+-*/]c' i.txt 
grep: Invalid range end
[root@www1 ~]# grep 'a[+\-*/]c' i.txt 
grep: Invalid range end
[root@www1 ~]# grep 'a[+*/-]c' i.txt 
a+c
a-c
a*c
a/c
[root@www1 ~]# 

# 例3:
[root@www1 ~]# cat i.txt 
a+c
a-c
a*c
a/c
a1c
a2c
a!c
[root@www1 ~]# grep 'a[!+*/-]c' i.txt 
a+c
a-c
a*c
a/c
a!c
[root@www1 ~]# grep 'a[^!+*/-]c' i.txt 
a1c
a2c
[root@www1 ~]# 
[root@www1 ~]# 

案例4

+ 左边那个字符出现1次或无穷次

ab*
ab+

# 例1
[root@www1 ~]# cat d.txt 
a
ab
abbcccc
abbbbbbbbbbbbbbbbccc
bbbbbbbbbbbbb
aaaaaaaaaaaaaaaab
[root@www1 ~]# grep -n 'ab*' d.txt 
1:a
2:ab
3:abbcccc
4:abbbbbbbbbbbbbbbbccc
6:aaaaaaaaaaaaaaaab
[root@www1 ~]# egrep -n 'ab+' d.txt 
2:ab
3:abbcccc
4:abbbbbbbbbbbbbbbbccc
6:aaaaaaaaaaaaaaaab
[root@www1 ~]# 

# 例2
[root@www1 scripts]# cat 14.sh 
#!/bin/bash

while true
do
    read -p "请输入您的年龄: " age
    
    if [[ $age =~ ^[0-9]+$ ]];then
        break
    else
        echo "必须输入数字小垃圾"       
    fi
done


echo "后续代码"
[root@www1 scripts]# chmod +x 14.sh 
[root@www1 scripts]# ./14.sh 
请输入您的年龄: asdf
必须输入数字小垃圾
请输入您的年龄: asdf
必须输入数字小垃圾
请输入您的年龄: 123
后续代码

二 sed命令

# 场景1:
定位到某一行,然后将该的某一部分给替换掉
sed -r "定位+操作" test.txt

sed -r "3s/egon/EGON/gi" test.txt
sed -r "3,5操作" test.txt
sed -r "1操作;3操作" test.txt


sed -r "/^[a-zA-Z]/s/egon/EGON/gi" test.txt


# 场景2:
定位到某一行,然后删除

sed -r "1,3d" new.txt
sed -r "/^[0-9]/d" new.txt

# 场景3:
定位到某一行,在改行后添加新的配置
sed -r "1a XXXXXXXXXXXXXXXX" new.txt

# 场景4:
定位到某一行,将整行修改掉
sed -r "1c XXXXXXXXXXXXXXXX" new.txt

场景1案例

[root@www1 ~]# cat new.txt 
xxEgonxxxegonxxxxx
xxx123xxegonxxegonxxx
EgonxxxxdaxegonxxEGONxxx
xxxyyyyxxegonxxxEgOnxx
xxxzzzxxegonxxxxx
xxxhhhhhxxegonxxxxx
[root@www1 ~]# sed -r '3s/egon/666666/' new.txt 
xxEgonxxxegonxxxxx
xxx123xxegonxxegonxxx
Egonxxxxdax666666xxEGONxxx
xxxyyyyxxegonxxxEgOnxx
xxxzzzxxegonxxxxx
xxxhhhhhxxegonxxxxx
[root@www1 ~]# sed -r '3s/egon/666666/i' new.txt 
xxEgonxxxegonxxxxx
xxx123xxegonxxegonxxx
666666xxxxdaxegonxxEGONxxx
xxxyyyyxxegonxxxEgOnxx
xxxzzzxxegonxxxxx
xxxhhhhhxxegonxxxxx
[root@www1 ~]# sed -r '3s/egon/666666/gi' new.txt 
xxEgonxxxegonxxxxx
xxx123xxegonxxegonxxx
666666xxxxdax666666xx666666xxx
xxxyyyyxxegonxxxEgOnxx
xxxzzzxxegonxxxxx
xxxhhhhhxxegonxxxxx
[root@www1 ~]# sed -ri '3s/egon/666666/gi' new.txt 
[root@www1 ~]# vim new.txt 

场景1案例

[root@www1 ~]# cat new.txt 
xxEgonxxxegonxxxxx
xxx123xxegonxxegonxxx
EgonxxxxdaxegonxxEgOnxxx
xxxyyyyxxegonxxxEgOnxx
xxxzzzxxegonxxxxx
xxxhhhhhxxegonxxxxx
[root@www1 ~]# sed -r '3s/^egon/666/gi' new.txt 
xxEgonxxxegonxxxxx
xxx123xxegonxxegonxxx
666xxxxdaxegonxxEgOnxxx
xxxyyyyxxegonxxxEgOnxx
xxxzzzxxegonxxxxx
xxxhhhhhxxegonxxxxx
[root@www1 ~]# 

场景1案例

[root@www1 ~]# cat new.txt 
xxEgonxxxegonxxxxx
xxx123xxegonxxegonxxx
EgonxxxxdaxegonxxEgOnxxx
xxxyyyyxxegonxxxEgOnxx
xxxzzzxxegonxxxxx
xxxhhhhhxxegonxxxxx
[root@www1 ~]# sed -r '3,5s/egon/666/gi' new.txt 
xxEgonxxxegonxxxxx
xxx123xxegonxxegonxxx
666xxxxdax666xx666xxx
xxxyyyyxx666xxx666xx
xxxzzzxx666xxxxx
xxxhhhhhxxegonxxxxx
[root@www1 ~]# sed -r '3s/egon/666/gi;5s/egon/666/gi' new.txt 
xxEgonxxxegonxxxxx
xxx123xxegonxxegonxxx
666xxxxdax666xx666xxx
xxxyyyyxxegonxxxEgOnxx
xxxzzzxx666xxxxx
xxxhhhhhxxegonxxxxx

场景1案例

sed对指定行添加或删除注释  
 11.txt

aaaaa

#bbbbbb

cccccc

dddddd
用sed在aaa前加#号注释 
sed 's/^/#/' 11.txt    #给所有行前面加入#号


 
用sed在aaa前加#注释 

sed 's/^aaa/#&/' 11.txt      # &的意思是匹配任意字符(就是说未知数,啥都行)  替换以aaa开头的行



用sed取消bbb前面的注释

sed 's/^#\(bbb\)/\1/' 11.txt    #\1的意思 就类似于   前面的 (bbb\) \1就是复制这个位置的内容  如果有 第二个 那么久\2就是复制第二个位置的内容



两条命令等同于

sed 's/^#bbb/bbb/' 11.txt




三 awk命令

awk -F: '定位{}' 文件路径

awk -F: 'NR==3{print $1,$7}' /etc/passwd
awk -F: 'NR>3 && NR<5{print $0}' /etc/passwd
awk -F: 'NR==3 || NR==5{print $1"-"$3}' /etc/passwd

awk -F: '/bash$/{print $1}' /etc/passwd

获取ip地址

[root@www1 ~]# ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 1.1.1.8  netmask 255.255.255.0  broadcast 1.1.1.255
        inet6 fe80::20c:29ff:fea2:d75e  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:a2:d7:5e  txqueuelen 1000  (Ethernet)
        RX packets 51153  bytes 4323126 (4.1 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 59883  bytes 64679002 (61.6 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@www1 ~]# ifconfig eth0 | awk 'NR==2{print $1}'
inet
[root@www1 ~]# ifconfig eth0 | awk 'NR==2{print $2}'
1.1.1.8
[root@www1 ~]# ip=`ifconfig eth0 | awk 'NR==2{print $2}'`
[root@www1 ~]# echo $ip
1.1.1.8
[root@www1 ~]# 

四 expect

yum install expect -y
    expect << EOF
		spawn ssh $user@$ip $cmd

		expect {
		    "yes/no" {send "yes\r";exp_continue}
		    "*assword" {send "1\n"}
		}

		expect eof
	EOF

cat > a1.txt << EOF

111

222

333

EOF

[root@www1 scripts]# cat 15.sh 
#!/bin/bash

expect << EOF
    spawn ssh root@1.1.1.2 hostname

    expect {
	    "yes/no" {send "yes\r";exp_continue}
	    "*assword" {send "1\n"}
    }

    expect eof

EOF
[root@www1 scripts]# chmod +x 15.sh 
posted @ 2021-04-13 15:39  ଲ一笑奈&何  阅读(89)  评论(0编辑  收藏  举报