首页 > 其他 > 详细

sed常用方法

时间:2018-08-23 20:07:44      阅读:235      评论:0      收藏:0      [点我收藏+]
1、将空格替换成换行
sed "s/ /\n/g" yum.old.txt

2 将换行替换成空格

cat yum.new3.txt |tr "\n" " "

2、将每一行最后的空格+\替换掉

sed "s/ [\]$//g" yum.new.txt

3、在每一行最后添加 \

sed "s/$/ \\\/g" yum.new3.txt

4、将文本中每行字符串后面的空格去掉

#原文件
[root@docker soft]# cat -A test.txt 
huilong$
uc     $
sso    $
index  $
hq     $
static $
news   $
broker $
edu    $
px     $
bbs    $
www    $
salon  $
gold   $
cms    $
m.salon$

#匹配规则 空格加*表示匹配任意多个空格
[root@docker soft]# sed "s/ *$//g" test.txt > test2.txt

#最终结果
[root@docker soft]# cat -A test2.txt 
huilong$
uc$
sso$
index$
hq$
static$
news$
broker$
edu$
px$
bbs$
www$
salon$
gold$
cms$
m.salon$

5、将文件中每行字符后面拼接一段相同的字符串。

#原文件
[root@docker soft]# cat -A test2.txt 
huilong$
uc$
sso$
index$
hq$
static$
news$
broker$
edu$
px$
bbs$
www$
salon$
gold$
cms$
m.salon$

#例如在每行字符串结尾添加一个域名后缀.forex.com.cn
#匹配规则 测试ok
[root@docker soft]# sed "s/$/.forex.com.cn/g" test2.txt
huilong.forex.com.cn
uc.forex.com.cn
sso.forex.com.cn
index.forex.com.cn
hq.forex.com.cn
static.forex.com.cn
news.forex.com.cn
broker.forex.com.cn
edu.forex.com.cn
px.forex.com.cn
bbs.forex.com.cn
www.forex.com.cn
salon.forex.com.cn
gold.forex.com.cn
cms.forex.com.cn
m.salon.forex.com.cn

sed常用方法

原文:http://blog.51cto.com/haowenliu/2163554

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!