awk help (external variable)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk help (external variable)
# 1  
Old 02-28-2008
awk help (external variable)

i need help with an awk question. i am looking to have an external variable be defined outside of awk but used in awk. so if we have fields $1, $2, $3 so on and so forth, i would like to be able to dictate what field is being printed by something like $[i]. so if i had a counter called test, make it 3 so the awk line would read `awk -F, '{ if ($[test] == "Annual") print $2}'. thus printing field 2 if field 3 equals Annual.

i hope i've somehow explained what i'm looking for. any thoughts, ideas, suggestions would be awesome. note that i am calling awk from within a function. call from case looks like this:

Code:
"02") get_jan_info "$(echo ${userpri[@]})" "$(echo ${usersec[@]})";;


code is here:
Code:
get_jan_info() {
        for pri_user in `awk -F, '{ if ($3 == "Annual") print $2;}' review.csv`
        do
               userpri[$num]=$pri_user  
               let "num = $num + 1"
        done

... #more that isn't relevant. 
}

thanks in advance.
# 2  
Old 02-28-2008
Yes, it is possible to do what you want to do. Here is a simple example
which demonstrates how to do it

Code:
#!/usr/bin/ksh

tmp=file.$$

cat <<EOT >$tmp
Annual 222 333 444
EOT

awk 'BEGIN { test=1; } { if ($test == "Annual") print $2 }' $tmp

rm $tmp

exit 0

# 3  
Old 02-29-2008
thanks for the quick reply. i'll go check that out right now.
# 4  
Old 02-29-2008
ok. however, i need test to be declare and defined outside of awk. such as:

Code:
get_jan_info() {
  test=3;
  for pri_user in `awk -F, 'BEGIN {if ($test == "Annual") print $2;}' review.csv`

# 5  
Old 02-29-2008
actually, its very similar to what this guy is trying to do.

pass variable to awk - The UNIX Forums
# 6  
Old 02-29-2008
It's not clear what you're trying to achieve but with awk you can't print a field of the input in the BEGIN section, only actions you want to perform before the first line of input is read.

Regards
# 7  
Old 02-29-2008
OK, I have modified my previous example to show you how to do what you now want to do

Code:
#!/usr/bin/ksh

tmp=file.$$

cat <<EOT >$tmp
Annual 222 333 444
EOT

awk -v tt=1 'BEGIN { test=tt; } { if ($test == "Annual") print $2 }' $tmp

rm $tmp

exit 0

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk - saving results of external script to variable.

So, I've been playing with speeding up some analysis we do by using multiple threads of awk (actually, mawk, but code-compatible as far as I use it) on multiple CPU cores. So, I have a big data file and I have several copies of exactly the same processor script, written in mawk. I also have a... (8 Replies)
Discussion started by: treesloth
8 Replies

2. Shell Programming and Scripting

Passing external variable to awk

Hi, I am trying to write a bash script in which I need to pass a external variable to the awk program. I tired using -v but it not accepting the value. Here is my sample code. #!/usr/bin/bash ###################################################################################### ####... (5 Replies)
Discussion started by: jpkumar10
5 Replies

3. Shell Programming and Scripting

Help needed with awk external variable

I'm trying to get the universities result data into different file, where the $9 contains unversity field and field7,4 & 5 contains the keys to sort the students by marks. How to use uni variable to match against $9 inside awk? c=0 for uni in `cat /tmp/global_rank| awk -F ',' '{print... (1 Reply)
Discussion started by: InduInduIndu
1 Replies

4. Shell Programming and Scripting

How can we assign value to an array variable from an external file?

is it possible to assign value to an array variable from an external file?? if yes then how?? I am using below code but its not working. #!bin/bash myarray < file_name echo ${mayarray} (6 Replies)
Discussion started by: mukulverma2408
6 Replies

5. Shell Programming and Scripting

[awk] - how to insert an external variable

I want to incorporate the variable in the for statement as a column of my processed file. In the INCORRECT example below, it is $i which corresponds to the i in my for loop: for i in x86_64 i686; do awk '{ print $1" "$4" "$5" "$i }'awk $file-$i > processed-$i.log doneThanks! (3 Replies)
Discussion started by: graysky
3 Replies

6. Shell Programming and Scripting

using an awk internal variable as parameter for an external array

Hello, I am running a bash script under linux which first defines an CA-array like j=0 num1=120.00 num2=10.00 until do CA='echo $num1 + $j*$num2' j=$ done within the later awk section of this same script I want to read data from a file. If the value of the second column is... (3 Replies)
Discussion started by: MotAah
3 Replies

7. Shell Programming and Scripting

Help nawk change external variable

Hello, I have external variable rownumber, I am processing files within a loop and I would like to keep incrementing rownumber. What is happening is inside nawk section it passes rownumber but it never gets updated. I want to update rownumber inside the nawk, I would appreciate your help ... (6 Replies)
Discussion started by: srattani
6 Replies

8. Shell Programming and Scripting

BASH - Reference external variable name dynamically

Hi there, I have included an external properties file into my BASH script via the 'source' command. I am attempting to dynamically assign a variable in the BASH script, that references the variable name within the external properties file i.e. #!/bin/bash pth=${0%/*} source... (3 Replies)
Discussion started by: mjwoodford
3 Replies

9. Shell Programming and Scripting

Insert external variable in a AWK pattern

Dear all, ┐How can i insert a variable in a AWK pattern? I have almost succeeded in solving a puzzle with AWK but now i want to make a script. Let me explain. cat file.txt | awk 'BEGIN {RS="\\n\\n"} /tux/ { print "\n"$0 }' I know that this command makes right what i want to do, but mi... (8 Replies)
Discussion started by: antuan
8 Replies

10. Shell Programming and Scripting

If statement in awk with external variable

So I have a if statement inside an awk to check if $2 of a awk equals a specific IP but the test fails. So here is what I have. # !/bin/sh echo "Enter client ID" read ID echo "Enter month (01, 02, 03)" read month echo "Enter day (03, 15)" read day echo "Enter Year (07, 08)" read... (6 Replies)
Discussion started by: doublejz
6 Replies
Login or Register to Ask a Question

Featured Tech Videos