awk string comparison unterminated quoted string andrule of thumb

I have the logic below to look up for matches within the columns between the two files with awk.

In the if statement is where the string comparison is attempted with ==
The issue seems to be with the operands, as

  1. when " '${SECTOR[$i]}' " -- double quote followed by single quote -- awk matches all strings without space but cannot strings with space
    ex. find matches when Financials==Financials
    but cannot when Consumer Cyc == Consumer Cyc and produced unterminated string error

awk: cmd. line:1: if ( $1=="GB" && $3=="Consumer
awk: cmd. line:1: ^ unterminated string

  1. when ' "${SECTOR[$i]}" ' -- single quote followed by double quote -- awk matches all strings with space but cannot strings without space
    ex. find matches when Consumer Cyc == Consumer Cyc
    but cannot when Financials == Financials

no error however no match and prints out sample outputs where should

Any comment is appreciated

#! /bin/sh
#! /bin/bash
#########################################

line=`wc -l LE_Sample.csv | awk '{print $1}'`
for (( i=2; i<=$line; i++ ))
do

COUNTRY[$i]=`awk -F"," ' (NR=='$i'){ print $4 }' ./LE_Sample.csv`
REGION[$i]=`awk -F"," ' (NR=='$i'){ print $5 }' ./LE_Sample.csv`
SECTOR[$i]=`awk -F"," ' (NR=='$i'){ print $6 }' ./LE_Sample.csv`
MKTCAP[$i]=`awk -F"," ' (NR=='$i'){ print $7 }' ./LE_Sample.csv`

awk -F"," '{
   if ( $1=="'${COUNTRY[$i]}'" && $3=="'${SECTOR[$i]}'" &&  $4 <= '${MKTCAP[$i]}' && $5 >= '${MKTCAP[$i]}' )
  {
        print NR
        print $1
        print $3
        print $4
        print $5
        print '${MKTCAP[$i]}'
  }
}' ./CCModels.csv

done

Why don't you use the mechanism meant to pass parameters into awk : awk -vAWKVAR1="$SHELLVAR1" -vAWKVAR2="$SHELLVAR2" etc ...

1 Like