Awk column with pattern array

試著忘記壹切 提交于 2021-01-28 11:47:54

问题


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

回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/60063911/awk-column-with-pattern-array

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!