grep + grep + sed = sed:无输入文件 [英] grep + grep + sed = sed: no input files

查看:668
本文介绍了grep + grep + sed = sed:无输入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮我吗?

grep " 287 " file.txt | grep "HI" | sed -i 's/HIS/HID/g' 

sed: no input files

也尝试过xargs

grep " 287 " file.txt | grep HI | xargs sed -i 's/HIS/HID/g'
sed: invalid option -- '6'

这很好

grep " 287 " file.txt | grep HI

推荐答案

如果要保留管道:

f=file.txt
tmp=$(mktemp)
grep " 287 " "$f" | grep "HI" | sed 's/HIS/HID/g' > "$tmp" && mv "$tmp" "$f"

或者,简化:

sed -i -n '/ 287 / {/HI/ s/HIS/HID/p}' file.txt

这将过滤掉任何不包含"287"和"HI"的行-这就是您想要的吗?我怀疑您真的想要这个:

That will filter out any line that does not contain " 287 " and "HI" -- is that what you want? I suspect you really want this:

sed -i '/ 287 / {/HI/ s/HIS/HID/}' file.txt

对于与/ 287 /匹配的行,请用大括号执行命令.在其中,对于与/HI/匹配的行,搜索第一个"HIS"并替换为"HID".如果未指定-n,则sed隐式打印所有行.

For lines that match / 287 /, execute the commands in braces. In there, for lines that match /HI/, search for the first "HIS" and replace with "HID". sed implicitly prints all lines if -n is not specified.

其他执行相同操作的命令:

Other commands that do the same thing:

awk '/ 287 / && /HI/ {sub(/HIS/, "HID")} {print}' file.txt > new.txt
perl -i -pe '/ 287 / and /HI/ and s/HIS/HID/' file.txt

awk没有就地"选项(对于最新的gawk版本,gawk -i inplace除外)

awk does not have an "in-place" option (except gawk -i inplace for recent gawk versions)

这篇关于grep + grep + sed = sed:无输入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆