sed的基本命令

(2)grep

cat file | grep '^so'
southwestSWLewisDalsass2.7.8218
southernSOSuanChin5.1.95415

cat file | grep '4$'
northwestNWCharlesMain3.0.98334

cat file | grep '^$'

cat -E file 
northwestNWCharlesMain3.0.98334$
$
westernWESharonGray5.3.97523$
$
southwestSWLewisDalsass2.7.8218$

cat file | grep -E 's+'
northwestNWCharlesMain3.0.98334
westernWESharonGray5.3.97523
southwestSWLewisDalsass2.7.8218

cat file | grep -E '[ ]|^$'

cat file | grep -E 's{2}'
southwestSWLewisDalsass2.7.8218

cat file | grep '^\$'  # 转义\$
$centralCTAnnStephens5.7.94513

cat file | grep '[$]' #[]与\等同
$centralCTAnnStephens5.7.94513

cat file | grep -nE '^$|[ \t]+'  #过滤所有的空行和空格



^以什么开头
$以什么结尾
^$ 空行
. 匹配所有(不包括空行)
.* 匹配所有 *-->0次或者多次  .-->任意一个字符
+ 一个或者多个字符
[] 放一些特殊字符 如: \n \t 空格
s{2} 取连续两个s

注意:空行(\n)跟空格不一样

(3)grep

 cat file | grep '[a-z0-9A-Z]'  # [] 连字符--> 范围
northwestNWCharlesMain3.0.98334
westernWESharonGray5.3.97523
southwestSWLewisDalsass2.7.8218
southernSOSuanChin5.1.95415
过滤所有

(4)替换字符

[root@controller sed]# echo qwe123abc | sed 's/123/456/g'
qwe456abc
[root@controller sed]# echo qwe123abc | sed 's#123#456#g'
qwe456abc
[root@controller sed]# echo qwe123abc | sed 's@123@456@g'
qwe456abc
[root@controller sed]# echo qwe123abc | sed 's;123;456;g'
qwe456abc

g -->替换多个
无g替换一个
s 后面的字符作为分隔符

(5)sed

cat file | sed -n  '/centr/p'
centralCTAnnStephens5.7.94513

cat file | sed -n  '/centr/s/5.7/6.0/gp'
centralCTAnnStephens6.0.94513

(6)删除字符

echo 'qwe123abc' | sed 's/[0-9][0-9][0-9]//'
qweabc

echo 'qwe123abc' | sed 's/[0-9]\{3\}//'
qweabc

echo 'qwe123abc' | sed -r 's/[0-9]{3}//'
qweabc

echo 'qwe123abc' | sed -r 's/[0-9]+//'
qweabc

cat file | sed '1d'

cat file | sed -r  '/^$|[ \t]+/d'
# 删除空格空行
cat file | sed -n '/=/!p'
# !取反

cat file | sed '/^sou/,/^no/d'

# 范围


-r --> 启用扩展正则

/可用正则表达/不能用正则表达(直接替换成正则)/
echo qwe123qwe | sed -e 's/qwe/456/' -e 's/123/qew/'
456qewqwe

-e
sed -n 's/=/+/gp' file 
clonme+asda		

sed -i 's/=/+/g' file

sed -i 's/\+/=/g' file  # 修改回来需转义

-n 取消默认输出
p 仅输出改动过的文本
-i 直接修改源文件

(7)查看内容


[root@controller sed]# cat -n file | sed -n '1,5p'
     1	northwestNWCharlesMain3.0.98334
     2	
     3	westernWESharonGray5.3.97523
     4	
     5	southwestSWLewisDalsass2.7.8218

[root@controller sed]# cat -n file | sed -n '5,$p'
# 第五行到最后一行
[root@controller sed]# cat -n file | sed -n '1p:2p'
# 第一行与第二行
[root@controller sed]# cat -n file | sed  '18,$s/=/+/g'
#18 行的=替换成+

[root@controller sed]# cat file | sed -n '1p;5p;5q'
northwestNWCharlesMain3.0.98334
southeastSEPatriciaHemenway4.0.7417
# q 处理到对应的行即退出

(8) aci

cat file | sed '1i#!/bin/bash'
#!/bin/bash

cat file | sed '1a#!/bin/bash'
northwestNWCharlesMain3.0.98334
#!/bin/bash

cat file | sed '1c#!/bin/bash'
#!/bin/bash

cat file | sed '$i#!/bin/bash'
northwestNWCharlesMain3.0.98334
westernWESharonGray5.3.97523
southwestSWLewisDalsass2.7.8218
southernSOSuanChin5.1.95415
southeastSEPatriciaHemenway4.0.7417
easternEATBSavage4.4.84520
northeastNEAMMainJr.5.1.94313
northNOMargotWeber4.5.8959
#!/bin/bash
$centralCTAnnStephens5.7.94513


cat file | sed '$a#!/bin/bash'
northwestNWCharlesMain3.0.98334
westernWESharonGray5.3.97523
southwestSWLewisDalsass2.7.8218
southernSOSuanChin5.1.95415
southeastSEPatriciaHemenway4.0.7417
easternEATBSavage4.4.84520
northeastNEAMMainJr.5.1.94313
northNOMargotWeber4.5.8959
$centralCTAnnStephens5.7.94513
#!/bin/bash


i 在第一行前插入
a 在第一行后插入
c 修改第一行

(9)去c语言注释

cat a.c
#include <stdio.h>
/*
	made by hhh
	2022 2 31
*/

int main() {
		
		print("hellow world\n"); /*------------*/
             }
/*

	asd
	asdf
*/
cat a.c | sed 's/\/\/.*$//g'
#include <stdio.h>
/*
	made by hhh
	2022 2 31
*/
int main() {
		
		print("hellow world\n"); /*------------*/
             }
/*

	asd
	asdf
*/
cat a.c | sed 's/\/\/.*$//g' | sed 's/\/\*.*\*\///g'
#include <stdio.h>
/*
	made by hhh
	2022 2 31
*/



int main() {
		
		print("hellow world\n"); 
             }

/*

	asd
	asdf


*/
cat a.c | sed 's/\/\/.*$//g' | sed 's/\/\*.*\*\///g' | sed '/^\/\*/,/^\*\//d' | grep -v '^$'
#include <stdio.h>
int main() {
		
		print("hellow world\n"); 
             }
cat a.c | sed 's/\/\/.*$//g' | sed 's/\/\*.*\*\///g' | sed '/^\/\*/,/^\*\//d' | grep -v '^$' > file2

cat -E file2 
#include <stdio.h>$
int main() {$
		$
		print("hellow world\n"); $
             }$
 $

(10)引用外部变量

tail -1 1.sh | bash
# 执行脚本命令
 export ip=192.168.20.10

cat file| sed "s/111/$ip/g"
northwestNWCharlesMain3.0.98334
westernWESharonGray5.3.97523
southwestSWLewisDalsass2.7.8218
southernSOSuanChin5.1.95415
southeastSEPatriciaHemenway4.0.7417
easternEATBSavage4.4.84520
northeastNEAMMainJr.5.1.94313
northNOMargotWeber4.5.8959
$centralCTAnnStephens5.7.94513
$ip=192.168.20.10


cat file| sed 's/111/'$ip'/g'
northwestNWCharlesMain3.0.98334
westernWESharonGray5.3.97523
southwestSWLewisDalsass2.7.8218
southernSOSuanChin5.1.95415
southeastSEPatriciaHemenway4.0.7417
easternEATBSavage4.4.84520
northeastNEAMMainJr.5.1.94313
northNOMargotWeber4.5.8959
$centralCTAnnStephens5.7.94513
$ip=192.168.20.10

cat file| sed 's/111/$ip/g'
northwestNWCharlesMain3.0.98334
westernWESharonGray5.3.97523
southwestSWLewisDalsass2.7.8218
southernSOSuanChin5.1.95415
southeastSEPatriciaHemenway4.0.7417
easternEATBSavage4.4.84520
northeastNEAMMainJr.5.1.94313
northNOMargotWeber4.5.8959
$centralCTAnnStephens5.7.94513
$ip=$ip

# 单引号不被解析
# 双引号被解析

(11)后向引用

cat file |sed -n '$=' 
10
# =(行号) $最后一行

cat file | wc -l
10

cat file
northwestNWCharlesMain3.0.98334
westernWESharonGray5.3.97523
southwestSWLewisDalsass2.7.8218
southernSOSuanChin5.1.95415
southeastSEPatriciaHemenway4.0.7417
easternEATBSavage4.4.84520
northeastNEAMMainJr.5.1.94313
northNOMargotWeber4.5.8959
$centralCTAnnStephens5.7.94513
$ip=111
ifconfig eth0 | sed -n '/inet /p'
        inet 192.168.0.133  netmask 255.255.255.0  broadcast 192.168.0.255



ifconfig eth0 | sed -n '/inet /p' | sed 's/^.* //g'
192.168.0.255


ifconfig eth0 | sed  '/inet /!d'
        inet 192.168.0.133  netmask 255.255.255.0  broadcast 192.168.0.255
        
ifconfig eth0 | sed  '/inet /!d' | sed 's/^.* //g'
192.168.0.255
ifconfig eth0 | sed  -n 's/^.*inet \(.*\)  netmask.*$/\1/pg'
192.168.0.133

ifconfig eth0 | sed  -rn 's/^.*inet (.*)  netmask.*$/\1/pg'
192.168.0.133

# (.*) 分组
cat file3 
123qwe456
789add123

cat file3 | sed -r 's/(.*)([a-z]{3})(.*)/\3\t\2\t\1/g'
456	qwe	123
123	add	789

# 替换成大写
cat file3 | sed -r 's/(.*)([a-z]{3})(.*)/\3\t\2\t\1/g' | tr 'a-z' 'A-Z'
456	QWE	123
123	ADD	789

cat file3 | sed -r 's/(.*)([a-z]{3})(.*)/\3\U\2\1/g'
456QWE123
123ADD789


# U ---> 大写

cat file3 | sed -r 's/(.*)([a-z]{3})(.*)/\3\u\2\1/g'
456Qwe123
123Add789
# u ---> 首字母大写

cat file3 | sed -r 's/(.*)([a-z]{3})(.*)/\3\L\2\1/g'
456qwe123
123add789
# L 小写


cat file3 | sed -r 's/(.*)([a-z]{3})(.*)/\3\l\2\1/g' |sed 's/.*/\n&/g'

456qwe123

123add789

# & 地址符

echo '123132131' | sed -r 's/[0-9]{3}/&- -/'
123- -132131


echo '123132131' | sed -r 's/./&\n/g'
1
2
3
1
3
2
1
3
1


(12)批量修改文件名

cat chang.sh 
#!/bin/absh
for i in `ls *.html`
do
		cp  $i `echo $i | sed 's/html/jpg/g'`
done
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值