Creating files using Awk

Thursday, January 24, 2013 , 0 Comments

I have 40,000 data files. Each file contains 1445 lines in single column. Now I need to rearrange the data in different order. The first number from each data file need to be collected and dumped in a new file (lets say abc1.dat). This particular file (abc1.dat) will contain 40,000 numbers.
And the second number from each data file need to be extracted and dumped in a another new file (let's say abc2.dat). This new file also will be containing 40,000 numbers. But only second numbers from each data file. At the end of this operation I should have 1445 files (abc1.dat, abc2.dat,...abc40000.dat) and each contains 40,000 data.

Below is a simple way to do it in awk:
awk '{print $1>"abc"FNR".txt"}' file*
file* above should refer to all 40k files.
Some flavors of unix does not support the above syntax,in such case go for:
awk '{x="abc"FNR".txt";print $1>x}' file*