File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Linux / UNIX and the fly likes Passing multiple filenames as argument Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "Passing multiple filenames as argument" Watch "Passing multiple filenames as argument" New topic

Passing multiple filenames as argument

yuga devi

Joined: Aug 07, 2008
Posts: 22

I have mutiple files in a directory[around 600]. I want to search for "|" in all the files and print output.

I have the follwing code.

while [ ]
awk -F"|" '{print $2 $5 $6}' >> final.txt

My question is:

How to pass the multiple file names to this while loop as an argument.

Note: All the files start with the following string : " output*"
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11778

If you only have 600 files, then you should be able to run your commands without needing any sort of loop. A simple:
should suffice.

Note that your code block does not check whether a line has the pipe delimiter in it - it just assumes that the delimiter is there and tries to parse the line accordingly. However your initial statement implies that some files and/or lines may not have the delimiter. So I started the awk statement by specifying that I am only interested in lines that contain a pipe symbol (the /\|/ specifier). As the pipe symbol has special meaning in the line matching regular expression (it normally signifies an OR statement), it needs to be escaped as shown.

If you really want to iterate over the files, you might want to use a for loop:

Or you could use the while loop:

The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17417

Unix/Linux command shells explode globbed filenames when they parse the command line. The list of filenames becomes part of the arguments passed to the application. Depending on the application, it may or may not enumerate that list and work through each file in turn. Check the AWK man page to see if it's one of them.

Failing that, you can use the "cat" command to concatenate the files into a single data stream, which you then pipe into AWK as a standard-input stream.

An IDE is no substitute for an Intelligent Developer.
I agree. Here's the link:
subject: Passing multiple filenames as argument
It's not a secret anymore!