如何使用找到sed和awk命令删除从文件中重复的IP [英] How to use find command with sed and awk to remove duplicate IP from files
问题描述
Howdie DO,
我在写一个脚本,将两个文件删除重复的IP的。例如,
的grep -RW123.234.567的/ home /测试/ IPS /
/home/test/ips/codingte:123.234.567
/home/test/ips/codingt2:123.234.567
好吧,这样IP是在两个不同的文件,所以我需要从第二个文件删除IP。
grep的给我的文件路径和IP地址。我的想法:将文件存储路径中使用awk一个变量,然后使用发现到那个文件,并使用sed来删除重复的IP,所以我改变了我的grep语句:
的grep -RW123.234.567。 | awk的-F:'{打印$ 1}
返回:
./codingte
./codingt2
我最初尝试使用完全路径在find命令,但没有工作,要么
找到-name在/ var /的cPanel /骤降/ codingte -exec SED/123.234.567/d'{} \\;
所以,我只是做了一个目录CD并改变find命令:
找到-name'codingt2'-exec SED/123.234.567/d'{} \\;
其中运行,但不会删除IP地址
猫codingt2
123.234.567
现在,我知道这个问题是与IP地址的点。他们需要逃过一劫,但我不知道如何做到这一点。我一直在阅读关于逃脱正则表达式小时,但我不知道如何使用sed的做到这一点。
任何帮助将是AP preciated。我只是想更多地了解正则表达式,并使用它们与其他Linux工具,如awk和发现。
我没有写完整的剧本呢。我试图把它分解成片,然后在脚本把它在一起。
所以,你知道的输出应该是什么样子:
codingte
123.234.567 codingt2
第二个文件将只拥有IP删除
猫FILE1.TXT |同时读取IP;做SED -i/ ^ $ {IP} $ / DFILE2.TXT; DONE
该命令执行以下操作:
- 有两个文件:
FILE1.TXT
和FILE2.TXT
- 将在
FILE2.TXT
删除线(在你的情况下,IP地址)在FILE1.TXT
发现
Howdie do,
I'm writing a script that will remove duplicate IP's from two files. For example,
grep -rw "123.234.567" /home/test/ips/
/home/test/ips/codingte:123.234.567
/home/test/ips/codingt2:123.234.567
Ok, so that IP is in two different files and so I need to remove the IP from the second file.
The grep gives me the file path and the IP address. My thinking: store the file path in a variable with awk and then use find to go to that file and use sed to remove the duplicate IP, so I changed my grep statement to:
grep -rw "123.234.567" . | awk -F ':' '{print $1}'
which returns:
./codingte
./codingt2
I originally tried to use the fully pathname in the find command, but that didn't work either
find -name /var/cpanel/dips/codingte -exec sed '/123.234.567/d' {} \;
So, I just did a CD in the directory and changed the find command to:
find -name 'codingt2' -exec sed '/123.234.567/d' {} \;
Which runs, but doesn't delete the IP address:
cat codingt2
123.234.567
Now, I know the issue is with the dots in the IP address. They need to be escaped, but I'm not sure how to do this. I've been reading for hours on escaping the regex, but I'm not sure how to do this with sed
Any help would be appreciated. I'm just trying to learn more about regex and using them with other linux tools such as awk and find.
I haven't written the full script yet. I'm trying to break it into pieces and then bring it together in the script.
So you know what the output should look like:
codingte
123.234.567
codingt2
The second file would just have the IP removed
cat FILE1.txt | while read IP ; do sed -i "/^${IP}$/d" FILE2.txt ; done
The command does the following:
- There are two files:
FILE1.txt
andFILE2.txt
- It will remove in
FILE2.txt
lines (in your case, IP addresses) found inFILE1.txt
这篇关于如何使用找到sed和awk命令删除从文件中重复的IP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!