How do I pass a variable to awk?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How do I pass a variable to awk?
# 8  
Old 04-03-2007
Quote:
Originally Posted by eja
Yes that would help. you can tell I am a newbie. Works great. I changed it slightly but it works.

awk -F'|' '
NR==FNR { arr[$1]=1; next }
arr[$1] !=1' $HOME/$WORK/$NewFile $HOME/$COMPARE/$OldFile > $HOME/$WORK/$NewFile.del

The above compares yesterdays file to todays and find deleted records.

Thanks very much for the help.
First, get used to using vB Codes when quoting either code or other's posts - it really makes it easier to read and would improve your chances of people pay attention to you posts.

Secondly, you really don't need to
Code:
arr[$1]=1

OR
Code:
arr[$1] != 1

both of the above is redundant: you don't need to assign '1' reading the first file as you're just building the hash the values of the hash don't matter as long as there's a key/index in a hash.

and you don't need to 'arr[$1] != 1' as the sheer presence of a key in a hash is enough. you re-code the second piece for ease of reading/understanding as:
Code:
$1 in arr

# 9  
Old 04-03-2007
I did try it w/o =1 or !=1. When I did I got no results. As soon as I put the =1 and !=1. It displayed results.
# 10  
Old 04-03-2007
one awkward awk you've got there - must be M$ or something.....
# 11  
Old 04-03-2007
COuld be the version of awk. More likely me and something I am doing. But it does what i need for now. And it is all thanks to your help.

Again, thank you for taking the time to help.
# 12  
Old 04-03-2007
Dear vgersh99,

One more followup question if you do not mind? I may need to compare other files where the static data is not just in the first field arr[$1].

It could be the 1st and 3rd arr[$1,$3] or 2nd and 5th etc..

Is there a way to write this so I have one script and I can pass the value to it? So somehting like: arr[$NUMS]. Where $NUMS may equal $1 or $1,$3?
# 13  
Old 04-03-2007
NOTE: fields are defined on the command line by the value of a variable 'fields' - list of fields separated by a ','

awk -v flds='5,6,7' -f eja.awk FILE1 FILE2

eja.awk:
Code:
BEGIN {
   SEP_list=","
   n=split(flds, fldsA, SEP_list)
}
{
   idx=""
   for(i=1; i<=n; i++) idx=(i==1) ? $(fldsA[i]) : idx SUBSEP $(fldsA[i])
   #printf("[%f]: idx->[%s]\n", FNR, idx)
}
NR==FNR { arr[idx]=1; next }
arr[idx] !=1


Last edited by vgersh99; 04-03-2007 at 07:01 PM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Pass shell Variable to awk

Hello, May i please know how do i pass the shell variable to awk expression in the below script. It is returning null #!/bin/bash UNINUM=720922 UNINUM_DESC=`awk -F'|' -v UNINUM=$2 '/UNINUM/ {print $4}' datafile` echo $UNINUM_DESC datafile 4|First|720194|asdasdad 4|First|720735|asdasdsa... (8 Replies)
Discussion started by: Ariean
8 Replies

2. Shell Programming and Scripting

pass an awk variable to kill

Does anyone know of a way to do something similar to this with awk and kill? I want to create the variable in awk and pass that variable to kill. ps -ef | grep -i chromium | awk '{$2=x}' | kill -9 $x 2>/dev/null (9 Replies)
Discussion started by: cokedude
9 Replies

3. UNIX for Dummies Questions & Answers

How to pass a variable from shell to awk

I know this topic has been dealt with previously, but the solutions I've seen don't work for me apparently. I need to pass a variable defined in the shell to one in awk: $ echo $var1 3 $ cat aaa aaa 1 bbb 2 ccc 3 ddd 4 eee 5I've tried this, without success: $ awk... (2 Replies)
Discussion started by: metaltree
2 Replies

4. Shell Programming and Scripting

ksh variable pass to awk

I'm trying to store the response from a nawk command inside of a ksh script. The command is: text=$(nawk -F: '$1 ~ /${imgArray}/ {print $2}' ${etcDir}/captions.txt) From what I can tell, the imgArray variable is not being expanding when it is inside the single quote ('). Is there something I... (4 Replies)
Discussion started by: meman1188
4 Replies

5. Shell Programming and Scripting

How to pass a Awk variable to another script

Read parameter from a text file with one line which stored the date value like 20080831; below is the awk command I used gawk -F, "{getline RunDate;print $RunDate" text file When print $RunDate, it display 20080831 Would like to pass this variable to another script to use but not... (6 Replies)
Discussion started by: cbauw
6 Replies

6. Shell Programming and Scripting

Is it possible to pass variable from awk to shell script

Hello experts, can I return a value from gawk to a shell script ? My script as follows, #Here I want the num value to shell script so that I can use later gawk ' { split($0,num,","); print num }' gawk -v no=$number '{print no}' file1 ... (3 Replies)
Discussion started by: user_prady
3 Replies

7. UNIX for Dummies Questions & Answers

How to pass Shell script variable to awk

Hi, I have a shell script with an ambedded awk script. i need to pass a script variable to the awk script. Please help. Thanks in advance Himani (3 Replies)
Discussion started by: HIMANI
3 Replies

8. Shell Programming and Scripting

Pass script variable value to AWK

HI all, some more mistery about AWK, I hope you can help me out: 1) I have a normal ksh script and sometime I call awk command. I set some variables in the script and I would like to use them up within AWK as well. Unfortunately AWK seems to forget all the variable values outside of its own... (1 Reply)
Discussion started by: BearCheese
1 Replies

9. Shell Programming and Scripting

How to pass a variable to Awk ?

I am trying to pass 2 shell variable's ("START" and "END") define earlier in the script to this awk statement, but i can't seem to pass it on. PLs help. set START = xxxx set END = yyyy set selected_file = `awk '/$START/,/$END/' filename` (24 Replies)
Discussion started by: Raynon
24 Replies

10. UNIX for Dummies Questions & Answers

pass variable to awk

i would like to pass a variable to awk wherein the variable comes from external loop. i tried this... let x=0 until test $x -eq 32 do cat file | awk '{ print $1 , "Number" , $($x) }' >> output done thanks, (4 Replies)
Discussion started by: inquirer
4 Replies
Login or Register to Ask a Question