Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

-v inFile[1]=file1.txt (use arrays with -v option)

-v inFile[1]=file1.txt (use arrays with -v option)

-v inFile[1]=file1.txt (use arrays with -v option)

hey guys, i just joined. Looks like cool community, thanks in advance.

I have an AWK script, "awk.prog", that I launch with a text file that looks like this:

awk -f awk.prog \
-v inFile1=file1.txt \
-v inFile2=file2.txt \
-v inFile3=file3.txt \

and then in the AWK script I have this:



function readFile(file)
  while(  (getline < file) > 0)

I would much prefer to do it somehow like this:

awk -f awk.prog \
-v inFile[1]=file1.txt \
-v inFile[2]=file2.txt \
-v inFile[3]=file3.txt \

and have the AWK script like this:


for(a=0; a<number_of_inFiles; a++)

Is there some way to do this? ...to pass the "file*.txt" filenames to the AWK program thru an array as I've done above? In actuality I am passing 50+ variables to my AWK program using the -v parameter, including ~25 input filenames. Thanks for any help.

RE: -v inFile[1]=file1.txt (use arrays with -v option)

Why not use awk -f awk.prog file1.txt file2.txt file3.txt, then they will be ready for you in the ARGV array.

(note that you will need to remove them from the ARGV array manually as you process them if you want to prevent awk from processing them as normal input files)

For example:


awk 'BEGIN { for (i=1;i<ARGC;i++) print ARGV[i] }' file1.txt file2.txt file3.txt 

tgmlify - code syntax highlighting for your tek-tips posts

RE: -v inFile[1]=file1.txt (use arrays with -v option)

Thanks! I'm pretty sure I tried this before but didn't know about "need to removed them from the ARGV array"... so it didn't work properly. But yeah, this seems to work:



  for(i=1; i<ARGC; i++)
    inFile[i] = ARGV[i];    # assign filenames to array
    delete ARGV[i];         # remove from ARGV


...and now I can access the input files thru their names stored in the array. Thanks again.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close