linux | sed 命令使用 | xargs命令 使用

92 篇文章 0 订阅
41 篇文章 0 订阅

#####################################################

sed命令来自英文词组stream editor的缩写,其功能是利用语法/脚本对文本文件进行批量的编辑操作。sed命令最初由贝尔实验室开发,后被众多Linux系统集成,能够通过正则表达式对文件进行批量编辑,让重复性的工作不再浪费时间。

参数  
-i 不在终端显示	直接修改
-n 仅显示处理后的结果

在这里插入图片描述

#########################
cat test1219.txt 
a b c d e f g
h i j k l m n
o p q
r s t
u v w x y z
tttttttttttttttttttttttt
zhangsan____
wangwu
zhangzhangzhang

把新的字符替换旧的字符  其中  "s/"new"/"old"/gi"  其中 i 表示忽略大小写
sed -i  "s/"tttttttttttttttttttttttt"/"zhangsan____"/gi"  ./test1219.txt


cat test1219.txt 
a b c d e f g
h i j k l m n
o p q
r s t
u v w x y z
zhangsan____
zhangsan____
wangwu
zhangzhangzhang

#####################################################
删除指定字符串
 cat test1219.txt 
a b c d e f g
h i j k l m n
o p q
r s t
u v w x y z
少壮不努力老大徒伤悲
少壮不努力老大徒伤悲
wangwu
zhangzhangzhang

sed -i  "/"少壮不努力老大徒伤悲"/d"  ./test1219.txt

cat test1219.txt 
a b c d e f g
h i j k l m n
o p q
r s t
u v w x y z
wangwu
zhangzhangzhang

#在第5行新增 数据  -e "xiyyyy" file 在文件file第x行 插入yyyy数据
# 下面这两个 都是等价的 重点是后者可以插入多行数据
sed -i -e '5ihello world this new line' ./test1219.txt

sed -i -e '5i\
hello world this new line' ./test1219.txt
# 这个就是插入 字符 "hello world this new line"
sed -i -e '5i"hello world this new line"' ./test1219.txt

#替换2-5行的数据内容
sed '2,5c NewSentence' test1219.txt
a b c d e f g
NewSentence
"hello world this new line"
hello world this new line
u v w x y z
wangwu
zhangzhangzhang

#读取1-2 两行
sed -n '1,2p' test1219.txt
a b c d e f g
h i j k l m n

#####################################################

xargs(英文全拼: eXtended ARGuments)是给命令传递参数的一个过滤器,也是组合多个命令的一个工具。

xargs 可以将管道或标准输入(stdin)数据转换成命令行参数,也能够从文件的输出中读取数据。

xargs 也可以将单行或多行文本输入转换为其他格式,例如多行变单行,单行变多行。

xargs 默认的命令是 echo,这意味着通过管道传递给 xargs 的输入将会包含换行和空白,不过通过 xargs 的处理,换行和空白将被空格取代。

###################
# 五个元素  一行输入
cat test1219.txt | xargs -n5
a b c d e
f g h i j
k l m n o
p q r s t
u v w x y
z

#############################

参考1

参考2

#######################################
写的有点乱 ,后面逐步完善, sed 命令 很重要 配上 正则 无敌!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值