Grep一个文件,并在第二个文件上使用sed -i删除grep的内容 [英] Grep one file and remove contents of the grep with sed -i on a second file

查看:329
本文介绍了Grep一个文件,并在第二个文件上使用sed -i删除grep的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用一个命令从一个文件grep ip地址,并从另一个文件中删除grep的内容.

I am trying to grep ip addresses from one file and remove the contents of the grep from a second file with a single command.

主文件具有

header x.x.x.x
header x.x.x.x
header2 y.y.y.y
header2 y.y.y.y
header3 z.z.z.z
header3 z.z.z.z

临时文件具有

x.x.x.x
x.x.x.x
y.y.y.y
y.y.y.y
z.z.z.z
z.z.z.z

尝试

grep "header" masterfile | sed 's/^.* //' | sed -i "" '/$/d' tempfile

在sed命令删除条目之后,也希望删除空行.

Would also like to remove the empty line after the sed command removes an entry.

推荐答案

如果我理解正确,则需要以下内容:

If I understand it correct, you need the following:

grep -v -F -f <(grep '\<header\>' masterfile | cut -d' ' -f2) tempfile

grep -v -F -f <(sed 's/^header *//' masterfile) tempfile

您的输入将产生:

y.y.y.y
y.y.y.y
z.z.z.z
z.z.z.z

为了将更改保存到tempfile,您可以将命令输出重定向到另一个文件并将其移动到所需文件:

In order to save the changes to tempfile, you could redirect the command output to another file and move it to the desired file:

grep -v -F -f <(sed 's/^header *//' masterfile) tempfile > tmp && mv tmp tempfile


您似乎是sh,它不支持流程替换".在这种情况下,您可以使用以下代码:


You seem to be sh which doesn't support Process Substitution. In that case, you can use the following:

grep '\<header\>' master | cut -d' ' -f2 | grep -v -F -f - tempfile

这篇关于Grep一个文件,并在第二个文件上使用sed -i删除grep的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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