Awk script problem - Variables Causing Issue


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk script problem - Variables Causing Issue
# 1  
Old 03-15-2012
Awk script problem - Variables Causing Issue

can someone please explain to me what i'm doing wrong with this code:

Code:
WELT=$(awk '(($1 ~ "^${caag}$") || ($2 ~ "^${caag}$"))' /tmp/Compare.TEXT)


when run from the command line, it works. but it seems to be having a problem doing the comparison when variables are involved.

i tested from the command line and assigned a value to "caag", it wouldn't work.

but when i just replaced caag on the command line with the actual value, it passes.

i've been stalled all day by this. any help will be much appreciated.
# 2  
Old 03-15-2012
Variables do not expand inside single quotes:

Code:
$ echo '${HOME}'
${HOME}

$

Awk has better means to pass variables than embedding them in the string, fortunately:

Code:
WELT=$(awk -v CAAG="^${caag}\$" '(($1 ~ CAAG) || ($2 ~ CAAG))' /tmp/Compare.TEXT)

If awk complains about this syntax, try nawk. Some systems keep a very old awk around for compatibility reasons.
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Issue in shell script variables

Hi, I have a file at $HOME/t.txt, below is file content cat $HOME/t.txt CUSTOMER_${REGION}.out Am using this file content in one script $HOME/samp.sh, below is the script #!/bin/bash REGION=USA x=`cat ${HOME}/t.txt` echo $x Am getting following output.. CUSTOMER_${REGION}.out ... (3 Replies)
Discussion started by: shieksir
3 Replies

2. Solaris

Solaris 5.8 Upgrade is causing Date format issue.

Hi I have a JAVA based application in development and production environment. These two environments are exactly identical in terms of OS, hardware and application components. Initially the OS was Solaris 5.6 and the application was working fine in both environments. But later when the OS... (12 Replies)
Discussion started by: Jayant Tripathi
12 Replies

3. Shell Programming and Scripting

Scripting Issue with Variables from awk

Greetings all, Disclaimer: I'm a novice and always welcome best practices as I'm learning. File example: 100,1.1.1.1,1.1.1.2,10.10.10.1,172.16.1.10,172.16.1.20 101,1.1.2.1,1.1.2.2,10.10.20.1,172.16.2.10,172.16.2.20 102,1.1.3.1,1.1.3.2,10.10.30.1,172.16.3.10,172.16.3.20 ...and so on ... (3 Replies)
Discussion started by: sjrupp
3 Replies

4. Shell Programming and Scripting

Text qualifier issue causing data alignment problem

Hello Everyone, I have a csv file with text qualifier as "" and data similar to below: "1","abc","address1","US" "2","def","address1 "characters in double-quote" address2","IND" "3","ghi","address1","UK" In above example, for record 2, we have an issue as in column3 contains double... (2 Replies)
Discussion started by: AnkitSenghani
2 Replies

5. Shell Programming and Scripting

awk issue expanding variables in ksh script

Hi Guys, I have an issue with awk and variables. I have trawled the internet and forums but can't seem to get the exactt syntax I need. I have tried using awk -v and all sorts of variations but I have hit a brick wall. I have spent a full day on this and am just going round in circles. ... (3 Replies)
Discussion started by: gazza-o
3 Replies

6. UNIX for Dummies Questions & Answers

Issue with variables via cronned script

I have script sourcing the profile of functional user like as below and my .profile for functional user has below entry now when i cron the script i am receiving the below error after execution, though i could see the output of variable when i do echo as below. can you tell me where... (11 Replies)
Discussion started by: Ariean
11 Replies

7. Shell Programming and Scripting

Problem with variables in awk

Hi, I've a problem with wariables in awk. My example: $ cat test.txt 12|aaaa 13|bbbb 012|cccc 0123|dddd $ cat test.awk $1 == var {print $0} $ cat test.sh /usr/xpg4/bin/awk -F"|" -v var="012" -f test.awk test.txt (5 Replies)
Discussion started by: apresa
5 Replies

8. IP Networking

Could a tcp issue be causing a null pointer exception?

The client's app gets a 'suspend error' which they say is due to a null pointer exception. Application people say nothing's wrong with the app. Network people say the network's fine. I'm supposed to see what's wrong with the system to be causing this error. I checked the NIC card settings, which... (1 Reply)
Discussion started by: pmichner
1 Replies

9. Shell Programming and Scripting

Environment variables causing cronjob to fail

I am trying to run a script which makes use of these environment variables, hence i need to export them out. i've posted a similar post in an oracle forum. I've posted the same qn here to get some advise on the scripting aspect. i execute my program in an unix environment by typeing... (2 Replies)
Discussion started by: new2ss
2 Replies

10. UNIX for Dummies Questions & Answers

Variable for -name causing issue in Find command

Hi there, I'm trying to find files that are greater then 30 days old, zip them and move to a different directory. I'm encountering an issue passing a variable (FilesToFind) to name within the find command. Here's the code I'm running: #! /usr/bin/sh FileDir=/home/ariba... (2 Replies)
Discussion started by: ParNone
2 Replies
Login or Register to Ask a Question