删除包含单词列表中单词的行

时间:2014-02-14 02:41:43

标签: excel bash sed

如何删除列表中包含数字的行? bash或任何其他脚本语言或excel或应用程序。

这是我的例子:

COLUMN A                COLUMN B
____________________________________________
192.168.1.1:27794           27800
192.168.1.1:27795           27799
192.168.1.1:27796           27794
192.168.1.1:27797           27795
192.168.1.1:27798
192.168.1.1:27799
192.168.1.1:27800
192.168.1.1:27801

A列和B列是分开的文件。

列A full.txt B列todelfromfull.txt

因此,如果A列中的行包含B列中的单词,我希望删除B列中的A行

我在bash中尝试了sed,但没有成功

非常感谢

3 个答案:

答案 0 :(得分:2)

使用awk

awk -F : 'NR==FNR{a[$1];next} !($2 in a) ' todelfromfull.txt full.txt

解释

  • -F :字段拆分:
  • NR==FNR{a[$1];next}读取第一个文件todelfromfull.txt,将端口保存在关联数组a
  • !($2 in a)如果第2列不在数组a中,请将其打印出来。

答案 1 :(得分:2)

使用GNU grep:

fgrep -vw -f todelfromfull.txt full.txt

答案 2 :(得分:0)

你也可以使用Bash:

file=$(< todelfromfull.txt)
while IFS=: read ip port; do
    n=0
    for i in $file; do
        if [[ $i == $port ]]; then
            ((n++))
            break
        fi
    done
    if [ $n -eq 0 ]; then
        echo $ip $port
    fi
done < full.txt
相关问题