Issue with AWK using a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Issue with AWK using a variable
# 1  
Old 03-20-2012
Issue with AWK using a variable

Hi,

I am doing an AWK in ksh as below with the string to search to be read from variable but for some reason there is no output. It works when I hard code it.
Code:
awk 'substr($0,22,6)=="${VAR}"' XXX.txt' >YYY.txt

On reading other posts I tried below option,
Code:
'substr($0,22,6)=="/"${VAR}/""'

Also tried concatenating the quotes within the variable for ex if VAR=2011 I made it to VAR="2011".

When I did an echo of the awk the comment appears but the output is not generated. Please help.

Last edited by Franklin52; 03-20-2012 at 03:14 PM.. Reason: Please use code tags for data and code samples, thank you
# 2  
Old 03-20-2012
It's generally better to use the -v option than to creatively quote things to get variables inside awk.

Code:
awk -v VAR="${VAR}" 'substr($0,22,6)==VAR' input > output

If that doesn't work, then I suggest that your substring really doesn't equal var for some reason -- one off perhaps, or spaces in places you didn't expect.
# 3  
Old 03-20-2012
You missed a quote here:
Quote:
Originally Posted by junnujayz
Code:
awk 'substr($0,22,6)=='"${VAR}"' XXX.txt' >YYY.txt

# 4  
Old 03-20-2012
That worked like a charm. Thanks! I would have saved 5 hrs had I started asking from here. You guys are great!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grepping for one variable while using awk to parse an associated variable

Im trying to search for a single variable in the first field and from that output use awk to extract out the lines that contain a value less than a value stored in another variable. Both the variables are associated with each other. Any guidance is appreciated. File that contains the... (6 Replies)
Discussion started by: ncwxpanther
6 Replies

2. UNIX for Beginners Questions & Answers

How can I assign awk's variable to shell script's variable?

I have the following script, and I want to assign the output ($10 and $5) from awk to N and L: grdinfo data.grd | awk '{print $10,$5}'| read N L output from gridinfo data.grd is: data.grd 50 100 41 82 -2796 6944 0.016 0.016 3001 2461. where N and L is suppose to be 3001 and 100. I use... (8 Replies)
Discussion started by: geomarine
8 Replies

3. Shell Programming and Scripting

Issue when printing filename through cygwin using a variable with awk

Hi, If i were to do this an print out the file, it will show as it is in the command $ awk '/Privilege Use/ {P=0} /Object Access/ {P=1} P' AdvancedAudit.txt Object Access File System No Auditing Registry No Auditing Kernel... (1 Reply)
Discussion started by: alvinoo
1 Replies

4. Shell Programming and Scripting

Variable value substitution issue with awk command issue

Hi All, I am using the below script which has awk command, but it is not returing the expected result. can some pls help me to correct the command. The below script sample.ksh should give the result if the value of last 4 digits in the variable NM matches with the variable value DAT. The... (7 Replies)
Discussion started by: G.K.K
7 Replies

5. Shell Programming and Scripting

AWK variable substitute issue

dear, I have below file called folderlist.txt # ParentFolder environment_flag SubFolders triss 1 checksum bookstructure 1 fx 1 checksum_GMDB I have a script which which will create the folders under... (3 Replies)
Discussion started by: manas_ranjan
3 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. Shell Programming and Scripting

using awk for setting variable but change the output of this variable within awk

Hi all, Hope someone can help me out here. I have this BASH script (see below) My problem lies with the variable path. The output of the command find will give me several fields. The 9th field is the path. I want to captured that and the I want to filter this to a specific level. The... (6 Replies)
Discussion started by: Cowardly
6 Replies

8. Shell Programming and Scripting

variable issue

Hi, I'm sure that it's a very simple issue. this is a part of my code : while read ligne do result=`ls -R ../FILES/|grep "."$ligne"$"` echo $result done<TYPE_EXT_FILES.txt the echo return nothing (as my variable is empty). I'm sure that the problem... (10 Replies)
Discussion started by: skubann
10 Replies

9. Shell Programming and Scripting

assign awk's variable to shell script's variable?

Dear All, we have a command output which looks like : Total 200 queues in 30000 Kbytes and we're going to get "200" and "30000" for further process. currently, i'm using : numA=echo $OUTPUT | awk '{print $2}' numB=echo $OUTPUT | awk '{print $5}' my question is : can I use just one... (4 Replies)
Discussion started by: tiger2000
4 Replies

10. Programming

Have issue with variable

Hi All, i am new to unix.. i have an issue with my unix script...let me explain the task that i want to make script.... i prepared script which will connect data base from my linux box using sqlplus cmd... however in that i want to use the variable like below.. select * from... (0 Replies)
Discussion started by: Shahul
0 Replies
Login or Register to Ask a Question