awk - Print where value is in quotes


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk - Print where value is in quotes
# 1  
Old 01-22-2016
awk - Print where value is in quotes

Hi All,

I have input data like follows:

Code:
"1234"|"ABC"
"1234"|"CBA"
"1222"|"ZZZ"

I am trying to awk print all records where Col1 = "1234".

Below is the code I have so far:

Code:
Var1=1
Var2=1234

awk -F "|" "$ ${Var1} == "\"${Var2}\"" { print; }' inputfile

However when the AWK command is running, it is not enforcing that I want to find data which = "1234", it is just looking for data which = 1234 (i.e. without the double quotes).

Note -- The above command works fine from command line, however when running from a script, the "\ is being removed, therefore not enforcing that I want to find data with double quotes around.

Can anyone assist? Smilie

Last edited by Don Cragun; 01-22-2016 at 11:25 PM.. Reason: Add CODE tags again.
# 2  
Old 01-22-2016
Your quoting is wrong in several places. Try
Code:
awk -F "|" "\$${Var1} == \"\\\"${Var2}\\\"\"" file
"1234"|"ABC"
"1234"|"CBA"

which is not the recommended way to pass variables...
# 3  
Old 01-22-2016
Note that the recommended way to pass variables would be:
Code:
Var1=1
Var2=1234

awk -F "|" -v field="$Var1" -v value="$Var2" '$field == "\"" value "\""' inputfile

This User Gave Thanks to Don Cragun 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

Using quotes in awk

Hello, i had a problem running a script , and after investigation found its all to do with the quotes: cat file1 line1 val1 val2 line2 val1 val2 line3 val1 val2 awk 'BEGIN {RS="\n\n"; FS="\n";} {print $1 $2}' file1 This gives me the wrong output: (5 Replies)
Discussion started by: andy391791
5 Replies

2. Shell Programming and Scripting

awk print - fields separated with comma's need to ignore inbetween double quotes

I am trying to re-format a .csv file using awk. I have 6 fields in the .csv file. Some of the fields are enclosed in double quotes and contain comma's inside the quotes. awk is breaking this into multiple fields. Sample lines from the .csv file: Device Name,Personnel,Date,Solution... (1 Reply)
Discussion started by: jxrst
1 Replies

3. Shell Programming and Scripting

awk problem with quotes

can someone help me with this. i keep getting errors: var1="MaxClients" var2="java|could not.*problem found|panic() failure seen|aborting " awk 'NR>=1&&NR<=10 && /'${var1}'/ && !/'${var2}'/ {++c}c==3{o=$0 RS $0 RS $0; print o; c=0}' log when i run the above, i keep getting: awk:... (3 Replies)
Discussion started by: SkySmart
3 Replies

4. Shell Programming and Scripting

awk without quotes

I want to execute awk command without quotes. who am i | awk {'print $2'} above code should be something like: who am i | awk {print $2} Why such weird requirement? Im assigning command to a variable, hence i need to escape the quotes. e.g: x='who am i | awk {\'print $2\'}' I want... (11 Replies)
Discussion started by: Arun_Linux
11 Replies

5. Shell Programming and Scripting

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: from_instance_trans=`awk "/INPUT =\"$frm_inst\"/,/<\/TRANSFORMATION>/" $xml_object | grep -w "<TRANSFIELD" | awk... (9 Replies)
Discussion started by: dips_ag
9 Replies

6. Shell Programming and Scripting

quotes using awk

i want to print the statement below using awk,but i am unable to get the quotes ("22345",1,"Thank you"); How can i do this (5 Replies)
Discussion started by: tomjones
5 Replies

7. Shell Programming and Scripting

Using echo to print double quotes along with variable substitution

Hi, I am generating html code using cshell, but i am having one problem while printing double quotes, I need to write following code in file. where $var contains list of web address <a href="$var">$var</a> So i am using echo "<a href="$var">$var</a>" > file.html But with this " in... (4 Replies)
Discussion started by: sarbjit
4 Replies

8. Shell Programming and Scripting

print pattern within double quotes

Hi All, I have multiple lines in a file like:- "abc" def "ghi" jkl "mno" 1 I want to print in output:- abc/ghi/mno 1 How can I do this in perl? Regrds, Nilabh -----Post Update----- Additional info:- The last field of the file should be output as it is.In the above example 1 is... (6 Replies)
Discussion started by: nilabh_s
6 Replies

9. Shell Programming and Scripting

Dont want to print double-quotes

Hi, I have a perl script which generates an LDAP report in CSV Format. Issue: Some of attribute values has double-quotes as fed by users. I dont want to print those double-quotes while printing the attribute values. Please let us know how I can overcome this issue. Thank You. ... (2 Replies)
Discussion started by: gazalinawaz
2 Replies

10. UNIX for Dummies Questions & Answers

awk to print ' (single quotes)

I'm building a file with sql delete statements. I need to print the single quotes for the where clause. i.e. delete from Command where CommandName = 'SomeName'; I have the following in my script input=$(pwd)/Cmnd.csv i=0 while read line do if test $i -ge 1; then ... (2 Replies)
Discussion started by: orahi001
2 Replies
Login or Register to Ask a Question