awk issue expanding variables in ksh script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk issue expanding variables in ksh script
# 1  
Old 09-20-2012
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.

Code:
#set 2 variables below
JOBNAME=abc_def
AUTO=A12
 
#Below line creates a variable using comma as a field separator
DESC=`/bin/awk -F',' "/^$JOBNAME/" 
/apps/test/scripts/library/desc.output.$AUTO`

Content of /apps/test/scripts/library/desc.output.A12
Code:
abc_def,GHIJKLABCEQAVTCRIT AMM Clean Applogs

I am expecting $DESC variable to be the line in that file so my next variable I can print i.e.
Code:
DESC1=`echo $DESC | awk -F',' '{print $2}'` ;
TAGID=`echo $DESC1 | awk -F' ' '{print $1}'` ;

The problem is that although this works from the command line it doesn't in my ksh script. It doesn't assign anything to $DESC, presumably because it can't expand the JOBNAME and AUTO variables.

Can anyone please advise if this is just a quoting issue and if so - what syntax should work?

Thanks,
Gary

Last edited by Corona688; 09-20-2012 at 06:55 PM..
# 2  
Old 09-20-2012
awk seems to be the wrong tool for the job for this. grep would be more straightforward. Also, you don't need to run awk 9 times to extract 9 columns from 1 line, the shell is quite capable of splitting things by itself.

Code:
OLDIFS="$IFS"
IFS=","
        set -- `grep "^$JOBNAME," /apps/test/scripts/library/desc.output.$AUT`
IFS="$OLDIFS"

echo "DESC1 is $1"
echo "TAGID is $2"
...

# 3  
Old 09-20-2012
Quote:
Originally Posted by gazza-o
Code:
#set 2 variables below
JOBNAME=abc_def
AUTO=A12
 
#Below line creates a variable using comma as a field separator
DESC=`/bin/awk -F',' "/^$JOBNAME/" 
/apps/test/scripts/library/desc.output.$AUTO`

maybe it's the newline between `` Try with a \

Code:
 DESC=`/bin/awk -F',' "/^$JOBNAME/" \
/apps/test/scripts/library/desc.output.$AUTO`

# 4  
Old 09-20-2012
I agree with Corona688 that awk isn't needed for this job. But, if you're just having trouble understanding how to use the various quotes, this is one way to use one invocation of awk to do the job:
Code:
#!/bin/ksh
JOBNAME=abc_def
AUTO=A12awk -F',' -v jobname="$JOBNAME" '$1 == jobname {
        printf("%s\n%s\n",$0,$2)
        sub(/ .*/,"",$2)
        printf("%s\n",$2)
}' /apps/test/scripts/library/desc.output.$AUTO | (
        read DESC        printf "DESC is \"%s\"\n" "$DESC"
        read DESC1        printf "DESC1 is \"%s\"\n" "$DESC1"
        read TAGID        printf "TAGID is \"%s\"\n" "$TAGID"
)


Last edited by Don Cragun; 09-20-2012 at 09:53 PM.. Reason: I missed the fact that FS changed between the 2nd and 3rd calls to awk.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

[BASH] eval command not expanding variables as expected.

Hi Guys, I wrote a collection of bash functions years ago and now need to use them again but I'm getting some error messages when eval tries to expand the variables names. I recollect that I used the shopt command to set one of the options but I can't quite remember the command that I... (8 Replies)
Discussion started by: ASGR
8 Replies

2. Shell Programming and Scripting

Possible ksh93 Bug Expanding Variables?

My OS is Linux (kernel 4.08.something) and AIX (7100-04-01-1543), the used ksh versions are: ksh88: Version M-11/16/88f (AIX) ksh93: Version M 93t+ 2009-05-01 (AIX), Version M 93u (Linux) When writing a parser for stanza files in ksh i encountered a rather strange behavior. Here is a... (4 Replies)
Discussion started by: bakunin
4 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

ksh passing to awk multiple dyanamic variables awk -v

Using ksh to call a function which has awk script embedded. It parses a long two element list file, filled with text numbers (I want column 2, beginning no sooner than line 45, that's the only known thing) . It's unknown where to start or end the data collection, dynamic variables will be used. ... (1 Reply)
Discussion started by: highnthemnts
1 Replies

5. Shell Programming and Scripting

Awk script problem - Variables Causing Issue

can someone please explain to me what i'm doing wrong with this 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... (1 Reply)
Discussion started by: SkySmart
1 Replies

6. Shell Programming and Scripting

AWK Variable assignment issue Ksh script

Hi There, I am writing a ksh script which assigns variable values from file "A" and passes that variables to file "B". While passing the parameters an additional "$" sign is being assigned to awk -v option. Could any one help me with this please. #!/bin/ksh head -1... (3 Replies)
Discussion started by: Jeevanm
3 Replies

7. UNIX for Dummies Questions & Answers

Expanding variables with Ed Bash 3.2.33

Hi, The following code finds the line containing fruits in test.txt and replaces instances of apple with banana. ed -s test.txt <<< $'/fruits/s/apple/banana/g\nw' What I want to do is put variables in the place of fruits, apple and banana. I have tried replacing ' with " to get... (2 Replies)
Discussion started by: de_la_espada
2 Replies

8. Shell Programming and Scripting

accessing ksh variables from awk

hi everybody! i am running this ksh script for replacing a set of strings by another set of new ones. i am getting both these from a file. also, the strings that i want to replace, are sub-strings(can occur more than once in each chunk) in a big chunk of data that i have bulk-copied(bcp utility)... (1 Reply)
Discussion started by: trupti wagh
1 Replies

9. UNIX for Advanced & Expert Users

Expanding Variables in User Input

If have var='$variable' how can I expand $variable. I have tried many thing like duble quotes/braces etc, but nothing worked. I need the solution ASAP. (2 Replies)
Discussion started by: Bsk
2 Replies

10. Shell Programming and Scripting

passing variables to awk from ksh script

I'm trying to write a ksh script that uses awk, but I want to pass variables to awk. For example (not working): if ];then searchstr=$1 lsof -i | awk '{if($9~/SEARCHSTR/) print $2} SEARCHSTR=$searchstr' else echo "usage: $0 <search string>" fi I tried several options. Is it... (3 Replies)
Discussion started by: rein
3 Replies
Login or Register to Ask a Question