print argv inside awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting print argv inside awk
# 1  
Old 06-18-2009
print argv inside awk

Code:
 
 mode=$1
  psg telnetd | awk current=`date +%M`'{
               printf ("mode is %s",mode)
               printf ("mode is %s",ARGV[1])
                }'


at command prompt when i run the script along with the argument i get only--
'mode is '
argument is not printed.(If the argument is printed outside the awk it gets printed!!) why is it so? . what can be done to make it print in the awk action block.
# 2  
Old 06-18-2009
You have to do like this...
e.g.
Code:
awk '{print variable}' variable=$argv[1] <filename>

# 3  
Old 06-18-2009
Use the -v option for shell variables:

Code:
awk -v mode=$1 'BEGIN{
  printf ("mode is %s\n",mode)
}'

Have a read of one of the awk tutorial here:

https://www.unix.com/answers-frequent...tutorials.html
# 4  
Old 06-18-2009
Quote:
Originally Posted by Franklin52
Use the -v option for shell variables:

Code:
awk -v mode=$1 'BEGIN{
  printf ("mode is %s\n",mode)
}'

Have a read of one of the awk tutorial here:

https://www.unix.com/answers-frequent...tutorials.html
yep..thanks..

from the code removed BEGIN and its working fine.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print hex Ip address in decimal format inside awk script

Hi to all, May someone help me with the following. The awk script below is part of a bigger awk script, and this part attempts to print an Ip address that is in hex format in decimal format. I'm trying with following code but I'm getting 0.0.0.0 and the correct answer is 192.168.140.100 ... (9 Replies)
Discussion started by: Ophiuchus
9 Replies

2. Shell Programming and Scripting

For loop inside awk to read and print contents of files

Hello, I have a set of files Xfile0001 - Xfile0021, and the content of this files (one at a time) needs to be printed between some line (lines start with word "Generated") that I am extracting from another file called file7.txt and all the output goes into output.txt. First I tried creating a for... (5 Replies)
Discussion started by: jaldo0805
5 Replies

3. UNIX for Advanced & Expert Users

O argv, argv, wherefore art thou argv?

All of my machines (various open source derivatives on x86 and amd64) store argv above the stack (at a higher memory address). I am curious to learn if any systems store argv below the stack (at a lower memory address). I am particularly interested in proprietary Unices, such as Solaris, HP-UX,... (9 Replies)
Discussion started by: alister
9 Replies

4. Shell Programming and Scripting

argc/ argv in awk

Hi guys, i'm trying to solve this problem. I have to run something like cat file1.txt | awk -f script.awk 10 if i'm in the awk script, how can i take the parameter :10 ??:wall: i try something like : BEGIN{ var=argv } {..} END{..} but obviously is not correct... (5 Replies)
Discussion started by: heaven25
5 Replies

5. Shell Programming and Scripting

Matching options from ARGV in awk

I have written this code in an awk script. BEGIN { print "ARGV", ARGV if ( match(ARGV,/-u/) || match(ARGV,/--usg/) ) { print "MATCH -u:",match(ARGV,/-u/), RSTART, RLENGTH print "MATCH --usg:",match(ARGV,/--usg/), RSTART, RLENGTH usage() exit(1) } } I want... (7 Replies)
Discussion started by: kristinu
7 Replies

6. Shell Programming and Scripting

Sysdate inside awk print statement

Hi, I am using awk statement to extract data from a file and write a new file with certain columns rearranged and few hard coded values added to new file. Now i need to add a column with sysdate. can i do that inside the awk print statement? Now: nawk ' /^3/ BEGIN {FS=","}... (2 Replies)
Discussion started by: selvankj
2 Replies

7. Shell Programming and Scripting

Can't print multiple lines inside awk :(

Hi Friends, I have small issue with following code snippet. I am trying call one function inside awk in which the function inturn will echo few lines. However when i ran script its throwing an error saying "nawk: syntax error at source line 1". #!/bin/sh eval input=$@ while read... (3 Replies)
Discussion started by: Shahul
3 Replies

8. Shell Programming and Scripting

Print filename inside loop

Im want to print filename inside loop .. the code im using :- Filename_1=abc_20090623_2.csv.lk Filename_2=def_20090623_2.csv.lk i want to extract filename till .csv eg Filename_1=abc_20090623_2 Filename_2=def_20090623_2 How can i do this inside the for loop ... (3 Replies)
Discussion started by: r_t_1601
3 Replies

9. Shell Programming and Scripting

variable inside awk '{print $c}'

i'm trying to do this (in bash darwin); echo "give me some words: " read a c=2 # this is get by other ways echo $a | awk '{print $c}' # i want to print the column given # by de $c variable if there is someone understand what i pretend... (3 Replies)
Discussion started by: Tártaro
3 Replies

10. UNIX for Dummies Questions & Answers

argv command in awk

hello does anyone knows how can i reach a parameter in awk command line for example p1 f 4 p1 is an awk script. 4 is parameter i want to work with the second parameter i know it has something to do with argv command i just dont know the syntax. please help. (1 Reply)
Discussion started by: emil2006
1 Replies
Login or Register to Ask a Question