I cannot reproduce what's happening in your codes but I see all is syntaxically fine and working; but here is what you seems are trying to do with shell-loops that you would better to avid it. Below I used awk
command alone.
gawk 'ARGIND==2{ strings[$0]; next; };
(substr($0, 2, 5) in strings);
' RS='[[:space:]]+' strings.txt RS='\n' criteria.txt
Here we are reading all strings (split on whitespaces or \n
ewline [[:space:]]
in "strings.txt" file into an awk
associated array (we name it strings
).
The ATGIND==2
controlling the input and to run first block of the code only for the "strings.txt" file and awk will skip running that block for next input(s). see here why we preferred this over more common use of NR==FNR
.
when next file "criteria.txt" opened for processing, we set Record Separator to default \n
ewline in RS='\n'
; and in (substr($0, 2, 5) in strings)
we are checking if the expected substr
ing start from position 2 with character length of 5 exist in our array or not, if it was, then the line will go to output .
Input data:
strings.txt
:
CDA01 CDA02
CDA03
CDA03 CDA05 CDA06
criteria.txt
:
xCDA01 something
xxCDA02 someotherthing
CDA01
vcCDA03 oCDA03
vCDA05 end
Output:
xCDA01 something
vCDA05 end
+ awk -v k=CDA01 '{if (substr($0,2,5) == k )print $0}' criteria5.txt
, seek=CDA01
; also do not use shell-loops for text-processing purposesif (substr($0,2,5) == k )print k, $0}
;k
with anything inside theawk
code for the purpose of showing it in theset -x
trace output, but you can rest assured that-v k="$ds"
will definitely make the value of thek
variable that of$ds
in the shell. If the code does what you want it to do, none of us can tell. In this case though, it may be more convenient to not use a shell loop and instead use a more sophisticatedawk
command (which would be much quicker).join
as I in my answer to that last example).