INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

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.

Jobs

-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)

(OP)
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 \
.
.
.
...etc...

and then in the AWK script I have this:

CODE

readFile(inFile1);
readFile(inFile2);
readFile(inFile3);

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 \
...etc....

and have the AWK script like this:

CODE

for(a=0; a<number_of_inFiles; a++)
{
  readFile(inFile[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:

CODE

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

Annihilannic
tgmlify - code syntax highlighting for your tek-tips posts

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

(OP)
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:


CODE

BEGIN {

  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!

Resources

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