
1 wwwjfy 2014 年 3 月 7 日 grep ... ip.txt | sponge ip.txt |
2 jakwings 2014 年 3 月 7 日 上面 wwwjfy 才是对的……不要胡乱猜测工具在输出内容前会不会为你缓存整个文本的内容,因为工具为了资源消耗的考虑可能不会如你所愿。 |
3 webjin OP |
5 7rack 2014 年 3 月 7 日 你的代码乍一看没错,其实是你在同一管道中读写同一文件,这里是问题。所以你可以改为 grep -v "$ip" ip.txt > iptmp.txt mv iptmp.txt ip.txt 试试 |
6 Mutoo 2014 年 3 月 7 日 sort 完 diff 不就行了? |
7 duzhe0 2014 年 3 月 7 日 sort 1.txt > using.txt sort ip.txt > all.txt comm -13 1.txt ip.txt > unused.txt |
8 duzhe0 2014 年 3 月 7 日 错了,最后一行应该是 comm -13 using.txt all.txt > unused.txt |
9 kfll 2014 年 3 月 7 日 grep -E -v "($(echo -n $(< 1.txt) | tr '[[:blank:]]*' '|'))" ip.txt |
10 webjin OP @7rack 谢谢了,同一管道中读写同一文件 。是我这个问题困扰我很久,为什么想不通,他grep输出来的内容然后再重定向到他本身文件会清空,如果是>>他会添加但是> 他直接清空。 |
15 winsweet 2014 年 3 月 7 日 cat 1.txt ip.txt ip.txt | sort | uniq -u |
16 webjin OP 其实这种方法也可以吧 #!/usr/bin/env bash for ip in $(cat 1.txt) do grep -v "$ip" ip.txt | tee ip.txt done |
17 webjin OP 我要纠正下我刚才实验了一下 grep -v "$ip" ip.txt | tee ip.txt 这个方法不行,因为我对比了 1.txt和ip.txt文件 最后输出的结果不一样,他多处理很多IP 。结果不正确。 |
19 amyangfei 2014 年 3 月 7 日 http://stackoverflow.com/questions/4780203/deleting-lines-from-one-file-which-are-in-another-file 这个链接有一些讨论还有一个简单的性能比较 grep -v -x -f 1.txt ip.txt 用这种方法貌似会多一些结果,不清楚为什么 |
20 webjin OP @7rack 我发现 他跟 grep -v "$ip" ip.txt | tee ip.txt 输出的结果不正确,会多删掉 1.txt里面没有的内容。 |
22 amyangfei 2014 年 3 月 7 日 @webjin cat ip.txt 192.168.0.1 192.168.0.10 192.168.0.100 比如:grep -v 192.168.0.10 ip.txt|tee ip.txt 会把192.168.0.100也删除 |
24 rrfeng 2014 年 3 月 7 日 最简洁的答案应该是 :grep -vf 1.txt ip.txt > 如果目标文件存在则直接清空 >> 追加到文件末尾 在 for 循环里用 > 每 grep 一次就清空一次 ip.txt ,当然没结果了 |
26 pfipdaniel 2014 年 3 月 7 日 grep -vF ip.txt 1.txt |
28 webjin OP @pfipdaniel 你视乎没理解我的意思。 比如 192.168.0.1-255 这是一个段IP 然后你们你们公司交换机里面 获取到了哪些IP在使用,然后你再去统计哪些IP没使用。 |
29 saihuang 2014 年 3 月 7 日 两个文件 |
30 saihuang 2014 年 3 月 7 日 楼主用if $?判断一下grep的返回,就知道这个ip在没在用了,然后单独输出到一个文件里 |
31 vzex 2014 年 3 月 7 日 awk -F"." '{a[$4]="1"} END{for(i=1;i<=255;i++){if(a[i]!="1"){print "not used:192.168.0." i} else {print "used:192.168.0." i}} }' 1.txt 不需要ip.txt |
32 vzex 2014 年 3 月 7 日 grep -v "$ip" ip.txt > ip.txt 你这个txt变空的原因是,重定向,会把ip.txt清空,才执行grep |
33 nanpuyue 2014 年 3 月 7 日 其实可以不依赖ip.txt: seq -f "192.168.0.%g" 255 | grep -vf 1.txt - > ip.txt |
34 pfipdaniel 2014 年 3 月 7 日 @webjin grep -vwF -f 1.txt ip.txt |
36 BOYPT 2014 年 3 月 7 日 这有很难么,用不着顺序的ip.txt,直接seq生成间隙数组: 1.txt如下: 192.168.0.9 192.168.0.11 192.168.0.14 192.168.0.126 192.168.0.18 命令执行: (for U in $(awk '{split($0,a,".");print a[4]}' 1.txt|sort -n) 255; do seq $BEG $((U-1)) | sed 's/^/192.168.0./'; BEG=$((U+1)); done) > r.txt 结果: $ wc -l 1.txt r.txt 5 1.txt 249 r.txt 254 total 有249个可用地址,逻辑无误。 |
37 BOYPT 2014 年 3 月 7 日 哦,上面我漏掉了手敲的初始变量BEG=1 BEG=1; (for U in $(awk '{split($0,a,".");print a[4]}' 1.txt|sort -n) 255; do seq $BEG $((U-1)) | sed 's/^/192.168.0./'; BEG=$((U+1)); done) > r.txt |