Help with a simple script using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with a simple script using awk
# 1  
Old 10-21-2006
Tools Help with a simple script using awk

I need a hand with this simple script,
in Unix i have a variable called portal:
$ echo $portal
chu0
when i use awk this variable is not recognized. How can i make awk recognize and print the value of this variable, since the output is not shown i.e.

awk '
BEGIN {printf("%4s\t%14s\n", "Portal:", portal)}' listacdrs
output:Portal:
Thnaks.
# 2  
Old 10-21-2006
Once you enter awk, you cannot use the shell variables directly using $portal or some such. This is because awk has no access to shell variables.

You can pass variables to awk using the '-v' switch. Something like this:
Code:
nawk -v a=$a '{print a}' filename

The above code serves no purpose other than to show how a shell variable can be passed to awk. Note that the original Solaris awk does not support this, so with Solaris, you have to use nawk.
# 3  
Old 10-23-2006
You can also define the variable in the following way :
Code:
awk '{print a}' a=$a filename

The difference is that in that case the variable a is not define in the BEGIN clause.

The script
Code:
awk -v v_var='v_option' \
    'BEGIN  { printf("BEGIN v_var=%s f_var=%s\n", v_var, f_var)}
     FNR==1 { printf("FILENAME=%s v_var=%s f_var=%s\n", FILENAME, v_var, f_var)}
     END    { printf("END   v_var=%s f_var=%s\n", v_var, f_var)}' \
     f_var='files_1_2' file1 file2 \
     f_var='file_3'    file3

gives the output :
Code:
BEGIN v_var=v_option f_var=
FILENAME=file1 v_var=v_option f_var=files_1_2
FILENAME=file2 v_var=v_option f_var=files_1_2
FILENAME=file3 v_var=v_option f_var=file_3
END   v_var=v_option f_var=file_3


Jean-Pierre.
# 4  
Old 10-23-2006
Actually, u can use shell variables inside awk.
for e.g.

u have the file abc which has the folloing contents :

abc def ghi
jkl mno pqr

u need to print the 3rd column
create the shell variable
x=3

then

awk '{print $'$x'}' abc

u'll get the required output. what u need to do is to switch off quoting before $x so that the shell can evaluate it (n return 3) n then switch it on again.


so in ur case, simply, instead of portal, write '$portal'
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Simple awk use

Hi, I have a file such that: 40454,31,48,4,1304.967741935484,1 31708,25,48,4,1268.32,1 20900,64501,671,788,0.3240259840932699,0 20137,51358,834,743,0.3920908135051988,0 I want to replace the 6th column by "ones" if it is 1, and with "zeros" if it is 0. Thanks. (6 Replies)
Discussion started by: shekhar2010us
6 Replies

2. Shell Programming and Scripting

Simple awk script needed

but I'm stumped...please help I have a file like this....... 1000 1 34 1000 10 34 1000 11 35 1000 20 35 1000 21 36 1000 30 36 2000 1 34 2000 10 34 which I would like printed out as 40 lines 1000 1 34 1000 2 34 1000 3 34 1000 4 ... (2 Replies)
Discussion started by: garethsays
2 Replies

3. Shell Programming and Scripting

Help making simple perl or bash script to create a simple matrix

Hello all! This is my first post and I'm very new to programming. I would like help creating a simple perl or bash script that I will be using in my work as a junior bioinformatician. Essentially, I would like to take a tab-delimted or .csv text with 3 columns and write them to a "3D" matrix: ... (16 Replies)
Discussion started by: torchij
16 Replies

4. Shell Programming and Scripting

help with simple awk script

Hi, I just don't understand awk. I think I'm close here but can someone give me a hand to finish this little parsing routine ? the input file is formatted like this: District 2502110 Gsub 2384889 Gsub 1428180 District 2502220 Gsub 1466390 Gsub 1466389 Gsub 1466388 Gsub 1466386 Gsub... (4 Replies)
Discussion started by: fwellers
4 Replies

5. UNIX for Dummies Questions & Answers

Simple awk script for positional replacement in text?

I have a string of letters. (They happen to be DNA, not that it's relevant to the question.) For analysis purposes, I need to replace the information at some of the sites. I need to do this based on their position, not the information in that position. I also need to ignore differences at other... (10 Replies)
Discussion started by: JFS
10 Replies

6. Shell Programming and Scripting

Simple AWK script problem.

Hi all, I have set up a simple awk script to calculate the average of values that are printed out a number of times per second (the number of time the printing occurs varies). The data is of the format shown below: 1 4.43 1 3.65 1 2.45 2 7.65 2 8.23 2 5.65 3 4.65 3 6.21 .. .. 120... (4 Replies)
Discussion started by: omnomtac
4 Replies

7. Shell Programming and Scripting

simple awk

when I execute this awk stmt .. awk "/log_directory/ { print $5}" /opt/dba/oraadmin/tools/tmp_purge_op.log it's returning the whole line as .. IRMD118_LISTENER1 parameter "log_directory" set to /opt/oracle/10.2/network/log/ my expected output is : /opt/oracle/10.2/network/log what... (7 Replies)
Discussion started by: talashil
7 Replies

8. Shell Programming and Scripting

need help with simple awk/ksh script

I need help finding out why this script wont run. The chmod is okay, but i get an error saying that I need '&&' on line 5. (18 Replies)
Discussion started by: tefflox
18 Replies

9. Shell Programming and Scripting

simple awk script...

hi everyone, i have a script that I use regulary to look through my files and directories it works fine, but I would like to add a segment to the script to make the output more readable and userfriendly.. (i am not an expert on scripts so there is no comments or status exits as of yet.. )... (3 Replies)
Discussion started by: moxxx68
3 Replies

10. Shell Programming and Scripting

Simple awk script question

Hi, I'm a total beginner at awk and hope someone can advise what I have done wrong in the following script: I have a file which (to simplify things) may be something like this Fred Smith and Sue Brown Joe Jones and Jane Watts Sally Green and Jim O? Connor Freda O? Reiley and Pat O?... (2 Replies)
Discussion started by: Bab00shka
2 Replies
Login or Register to Ask a Question