Here is an alternative to coding in awk . The crush toolset contains a number of programs to deal with CSV (DSV) files. One such is grepfield .
In this particular case, we need to collect the lines which contain the strings to search for. That's done with paste . After that, it's a simple command line:
#!/usr/bin/env bash
# @(#) s2 Demonstrate filtering for specific field, crush:grepfield.
# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
em() { pe "$*" >&2 ; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C paste grepfield dixf
rm -f f1
E=expected-output.txt
pl " Input data file data[12]:"
head data[12]
pl " File data2 pasted together into variable v1:"
v1="$(paste -d'|' -s data2)"
pe " v1 = $v1"
pl " Expected output:"
cat $E
pl " Results, grep field 4, pipe delimiter:"
# Gathering lines -- from an idea at:
# https://stackoverflow.com/questions/15758814/turning-multiple-lines-into-one-line-with-comma-separated-perl-sed-awk
grepfield -d '|' -v -f 4 "$v1" data1 |
tee f1
pl " Verify results if possible:"
C=$HOME/bin/pass-fail
[ -f $C ] && $C || ( pe; pe " Results cannot be verified." ) >&2
pl " Details on grepfield (one tool in crush suite):"
dixf grepfield
exit 0
producing:
$ ./s2
Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution : Debian 8.7 (jessie)
bash GNU bash 4.3.30
paste (GNU coreutils) 8.23
grepfield - ( local: /usr/local/bin/grepfield, 2017-05-17 )
dixf (local) 1.49
-----
Input data file data[12]:
==> data1 <==
A|B|C|167|D|E
C|K|D|123|D|E
A|B|D|789|G|F
C|D|G|143|A|B
==> data2 <==
123
456
780
143
-----
File data2 pasted together into variable v1:
v1 = 123|456|780|143
-----
Expected output:
A|B|C|167|D|E
A|B|D|789|G|F
-----
Results, grep field 4, pipe delimiter:
A|B|C|167|D|E
A|B|D|789|G|F
-----
Verify results if possible:
-----
Comparison of 2 created lines with 2 lines of desired results:
Succeeded -- files (computed) f1 and (standard) expected-output.txt have same content.
-----
Details on grepfield (one tool in crush suite):
grepfield looks for patterns within a specific field of a flat-file (man)
Path : /usr/local/bin/grepfield
Version : - ( local: /usr/local/bin/grepfield, 2017-05-17 )
Type : ELF 64-bit LSB executable, x86-64, version 1 (SYSV ...)
Help : probably available with -h,--help
Home : http://crush-tools.googlecode.com (doc)