FOR loop with multiple files as input and awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting FOR loop with multiple files as input and awk
# 1  
Old 03-11-2013
FOR loop with multiple files as input and awk

Hi all ,

i want to pass multiple files as input to a for loop

HTML Code:
for i in file1 file2 file3
do
some awk action < $i >> $i.out
done
but im getting error in that for loop is the way i use to pass files to awk using for correct and
2.we can directly pass multiple files to awk as input but how to out put results to different output files
# 2  
Old 03-11-2013
Create output file names using FILENAME which is the name of the file awk is currently reading.
Code:
awk ' { F = FILENAME ".out"; print $0 > F } ' file1 file2 file3

This User Gave Thanks to Yoda For This Post:
# 3  
Old 03-11-2013
HI Bipanajith ,

Thanks buddy

Thanks for ur reply we need to use it in the END { } or any where will do .
# 4  
Old 03-11-2013
Show your awk program, or we'll never guess right.
# 5  
Old 03-11-2013
Hi,

this is my awk part (original which i used did not modify as suggested till now)

HTML Code:
for i in file_1 file_2 file_3

do

awk -F, ' BEGIN {
              print "<table border=1>"
} NR == 1 {
print "<tr>"
for(i=1;i<=NF;i++){
print "<th><b><font face=\"verdana\"><font size=\"2\"><font color=\"Brown\">" $i"</font></b></th>"
}
print "</tr>"
} NR > 1 {
print "<tr>"
for(i=1;i<=NF;i++) {
print "<td><b><font face=\"verdana\"><font size=\"2\"><font color=\"Black\">" $i"</font></b></td>"
}
print "</tr>"
} END {
                print "</table>"
} ' $i >> $i.html

done
so i can use the mutiple file names in the at the end portion as output files as suggested by bipanajith i.e using filename variable of awk is it or is there any better way

Last edited by zozoo; 03-11-2013 at 05:07 PM..
# 6  
Old 03-11-2013
Sure. Whenever you do print, do print ... > FILENAME ".html"

And instead of NR use FNR
This User Gave Thanks to Corona688 For This Post:
# 7  
Old 03-14-2013
Hi corona,
Code:
when i use FNR instead of NR in the above code and redirecting the print as suggested im getting output is last file.html iwth only </table> in it can u tell me where im i wrong

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to use a loop for multiple files in a folder to run awk command?

Dear folks I have two data set which there names are "final.map" and "1.geno" and look like this structures: final.map: gi|358485511|ref|NC_006088.3| 2044 gi|358485511|ref|NC_006088.3| 2048 gi|358485511|ref|NC_006088.3| 2187 gi|358485511|ref|NC_006088.3| 17654 ... (2 Replies)
Discussion started by: sajmar
2 Replies

2. UNIX for Dummies Questions & Answers

Multiple string input in a awk

Hi everybody, I just start my learning about Linux. So, if you can help me, it would be very good ! I have already find the possibility to find the position of a character regular expression in a line with the help of awk : test.txt is : AAAAAGHIJKLAjKMEFJKLjklABCDJkLEFGHIJKL My script is... (2 Replies)
Discussion started by: thewizarde6
2 Replies

3. Shell Programming and Scripting

[Solved] Multiple input files and output files

Hi, I have many test*.ft1 files to which I want to read as input for a script called pipe2txt.tcl and print the output in each separate file. For example, pipe2txt.tcl < test001.ft1 > test001.txt How can I read many files in this maner? thank you very much, Best, Pahuja (5 Replies)
Discussion started by: Pahuja
5 Replies

4. Shell Programming and Scripting

Detail on For loop for multiple file input and bash variable usage

Dear mentors, I just need little explanation regarding for loop to give input to awk script for file in `ls *.txt |sort -t"_" -k2n,2`; do awk script $file done which sorts file in order, and will input one after another file in order to awk script suppose if I have to input 2 or... (4 Replies)
Discussion started by: Akshay Hegde
4 Replies

5. Shell Programming and Scripting

awk, multiple files input and multiple files output

Hi! I'm new in awk and I need some help. I have a folder with a lot of files and I need that awk do something in each file and print a new file with the output. The input file name should be modified when I print the outpu files. Thanks in advance for help! :-) ciao (5 Replies)
Discussion started by: gabrysfe
5 Replies

6. Shell Programming and Scripting

Loop for row-wise averaging of multiple files using awk

Hello all, I need to compute a row-wise average of files with a single column based on the pattern of the filenames. I really appreciate any help on this. it would just be very difficult to do them manually as the rows are mounting to 100,000 lines. the filenames are as below with convention as... (2 Replies)
Discussion started by: ida1215
2 Replies

7. UNIX for Dummies Questions & Answers

Writing a loop to process multiple input files by a shell script

I have multiple input files that I want to manipulate using a shell script. The files are called 250.1 through 250.1000 but I only want the script to manipulate 250.300 through 250.1000. Before I was using the following script to manipulate the text files: for i in 250.*; do || awk... (4 Replies)
Discussion started by: evelibertine
4 Replies

8. UNIX for Dummies Questions & Answers

Writing a for loop that processes multiple input files

I would like to write a for loop that does the following: I have a file called X.txt and other files called 1.txt,2.txt, .....,1000.txt. I want to substitute the 6th column of the file X.txt with 1.txt and store the output as X.1. Then I want to do the same with X.txt and 2.txt and store the... (1 Reply)
Discussion started by: evelibertine
1 Replies

9. Shell Programming and Scripting

how to give multiple csv files as input in awk

Hi All, I am new to shell scripting..My problem is i want to give multiple csv files as input to awk script and process the data into one file.. My input file is File1 File2 File3 Product Location Period SalesPrice A x 8/11/2010 ... (7 Replies)
Discussion started by: kvth
7 Replies

10. Shell Programming and Scripting

Splitting input files into multiple files through AWK command

Hi, I needs to split *.txt files from single directory depends on the some mutltiple input values. i have wrote the code like below for file in *.txt do grep -i -h "value1|value2" $file > $file; done. My requirment is more input values needs to be given in grep; let us say 50... (3 Replies)
Discussion started by: arund_01
3 Replies
Login or Register to Ask a Question