• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Passing multiple filenames as argument

 
yuga devi
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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 [ ]
do
awk -F"|" '{print $2 $5 $6}' >> final.txt
done;


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
Pie
Posts: 11915
209
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18282
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic