argc/ argv in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting argc/ argv in awk
# 1  
Old 02-02-2012
argc/ argv in awk

Hi guys,

i'm trying to solve this problem.
I have to run something like

Code:
cat file1.txt | awk -f script.awk 10

if i'm in the awk script, how can i take the parameter :10 ??Smilie

i try something like :
Code:
BEGIN{
var=argv[6]
}

{..}

END{..}

but obviously is not correct...

ty in advance
# 2  
Old 02-02-2012
Quote:
Originally Posted by heaven25
Hi guys,

i'm trying to solve this problem.
I have to run something like

Code:
cat file1.txt | awk -f script.awk 10

You get a useless use of cat award.

Fortunately you don't need to specify 'parameter 2' or anything. You can set variables directly, with the -v parameter.

Code:
awk -v VAR=10 -v ANOTHERVAR=42 -f script.awk file1.txt


Last edited by Corona688; 02-02-2012 at 11:17 AM..
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 02-02-2012
Quote:
Originally Posted by Corona688
Fortunately you don't need to specify 'parameter 2' or anything. You can set variables directly, with the -v parameter.

Code:
awk -v VAR=10 -v ANOTHERVAR=42 -f script.awk file1.txt

Ty Corona for your answer, but I know the -v option.

Unfortunately, i need to do like what i wrote before, sure it is very easy but i dont find the way..i'll try also
argv in upper case
Code:
var=ARGV[6]

but nothing..
# 4  
Old 02-02-2012
Quote:
Originally Posted by heaven25
Ty Corona for your answer, but I know the -v option.

Unfortunately, i need to do like what i wrote before, sure it is very easy but i dont find the way..
awk doesn't work that way. Giving awk an argument of '10' tells it to open a file named '10'. Positional parameters are arguments or filenames, period.

If ARGV[1] doesn't work in your awk, then your awk doesn't have ARGV.

Why do you need this? Couldn't you make a simple wrapper script to do this for you, converting shell arguments into a -v argument, instead of cramming arguments where they don't belong on awk's commandline?
# 5  
Old 02-02-2012
Quote:
Originally Posted by Corona688
Why do you need this? Couldn't you make a simple wrapper script to do this for you, converting shell arguments into a -v argument, instead of cramming arguments where they don't belong on awk's commandline?
This is a real good question. SmilieSmilie

I had a test yesterday and one of the question was: "Write an awk script that simulate the bash command tail, where the number of lines to print is give as:
cat file.txt | awk -f script.awk 10
"

wtf... Smilie
# 6  
Old 02-02-2012
That's a particularly awful way to do it since it will fail instantly, trying to open the file '10'. You'll have to cram everything into awk's BEGIN section, and exit at the end of it, so it doesn't try to open a file named 10. You'd do a while(getline<"/dev/stdin") loop. ARGV[3] would be the number of lines if your particular version of awk even has ARGV.

tail is not a "bash command", either. bash has some builtins and tail is not one of them. tail is an external utility that can be used with any shell or with no shell at all. It has nothing to do with bash whatsoever. This is an increasingly common and most vexing misconception because people keep trying to fix bash when the problem is not having installed the thing they're looking for in the first place Smilie

And of course that remains a useless use of cat. I've been wondering who keeps teaching this to people. Smilie
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

Help using argc/argv in assignment

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: First, create a "hello world" program that prints "Hello World". But NOW, instead use argc to verify that a... (9 Replies)
Discussion started by: miniviking10
9 Replies

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

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

4. Shell Programming and Scripting

ARGV and ARGC in bash 3 and bash 3.2

Hi Folks, I've prepared a shell script that takes action based on arguments and number of arguments..sample code like: ARGV=("$@") ARGC=("$#") case ${ARGV} in abc) if ; then ...... else printf "\nInvalid number of arguments, please check the inputs and... (2 Replies)
Discussion started by: SBC
2 Replies

5. Programming

Building an argc/argv style structure from a string (char*)

Hello All, First post. I've been struggling with the following: Given a char* string, I need to construct an "int argc, char *argv" style structure. What I'm struggling with most is handling escaped-whitespace and quotes. e.g. the string: char *s = "hello world 'my name is simon'... (10 Replies)
Discussion started by: cbarwise
10 Replies

6. Shell Programming and Scripting

print argv inside awk

mode=$1 psg telnetd | awk current=`date +%M`'{ printf ("mode is %s",mode) printf ("mode is %s",ARGV) }' 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... (3 Replies)
Discussion started by: Anteus
3 Replies

7. Programming

help for argv argc

Hi C experts, I have the following code for adding command line option for a program int main (argc, argv) int argc; char *argv; { char *mem_type; //memory type char *name; //name of the memory int addr; //address bits int data; ... (5 Replies)
Discussion started by: return_user
5 Replies

8. Programming

dbx debugger + argv[argc]

Is it possible to use the dbx debugger with the CL options for the executable ? Say you have created a executable called myfunc which can take string arguments at run-time. You run it like this ./myfunc Hello World where Hello and World are the string arguments My question is whether... (1 Reply)
Discussion started by: JamesGoh
1 Replies

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

10. Programming

Using argv argc

I searched on the forums. No advises. I am using a previous source code. I changed the main function main(int argc, char **argv) in a function misc(int argc, char **argv). How do you use the argc and argv parameters? This is how I am calling the function : char param; strcat(param,"wgrib ");... (4 Replies)
Discussion started by: Akeson Chihiro
4 Replies
Login or Register to Ask a Question