exvance
exvance

Reputation: 1439

bash: only process line if not in second file

I have this block of code:

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

I know this must be really simple. But I have another list (/tmp/excludeList). I only want to echo the line within my while loop if the line ins't found in my excludeList. How do I do that. Is there some awk statement or something?

Upvotes: 0

Views: 211

Answers (3)

glenn jackman
glenn jackman

Reputation: 247210

With 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

This reads the exclude list info an array (as the array keys) -- the NR==FNR condition is true while awk is reading the first file from the arguments. Then, while reading the include file, if the current line contains a non-space character and it does not exist in the exclude array, print it.

The equivalent with 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

Upvotes: 0

Chris Seymour
Chris Seymour

Reputation: 85883

You can do this with grep alone:

$ cat file
blue
green
red
yellow
pink

$ cat exclude 
green
pink

$ grep -vx -f exclude file
blue
red
yellow

The -v flag tells grep to only output the lines in file that are not found in exclude and the -x flags forces whole line matching.

Upvotes: 3

peteches
peteches

Reputation: 3629

use 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"

the -n $line means if $line is not empty the grep returns true if $line is found in exclude file which is inverted by the ! so returns true if the line is not found.
-x means line matched so nothing else can appear on the line
-F means fixed string so if any metacharacters end up in $line they'll be matched literally.

Hope this helps

Upvotes: 0

Related Questions