Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Merging files 1

Status
Not open for further replies.

hill007

Technical User
Mar 9, 2004
60
US
I have plenty of files and I want to make a single file from each of this files. To make it clear, here is an example:

file1.dat

A B C D E
1 2 3 4

file2.dat

E F A B C
2 A C D

Now I want to make it to:

A B C D E
1 2 3 4
E F A B C
2 A C D

However, after every 2nd row I want to enter the following lines:
1 1 1.000(10e12.4) 1

Thus the final output would look like this:
A B C D E
1 2 3 4
1 1 1.000(10e12.4) 1
E F A B C
2 A C D
1 1 1.000(10e12.4) 1

Any help is appreciated.
Thanks.
 
Try

awk -f hill007.awk file1 file2 ...

#hill007.awk
{print}
!(NR%2){print "1 1 1.000(10e12.4) 1"}

CaKiwi
 
Hi Catkiwi,

Instead of running the command {awk -f hill007.awk file1 file2 ...} from the awk prompt. How can I run the command if I write all the files in a text editor and then run in the awk prompt. I have total of 600 files.

Thanks.
 
Perhaps

awk -f hill007.awk file*

or

cat filelist | xargs awk -f hill007.awk

or maybe

xargs awk -f hill007.awk < filelist

CaKiwi
 
Hi Catkiwi,

I do all my awk in awk95. I do not have a UNIX machine. How can I run the multiple batch file in awk95?
Also you state earlier that I can run the command : cat filelist | xargs awk -f hill007.awk

I have files like file1.dat, e1.dat,e2.dat, file2.dat, etc.
Should I place all the files in the text editor and name it filelist? Or else how do you do it. Can you show me how you will put the filelist.
Thanks.
 
If you have a version of xargs then create a file in a text editor called filelist, say, containing the names of all the files you want to process and use

cat filelist | xargs awk -f hill007.awk

If you don't have xargs you can download the cygwin utiliies from


to get it.

I don't of know a way to get awk to take the names of its input files from a file

CaKiwi
 
Hi CatKiwi,

Thanks. My queston is how do I create the filelist file?
Should I call it filelist.dat or filelist.txt? Also, should the files be seperated by comma?

Thanks.
 
Create it in a text editor with one file name on each line. Call it anything you like.

CaKiwi
 
Hi CatKiwi,

It works. Thanks a lot for all the replies. Really appreciate.
 
I hope I don't confuse the issue, but you don't need xargs because awk95 can take the names of its input files from a file like this...
[tt]
C:\DATA>dir /b file?.dat e?.dat > filelist

C:\DATA>more <filelist
FILE1.DAT
file2.dat

C:\DATA>more <hill007.awk
BEGIN {
while (getline < "filelist") {
filename = $0
while (getline < filename) {
print $0
if (nr++ % 2)
print "1 1 1.000(10e12.4) 1"
}
close(filename)
}
}

C:\DATA>awk95 -f hill007.awk > result.txt

C:\DATA>more <result.txt
A B C D E
1 2 3 4
1 1 1.000(10e12.4) 1
E F A B C
2 A C D
1 1 1.000(10e12.4) 1[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top