Problem with variables in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem with variables in awk
# 1  
Old 05-21-2009
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


$ ./test.sh
12|aaaa
012|cccc
The correct output would be only "012|cccc", it looks a type problem (12 and 012 are the same number)

If I use a constant with " " in awk, it works good: $1 ==
"012" { print $0}

But I need use a variable, and if I write $1 == "var" { print $0} it doesn't work.

Someone can help me?


Thanks in advance,


Antonio.
# 2  
Old 05-21-2009
Your pattern should be
Code:
^012$

to get what you want, and your program action should be
Code:
$1 ~ var {print $0}


Last edited by yongitz; 05-21-2009 at 03:20 AM..
# 3  
Old 05-21-2009
Sorry but it doesn't work me. My problem is how put these pattern using a variable. If I put a constant I don't have any problem.
# 4  
Old 05-21-2009
Quote:
Originally Posted by apresa
Sorry but it doesn't work me. My problem is how put these pattern using a variable. If I put a constant I don't have any problem.

Did you try changing your prog action into this?

Code:
$1 ~ var {print $0}

# 5  
Old 05-21-2009
Code:
 
TEST>var1="012"
TEST>awk -F"|" -v var="^"$var1"$" '$1 ~ var {print $0}' file.txt
012|cccc

# 6  
Old 05-21-2009
Code:
$1 ~ var {print $0}

I bet you don't like to have to say -v var="^012$" and can't accept that -v var=".*" does match any line instead of the lines having exactly ".*" in the first field.
Then the following trick may work:

Code:
"z"$1 == "z"var {print $0}


Last edited by colemar; 05-21-2009 at 06:49 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing awk variables to bash variables

Trying to do so echo "111:222:333" |awk -F: '{system("export TESTO=" $2)}'But it doesn't work (2 Replies)
Discussion started by: urello
2 Replies

2. Shell Programming and Scripting

awk - Why can't value of awk variables be passed to external functions ?

I wrote a very simple script to understand how to call user-defined functions from within awk after reading this post. function my_func_local { echo "In func $1" } export -f my_func_local echo $1 | awk -F"/" '{for (k=1;k<=NF;k++) { if ($k == "a" ) { system("my_local_func $k") } else{... (19 Replies)
Discussion started by: sreyan32
19 Replies

3. Shell Programming and Scripting

Problem with variables in sed

Hello! I have a problem to insert variables with sed... And I can't find the solution. :confused: I would like to display one/few line(s) between 2 values. This line works well sed -n '/Dec 12 10:42/,/Dec 12 10:47/p' Thoses lines with variables doesn't work and I don't find the... (2 Replies)
Discussion started by: Castelior
2 Replies

4. Shell Programming and Scripting

Problem using iSQL and variables

I have searched the forums and other sites and cannot come with the applicable approach for what I am trying to do. I am trying to setup a cron job that executes this script. The script uses iSQL - which is connecting ok - to then query a field and then assign that field to a variable to do an If... (4 Replies)
Discussion started by: courtneyjh
4 Replies

5. 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

6. 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

7. Shell Programming and Scripting

Problem combining two variables into one

Hello, I have a problem combining two variables into one. I did the following: in my env variables i had set PATH_DESTINATION_1=/root/path_one PATH_DESTINATION_2=/root/path_two #!/usr/bin/ksh count=1 count_path=2 while do (3 Replies)
Discussion started by: Eraser
3 Replies

8. Shell Programming and Scripting

Sed with variables problem

I am writing a script with a sed call that needs to use a variable and still have quotations be present in the substitution. Example: sed -i "s/Replacable.\+$/Replaced="root@$VAR"/g" this outputs: where $VAR = place Replaced=root@place and i need Replaced="root@place" ... (2 Replies)
Discussion started by: mcdef
2 Replies

9. UNIX for Advanced & Expert Users

Problem in script when using variables

The AppName may be TCS/HCL/SCB. For All the above 3 i ill have exported TCSDIR/HCLDIR/SCBDIR in .profile with some path. i'm cnstruct the path in script and trying to cd $VARIABLE, it shows "not found". Any solution for this....? > AppName="TCS" > echo $AppName TCS >... (4 Replies)
Discussion started by: palsevlohit_123
4 Replies

10. Shell Programming and Scripting

problem with variables

hi all , i wanted to know if someone knows how can i use a variable inside another variable e.g. #!/bin/csh foreach test(1 2 3) set sta_$test = "2" ##now its the problem i want to echo the new var #$sta_$test , each time with anothe num ($test = 1... (2 Replies)
Discussion started by: udi
2 Replies
Login or Register to Ask a Question