0

我有这段代码:

 while IFS=$'\n' read -r line || [[ -n "$line" ]]; do
    if [ "$line" != "" ]; then
        echo -e "$lanIP\t$line" >> /tmp/ipList;
    fi
done < "/tmp/includeList"

我知道这一定很简单。但我有另一个列表(/tmp/excludeList)。如果在我的 excludeList 中找不到该行,我只想在我的 while 循环中回显该行。我怎么做。有什么 awk 声明之类的吗?

4

3 回答 3

3

您可以grep单独执行此操作:

$ cat file
blue
green
red
yellow
pink

$ cat exclude 
green
pink

$ grep -vx -f exclude file
blue
red
yellow

-v标志告诉grep只输出file未找到的行,exclude并且-x标志强制整行匹配。

于 2013-01-16T09:40:25.740 回答
0

使用 grep

while IFS=$'\n' read -r line || [[ -n "$line" ]]; do
    if [[ -n ${line} ]] \
        && ! grep -xF "$line" excludefile &>/dev/null; then
       echo -e "$lanIP\t$line" >> /tmp/ipList;
    fi
done < "/tmp/includeList"

-n $line 表示如果 $line 不为空,则如果在排除文件中找到 $line,则 grep 返回 true,该文件被 ! 如果找不到该行,则返回 true。
-x 表示行匹配,因此行上不会出现任何其他内容
-F 表示固定字符串,因此如果任何元字符以 $line 结尾,它们将按字面意思匹配。

希望这可以帮助

于 2013-01-16T09:23:14.853 回答
0

使用 awk:

awk -v ip=$lanIP -v OFS="\t" '
    NR==FNR {exclude[$0]=1; next}
    /[^[:space:]]/ && !($0 in exclude) {print ip, $0}
' /tmp/excludeList /tmp/includeList > /tmpipList

这会读取一个数组的排除列表信息(作为数组键)——NR==FNR当 awk 从参数中读取第一个文件时,条件为真。然后,在读取包含文件时,如果当前行包含非空格字符并且它不存在于排除数组中,则打印它。

与 grep 等价:

grep -vxF -f /tmp/excludeList /tmp/includeList | while IFS= read -r line; do
    [[ -n "$line" ]] && printf "%s\t%s\n" "$ipList" "$line"
done > /tmp/ipList
于 2013-01-16T12:18:07.070 回答