Using double quotes in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using double quotes in awk
# 1  
Old 03-28-2011
Using double quotes in awk

Hi

I read somewhere that when using double quotes in awk; variables gets expanded else it doesn't.

So I tried to use the double quotes inside an awk statement as below:

Code:
 
from_instance_trans=`awk "/INPUT =\"$frm_inst\"/,/<\/TRANSFORMATION>/" $xml_object | grep -w "<TRANSFIELD" | awk -F\" "{ if ($0 ~ "$frm_inst") { print $14":"$16} }"`

And I'm having problem with the part I've colored in red.
When the script executes I get the following error:
[NOTE: $frm_inst=STAGE_TABLE]

Code:
 
awk: { if (./name_of_the_script.ksh ~ STAGE_TABLE) { print 4 6} }
awk:       ^ syntax error

While at the command line if I use the same syntax but with single quotes I get correct result

Code:
 
awk '/INPUT ="STAGE_TABLE"/,/<\/TRANSFORMATION>/' test_file.XML | grep -w '<TRANSFIELD' | awk -F\" '{ if ($0 ~ "EMP_KEY")  { print $14":"$16} }'
 
results into
-----------
15:0

The o/p of the command
Code:
awk '/INPUT ="STAGE_TABLE"/,/<\/TRANSFORMATION>/' test_file.XML | grep -w '<TRANSFIELD'

is
Code:
 
<TRANSFIELD DATATYPE ="double" DEFAULTVALUE ="" DESCRIPTION ="" FIELD ="EMP_KEY" PICTURETEXT ="" PORTTYPE ="OUT" PRECISION ="15" SCALE ="0"/>
<TRANSFIELD DATATYPE ="string" DEFAULTVALUE ="" DESCRIPTION ="" FIELD ="DEPT_KEY" PICTURETEXT ="" PORTTYPE ="OUT" PRECISION ="7" SCALE ="0"/>
<TRANSFIELD DATATYPE ="string" DEFAULTVALUE ="" DESCRIPTION ="" FIELD ="MGR_NAME" PICTURETEXT ="" PORTTYPE ="OUT" PRECISION ="32" SCALE ="0"/>
<TRANSFIELD DATATYPE ="decimal" DEFAULTVALUE ="" DESCRIPTION ="" FIELD ="SAL_CODE" PICTURETEXT ="" PORTTYPE ="OUT" PRECISION ="3" SCALE ="0"/>
<TRANSFIELD DATATYPE ="string" DEFAULTVALUE ="" DESCRIPTION ="" FIELD ="PCT_RISE" PICTURETEXT ="" PORTTYPE ="IN" PRECISION ="7" SCALE ="0"/>

and I'm trying to extract PRECISION & SCALE field's values. Smilie
Any insight into this would be much appreciated.

-dips
# 2  
Old 03-28-2011
Generally, when invoking awk from CL, you want to use single quotes to prevent shell from expanding.
If you use double quotes instead of single, shell will expand $0 into the name of the script. When you use single quotes, shell passes $0 to awk literally, and awk interprets it as the whole record, which is what you wanted.

Here is how I'd awk it out:
Code:
 
awk '/EMP_KEY/ {for(i=1;i<=NF;i++){if($i~"PRECISION|SCALE") { print gensub(/[^0-9]/,"","g",$(i+1)); } } }'


Processing only lines that contain "EMP_KEY", looping through all fields (in case PRECISION and SCALE can be on different places); if the field matches "PRECISION" or "SCALE", 'gensub' call will replace all non-numbers [^0-9] in next field ($i+1) with empty string. "g" is a "global replacement" flag.
# 3  
Old 03-28-2011
Hi mirni,
ok got the point.
But the problem is that the below code line will be a part of a loop which reads a xml file and extracts the $frm_inst
And again in your code awk won't expand the variable $frm_inst. What I mean is that the pattern keeps on changing.

Quote:
Originally Posted by mirni
Code:
 
awk '/EMP_KEY/ {for(i=1;i<=NF;i++){if($i~"PRECISION|SCALE") { print gensub(/[^0-9]/,"","g",$(i+1)); } } }'

I encountered a post in this forum talking about the same issue. I tried this also but it doesn't work Smilie.
https://www.unix.com/shell-programmin...xpression.html
Please advice.

-dips
# 4  
Old 03-28-2011
you can pass a variable to awk with -v switch:
Code:
awk -v "myVar=$frm_inst" '{if($0~myVar){for(i=1;i<=NF;i++){if($i~"PRECISION|SCALE") { print gensub(/[^0-9]/,"","g",$(i+1)); } } } }'

Or just plug in the shell variable directly, turning off the single quote beforehand and turning it back on after:
Code:
 awk  '/'$frm_inst'/ {for(i=1;i<=NF;i++){if($i~"PRECISION|SCALE") { print gensub(/[^0-9]/,"","g",$(i+1)); } } }'

This User Gave Thanks to mirni For This Post:
# 5  
Old 03-31-2011
Hi mirni,

Brilliant! Smilie

There's only one problem with the code you have provided it matches two fields say EMP_KEY and also I_EMP_KEY. Is there a way to restrict to an exact match?

-dips
# 6  
Old 03-31-2011
Code:
awk -F'"' '{print $(NF-3),$(NF-1)}' infile

Code:
# cat tst
<TRANSFIELD DATATYPE ="double" DEFAULTVALUE ="" DESCRIPTION ="" FIELD ="EMP_KEY" PICTURETEXT ="" PORTTYPE ="OUT" PRECISION ="15" SCALE ="0"/>
<TRANSFIELD DATATYPE ="string" DEFAULTVALUE ="" DESCRIPTION ="" FIELD ="DEPT_KEY" PICTURETEXT ="" PORTTYPE ="OUT" PRECISION ="7" SCALE ="0"/>
<TRANSFIELD DATATYPE ="string" DEFAULTVALUE ="" DESCRIPTION ="" FIELD ="MGR_NAME" PICTURETEXT ="" PORTTYPE ="OUT" PRECISION ="32" SCALE ="0"/>
<TRANSFIELD DATATYPE ="decimal" DEFAULTVALUE ="" DESCRIPTION ="" FIELD ="SAL_CODE" PICTURETEXT ="" PORTTYPE ="OUT" PRECISION ="3" SCALE ="0"/>
<TRANSFIELD DATATYPE ="string" DEFAULTVALUE ="" DESCRIPTION ="" FIELD ="PCT_RISE" PICTURETEXT ="" PORTTYPE ="IN" PRECISION ="7" SCALE ="0"/>
# awk -F'"' '{print $(NF-3),$(NF-1)}' tst
15 0
7 0
32 0
3 0
7 0
# awk -F'"' '{print $(NF-3)":"$(NF-1)}' tst
15:0
7:0
32:0
3:0
7:0
# awk -F'"' '/EMP_KEY/{print $(NF-3)":"$(NF-1)}' tst
15:0

---------- Post updated at 02:31 PM ---------- Previous update was at 02:06 PM ----------

Example of exact match

Code:
# cat tst
<TRANSFIELD DATATYPE ="double" DEFAULTVALUE ="" DESCRIPTION ="" FIELD ="EMP_KEY" PICTURETEXT ="" PORTTYPE ="OUT" PRECISION ="15" SCALE ="0"/>
<TRANSFIELD DATATYPE ="double" DEFAULTVALUE ="" DESCRIPTION ="" FIELD ="I_EMP_KEY" PICTURETEXT ="" PORTTYPE ="OUT" PRECISION ="22" SCALE ="5"/>
<TRANSFIELD DATATYPE ="string" DEFAULTVALUE ="" DESCRIPTION ="" FIELD ="DEPT_KEY" PICTURETEXT ="" PORTTYPE ="OUT" PRECISION ="7" SCALE ="0"/>
<TRANSFIELD DATATYPE ="string" DEFAULTVALUE ="" DESCRIPTION ="" FIELD ="MGR_NAME" PICTURETEXT ="" PORTTYPE ="OUT" PRECISION ="32" SCALE ="0"/>
<TRANSFIELD DATATYPE ="decimal" DEFAULTVALUE ="" DESCRIPTION ="" FIELD ="SAL_CODE" PICTURETEXT ="" PORTTYPE ="OUT" PRECISION ="3" SCALE ="0"/>
<TRANSFIELD DATATYPE ="string" DEFAULTVALUE ="" DESCRIPTION ="" FIELD ="PCT_RISE" PICTURETEXT ="" PORTTYPE ="IN" PRECISION ="7" SCALE ="0"/>
# nawk -F'"' -v V="EMP_KEY" '$8==V{print $(NF-3)":"$(NF-1)}' tst
15:0
# nawk -F'"' -v V="I_EMP_KEY" '$8==V{print $(NF-3)":"$(NF-1)}' tst
22:5


Last edited by ctsgnb; 03-31-2011 at 09:16 AM..
# 7  
Old 03-31-2011
Hi ctsgnb,

I don't have nawk command on my m/c Smilie.

Quote:
Linux xxxxxxxx 2.6.18-164.el5 #1 SMP Tue Aug 18 15:51:48 EDT 2009 x86_64 x86_64 x86_64 GNU/Linux
Also I can't use NF as not necessarily PRECISION & SCALE fields would at NF-1 & NF-3 positions.

-dips
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk -File contain double quotes

Hi ALL, file data like : test.csv a,b,"c,d" my awk version is 4.0.2 ,if i am using the below code is working fine. awk -vFPAT='(*)|("+")' -vOFS="," '{print $3}' test.csv if the awk version is 3.1.7 is not working . Could you please help me on this one. output should be : "c,d" (6 Replies)
Discussion started by: bmk123
6 Replies

2. Shell Programming and Scripting

Replace Double quotes within double quotes in a column with space while loading a CSV file

Hi All, I'm unable to load the data using sql loader where there are double quotes within the double quotes As these are optionally enclosed by double quotes. Sample Data : "221100",138.00,"D","0019/1477","44012075","49938","49938/15043000","Television - 22" Refurbished - Airwave","Supply... (6 Replies)
Discussion started by: mlavanya
6 Replies

3. Shell Programming and Scripting

awk to find and replace Double quotes between pipes

Hello, Need a AWK command to find and replace Double Quotes in Pipe delimited files Actully its a CSV file converted to Pipe but the double quotes still exists. So i want to Get rid of them. Example Input 1|2|3|sadsad|"Abc Efg 3"""|dada Output 1|2|3|sadsad|Abc Efg 3"|dada Thanks... (5 Replies)
Discussion started by: krux_rap
5 Replies

4. UNIX for Dummies Questions & Answers

Help populating double quotes using awk

Want to populate double quotes for each filed using awk: Input: cat file.txt => "1-23-test_test1-test2" Required output : "1-23-test_test1-test2"|"#GT_properties_xyz" Was trying the below command on solaris 9 machine : awk -F"|" '{print $1"|""#GT_properties_xyz"}' file.txt ... (8 Replies)
Discussion started by: rajachandhok
8 Replies

5. UNIX for Dummies Questions & Answers

awk for inserting a variable containing single and double quotes

Hi i have to insert the below line into a specific line number of another file export MBR_CNT_PRCP_TYPE_CODES_DEL="'01','02','04','05','49','55','UNK'" I have passed the above line to a variable say ins_line. I have used below command to perform the insert awk 'NR==3{print "'"${ins_line}"'"}1'... (1 Reply)
Discussion started by: sathishteradata
1 Replies

6. Shell Programming and Scripting

Replace double double quotes using AWK/SED

Hi, I have data as "01/22/97-"aaaaaaaaaaaaaaaaa""aaa""aabbbbbbbbcccccc""zbcd""dddddddddeeeeeeeeefffffff" I want to remove only the Consequitive double quotes and not the one which occurs single. My O/P must be ... (2 Replies)
Discussion started by: Bhuvaneswari
2 Replies

7. Shell Programming and Scripting

awk - double quotes as record separator

How do I use double quotes as a record seperator in awk? (4 Replies)
Discussion started by: locoroco
4 Replies

8. Shell Programming and Scripting

How to alias an awk command with single and double quotes?

Hi, I am trying to write the following command as an alias in my .bashrc file. bjobs -u all | awk '{if (NR > 1) {username++;}}END{{print"\nJOBS BY USER:\n"} for (i in username) {print username,i;}{print"\n Total Jobs=",NR-1,"\n" }}' The command simply puts how many jobs each user is... (2 Replies)
Discussion started by: jacekmaciek
2 Replies

9. Shell Programming and Scripting

AWK removing away needed double quotes.

The below code is to convert csv file to pipe delimited. It replaces comma with pipe if it is not in double quotes; If comma is in double quotes it doesnot replace the comma with a pipe. The code works fine except it eat away the double quotes in the output file. BEGIN... (6 Replies)
Discussion started by: pinnacle
6 Replies

10. AIX

how to pass variables surrounded in double quotes to awk?

Hi, I'm making progress on this but hung up on one last detail. I'd like to use AWK to pass the system date and time(among other things) to the first line of a file. Here's what I have: BEGIN {TOTALPP = 0;FREEPP=0;USEDPP=0;print "LPAR NAME:",lpar,"DATE:",tdate } I call AWK with the... (4 Replies)
Discussion started by: cruiser
4 Replies
Login or Register to Ask a Question