需要将 file2 中与 file1 第一列匹配的字符串替换为 file1 第二列
所以,如果标题没有意义,这就是我想做的:
我有 file1:
66.115.135.84:123.123.123.1
66.115.135.85:123.123.123.2
66.115.135.86:123.123.123.3
66.115.135.87:123.123.123.4
66.115.135.88:123.123.123.5
66.115.135.89:123.123.123.6
66.115.135.90:123.123.123.7
66.115.135.91:123.123.123.8
66.115.135.92:123.123.123.9
66.115.135.93:123.123.123.10
66.115.135.94:123.123.123.11
66.115.135.95:123.123.123.12
66.115.135.96:123.123.123.13
66.115.135.97:123.123.123.14
如你所见,它是 ip 地址,用“:”分隔
File2 基本上是一个 apache 虚拟主机条目,或 httpd .conf 文件。这并不重要。只需知道 file2 包含 file1 第一列中某处的 IP 地址即可。并且需要将它们替换为 file1 的第二列。
出于某种原因,这个简单的问题让我感到困惑。我尝试过一些非常粗糙的事情,但总是陷入困境。
我知道我可以使用 awk 将它们分开,并且我知道我可以将其通过管道传输到 sed 中以对 file2 进行操作。
但我似乎无法理解将第 1 列“映射”到第 2 列的最佳方法,以便这实际上可以发生。
我愿意使用 perl、ruby、python,或者实际上任何实现此目的的方法,并且我非常希望简要说明如何解决此问题。
请要求任何澄清,我很乐意提供。
非常感谢!
So, if the title didn't make sense, here's what I'm trying to do:
I have file1:
66.115.135.84:123.123.123.1
66.115.135.85:123.123.123.2
66.115.135.86:123.123.123.3
66.115.135.87:123.123.123.4
66.115.135.88:123.123.123.5
66.115.135.89:123.123.123.6
66.115.135.90:123.123.123.7
66.115.135.91:123.123.123.8
66.115.135.92:123.123.123.9
66.115.135.93:123.123.123.10
66.115.135.94:123.123.123.11
66.115.135.95:123.123.123.12
66.115.135.96:123.123.123.13
66.115.135.97:123.123.123.14
As you can see, it's ip addresses, separated by a ":"
File2 is basically an apache virtual host entry, or httpd.conf file. It doesn't really matter. Just know that file2 contains the ip addresses from the first column of file1 somewhere in there. And they need to be replaced by the second column of file1.
For some reason, this simple problem has left me bewildered. I've tried some pretty gnarly things, but keep getting stuck.
I know I can separate them using awk, and i know i could pipe that into sed to act on file2.
But I can't seem to wrap my head around the best way to "map" column 1 to column 2 so that this can actually happen.
I'm willing to use perl, or ruby, or python, or really any method of achieving this, and I would very much like a brief explanation of how you are able to solve this.
Please ask for any clarification, and I'll be glad to supply it.
Thanks so much in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
将 file1 中的 IP 对读取到哈希中,例如
$ip{$old} = $new
。我假设没有重复的IP。通过 file2 查找 IP,并使用正则表达式,例如:编写类似以下内容的代码:
根据需要重定向到输出文件。 IP 可能需要更好的正则表达式。
Read the IP pairs from file1 into a hash, e.g.
$ip{$old} = $new
. I assume there are no duplicate IPs. Go through file2 looking for IPs, and use a regex such as:Code something like:
Redirect to output file as needed. Might need a better regex for the IP.
这会循环遍历文件 f1,然后循环遍历文件 f2。它将它们按照“:”字符分开,如果我们之前没有见过前半部分,请将其粘贴到散列中。如果我们之前看过前半部分,请打印我们存储在哈希中的值。
This loops over file f1 then file f2. It splits them on the ':' character and if we haven't seen the first half before, stick it in the hash. If we have seen the first half before, print the value we stored in the hash.
内部
sed
为外部 sed 创建一个多表达式正则表达式以应用于 file2..要安全地就地更新原始文件,您可以通过
sponge
将输出传输到 ir(来自包 moreutils)。The inner
sed
creates a multi-expression regex for the outer sed to apply to file2..To securely update your original file insitu, you can pipe the output to ir via
sponge
(from package moreutils).我使用perl。我们将其命名为
mapper.pl
。将映射文件作为参数,然后将stdin
映射到stdout
。所以你像这样使用它mapper.pl
程序是这样的:Id use perl. Lets call it
mapper.pl
. Takes map file as an arg, and then mapsstdin
tostdout
. So you use it like thisThe
mapper.pl
program is something like:感谢所有精彩的答案!
我受到他们的启发,创建了一个 ruby 版本:(它可以使用一些工作/减少,它还不是很 rubyesque,但它可以工作)
Thanks for all of the great answers!
I was inspired by them to create a ruby version: (it could use some work/reduction, it's not very rubyesque yet, but it works)