awk string comparison unterminated quoted string andrule of thumb


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk string comparison unterminated quoted string andrule of thumb
# 1  
Old 10-09-2015
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

2. 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

Code:
#! /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


Last edited by vbe; 10-09-2015 at 08:33 AM.. Reason: code tags please not ICODE thanks
# 2  
Old 10-09-2015
Why don't you use the mechanism meant to pass parameters into awk: awk -vAWKVAR1="$SHELLVAR1" -vAWKVAR2="$SHELLVAR2" etc ...
This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Getting " Unterminated quoted string"

Hi Guys, When I am executing the script #! /bin/bash SARBACKUPS=/home/pradeep/sarBackups cd /var/log/sysstat ls -1t sar* | while read SARNAME do cp -p "$SARNAME" $( echo "$SARBACKUPS"/"$HOSTNAME"_"$SARNAME"_"`date +"%Y%m%d`.bkup ) done I am getting final.sh: 1: Syntax... (3 Replies)
Discussion started by: Pradeep_1990
3 Replies

2. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

3. Shell Programming and Scripting

AWK string comparison

Hey guys.. New in linux scripting and need some help on some scripting with history command. I managed to export the command history into a file and now i'm trying to select from that file some specific commands that were made in a certain period. Here's what i got so far echo -n... (2 Replies)
Discussion started by: mishu_cgm
2 Replies

4. Homework & Coursework Questions

passing letters from an array into a string for string comparison

attempting the hangman program. This was an optional assignment from the professor. I have completed the logical coding, debugging now. ##I have an array $wordString that initializes to a string of dashes ##reflecting the number of letters in $theWord ##every time the user enters a (valid)... (5 Replies)
Discussion started by: lotsofideas
5 Replies

5. Shell Programming and Scripting

to extract string from main string and string comparison

continuing from my previous post, whose link is given below as a reference https://www.unix.com/shell-programming-scripting/171076-shell-scripting.html#post302573569 consider there is create table commands in a file for eg: CREATE TABLE `Blahblahblah` ( `id` int(11) NOT NULL... (2 Replies)
Discussion started by: vivek d r
2 Replies

6. Shell Programming and Scripting

Unterminated quoted string

Hello! I wroted a little script that should check for new updates on a server and get them if any. The problem is, every time I run it with sh, I'm getting an "script: 20: Syntax error: Unterminated quoted string" error! The problem is, there isn't any "unterminated quoted string" in my script:... (2 Replies)
Discussion started by: al0x
2 Replies

7. Shell Programming and Scripting

Syntax error: Unterminated quoted string

I keep having problems when exicuting this file. It always gives me the error message "36: Syntax error: Unterminated quoted string" If someone could help me edit this it would be much appreciated. #!/bin/sh # # This will continue adding numbers # untill the total is greater than 1,000 #... (5 Replies)
Discussion started by: evilSerph
5 Replies

8. Shell Programming and Scripting

how to delete blanks inside a quoted string

Hi I need to update a string inside a file which looks like the following: PX_LIST=" 4119 2390 2294 2776 2897 4099 " Is there a way to get rid of the blanks after the first quote mark and before the last quote mark. This needs to be done ONLY for the string named PX_LIST (there are some... (4 Replies)
Discussion started by: aoussenko
4 Replies

9. UNIX for Dummies Questions & Answers

Unterminated string

While running a shell script i am getting this warning but the script is working fine.while running the blocks of the scripts individually its running fine.But while pasting it combinedly this is the warning..wat may be the reason behind this and how to resolve it (3 Replies)
Discussion started by: dr46014
3 Replies

10. Shell Programming and Scripting

String Comparison between two files using awk

I have two files with field seperator as "~". File A: 12~13~14~15 File B: 22~22~32~11 i want to calculate the difference between two files and than calculate the percentage difference and output it to a new file. How do i do this using awk. Also please suggest GOOD awk tutorials. Thank... (7 Replies)
Discussion started by: rudoraj
7 Replies
Login or Register to Ask a Question