awk - how to read multiple files?
Hi, is there a ways to read multiple files in a single awk command?
For example: Code:
awk -f file1 file2 file3 *I'm new to Unix enviroment* |
do you mean this: awk -f file1 -f file2 -f file3
or this: cat file2 file3 | awk -f file1 ? |
Quote:
Code:
awk -f awk_script file1 file2 file3 |
is this wrong for you?
Code:
cat file[123] | awk -f awk_script Code:
for f in file[123] ; do awk -f awk_script "$f" ; done |
Quote:
The blue part tells awk to get its commands from a file called awk_script. The programming logic ("commands") can be found inside it. I'm not sure if and why you need the FNR variable. This variable holds the line number it processes (if multiple input files are used it starts with 1 again if it starts with a new input file). Here's a very basic example: Content of the input files: Code:
$ cat file1 Code:
$ cat awk_script Code:
$ awk -f awk_script file1 file2 file3 |
Yes. Very much possible.
Syntax is is awk '{commands ..}' file1 file2 .. FNR combined with NR and FILENAME are the way to go. FNR tells the current record number of all files and NR of the current file. Eg. assume that file1 has 2000 records and file2 has 1921 and file 3 has 4000. So long as FNR==NR, you are on the first file. FILENAME tells the name of the current file. So you can decide what to do based on this. Actually the usage is not uncommon. First file could have a different format/content and so on. So you need to differentiate between the files. OK |
IMO, FNR is best used with only two files, and using it gets more complex when you get into three or more. It's also, I believe, a gawk/nawk extension and isn't available in traditional awk.
A more robust solution would probably require testing the ARGC/ARGV values that keep track of the input arguments. http://www.gnu.org/software/gawk/man...o_002dset.html http://www.gnu.org/software/gawk/man...C-and-ARGV.htm Now, as for your specific question, we really need more than some vaguely-worded half-explanations about what you want to do. Please explain your exact goals in more detail, along with some examples of both input and output, and perhaps a bit of the overall coding context, so that we can understand you better. The exact methods to use often depend very much on the particulars of the coding situation, and without proper background knowledge we can only give you guesses and general suggestions. |
Quote:
File1 Code:
Red Apple 8 3 Code:
Orange 5 5 Code:
Tomatoes 5 4 |
As you might have noticed we are willing to help you, but.....
You still haven't told us what it is that needs to be done with the content of these files. - Add the similar entries (quantities and/or price) per file? - Add the similar entries (quantities and/or price) for all files? - Calculate the cost for each entry per file? - Calculate the total cost for each file? - Calculate the total cost for all the file? - ??? - ??? Please tell us what needs to be done so we can point you in the correct direction (which might or might not need to use of FNR). |
Quote:
What I'm trying to do is calculate the total cost of each fruit/vegetable in every files and output in a sorted format depending on the total. For example, the total cost of Red Apple in each file is 8*3 + 10*4 + 3*3=81, Tomatoes is 103, Orange is 80. In the output it will be like this Code:
Tomatoes 103 |
I do believe I understand what it is you are after, I do need some more info:
- Are the given examples correct: file3 seems to be missing some information. - Are all fields separated by spaces. One fruit (Red Apple) has a space in its name, which makes this more challenging. - Your math is also not correct 8*3 + 10*4 + 3*3=81 73 Please be careful with the examples posted, it needs to reflect the correct input used. EDIT: You mention that the output needs to be sorted: On what field? The name or the highest/lowest total price? |
Quote:
file3: Code:
Tomatoes 5 4 |
The Red Apple entry makes it more challenging due to the extra field it creates (the Red Apple lines has 4 fields and the rest have 3 fields). But it is possible, here's one way:
Code:
$ cat awk_script The green line looks for lines that do not (the !) start with Red. If this is the case then the name of the fruit ($1) is stored as the unique index and fields 2 and 3 are multiplied and added to the value present. The blue line looks for entries that start with Red, but now the index consists of 2 fields ($1 = Red and $2 = Apple). The amount and price are now $3 and $4. Once all the files are processed, the brown part is executed. This prints all the entries in the array (index and value). A sample run with the 3 files you posted as input: Code:
$ awk -f awk_script file1 file2 file3 BTW: You did not answer the question about sorting the output. If that is needed use the sort command. Sorting in (g)awk is possible but cumbersome. |
Quote:
|
All times are GMT -5. The time now is 03:38 PM. |