Parse 2 or more files into one.

Hi,
I have a really simple question...I think. I want to be able to parse two or more files into one by reading the first record from each file into new file then go back to the first file and start reading the second record in from each file into new file and so on. I am new to using awk and am not a programmer. I have played around and researched a bit and have only managed to read in the first rcord from each file and then it stops. (by using nextfile) I need to loop some how and keep track of where I am. I have hacked around with programming enough to be dangerous! :slight_smile: I do understand for loops, while next etc.. Any suggestions are appreciated.

Thanks.

I don't know if I understood your requirement correctly, but try this:

awk '{print > "record"FNR}' file1 file2 file3 ...

Records will be placed into "record.." files.

that is almost what I want except I want all the records in one file but as broken out by the single files. So when I ran this code File one result had the first record from input files 1 2 and 3. then file 2 result had the second record from file 1 2 and 3 etc.. I want one result file containing all the results so the file looks like this. I hope that explains it a little better.

first file record 1
second file record 1
third file record 1
first file record 2
second file record 2
third file record 2
.......
......
.......

You can accomplish it by:

cat record* > outfile

I origianally had a space between record and FNR so my out put did not contain the prefix "record" file name was just 1 2 3 etc.. So I definately would need the literal record so I can use wildcard to concatenate as you show. I will have thousands of files. When I took the space out between record and FBNR this is the error I get. BTW thanks for all your input. If I am to much of a pain nt to worry at least you got me moving in the right direction.

C:\Users\Ray>gawk "{print > "record"FNR}" test.txt test3.txt text4.txt
gawk: (FILENAME=test.txt FNR=1) fatal: expression for `>' redirection has null string value

You can't use double quotes like this... If you are using double quotes to enclose AWK code, then inside that code you have to escape them:

gawk "{print > \"record\"FNR}" test.txt test3.txt text4.txt