1

Is it possible to do this but use an actual array of strings where it says "array"

array=(cat
dog
mouse
fish
...)

awk -F "," '{ if ( $5!="array" ) { print $0; } }' file

I would like to use spaces in some of the strings in my array. I would also like to be able to match partial matches, so "snow" in my array would match "snowman" It should be case sensitive.

Example csv

s,dog,34
3,cat,4
1,african elephant,gd
A,African Elephant,33
H,snowman,8
8,indian elephant,3k
7,Fish,94
...

Example array

snow
dog
african elephant

Expected output

s,dog,34
H,snowman,8
1,african elephant,gd

Cyrus posted this which works well, but it doesn't allow spaces in the array strings and wont match partial matches.

echo "${array[@]}" | awk 'FNR==NR{len=split($0,a," "); next} {for(i=1;i<=len;i++) {if(a[i]==$2){next}} print}' FS=',' - file
0

2 Answers 2

2

The brief approach using a single regexp for all array contents:

$ array=('snow' 'dog' 'african elephant')
$ printf '%s\n' "${array[@]}" | awk -F, 'NR==FNR{r=r s $0; s="|"; next} $2~r' - example.csv
s,dog,34
1,african elephant,gd
H,snowman,8

Or if you prefer string comparisons:

$ cat tst.sh
#!/bin/env bash

array=('snow' 'dog' 'african elephant')

printf '%s\n' "${array[@]}" |
awk -F',' '
    NR==FNR {
        array[$0]
        next
    }
    {
        for (val in array) {
            if ( index($2,val) ) {      # or $2 ~ val for a regexp match
                print
                next
            }
        }
    }
' - example.csv

$ ./tst.sh
s,dog,34
1,african elephant,gd
H,snowman,8
Sign up to request clarification or add additional context in comments.

3 Comments

Thats perfect thanks. How could I do the reverse? Print if there is no match.
You're welcome. I'm sure you could figure that out with a couple minutes of thought (hint: the "not" symbol is !) but if you can't then ask a new question as everyone hates chameleon questions.
I think it needs to go in this line but not sure how. if ( index($3,val) )
1

This prints no line from csv file which contains an element from array in column 5:

echo "${array[@]}" | awk 'FNR==NR{len=split($0,a," "); next} {for(i=1;i<=len;i++) {if(a[i]==$5){next}} print}' FS=',' - file

1 Comment

Is it possible to have strings with spaces in my array? What about partial matching? So "snow" in my array would match "snowman". Should have mentioned these in the OP.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.