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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Formatting a text file based on row numbers 2

Status
Not open for further replies.

mguha06

Technical User
Apr 20, 2006
28
0
0
US
Hello,

I am trying to rewrite a text files in a certain format:
where the files look like:

"Row","Column","Values"
1,86,0.002
1,87,0.01
1,88,1.01
2,86,0.2
2,87,0.4
2,88,1.0
3,86,0.3
3,87,0.2
3,88,5.0
4,86,6.2
4,87,0.3
4,88,0.2

I want to now write the file based on the row number as, only the "values"
field will be displaced, and would look like a matrix, eg., shown below:

0.002 0.01 1.01
0.2 0.4 1.0
0.3 0.2 5.0
6.2 0.3 0.2


I now have multiple of similar files, which need to be converted to a matrix
format. I have the file names in a sequence format, such as
f1.dat, f2.dat, f3.dat, etc.

Could someone please help me to write the files in a matrix format and also
help to automate, so that all the other files are written similarly,
instead of running awk everytime for each file.

Thanks for any help.
 
Hi feherke,

When I try the code, with 3 files, it do not provide any output, it just run

BEGIN {
for (i=1;i<=3;i++) print "awk95 -f test.awk f" i ".dat > output_dir/f" i ".dat"
}
 
Hi

Let's see.

- Save this code in a file, generator.awk :
Code:
BEGIN {
  for (i=1;i<=3;i++) print "awk95 -f test.awk f" i ".dat > output_dir\\f" i ".dat"
}
- Type this to the DOS prompt :
Code:
[blue]C:>[/blue] awk95 -f generator.awk > commands.bat
- The file commands.bat should contain this lines :
[tt]
awk95 -f test.awk f1.dat > output_dir\f1.dat
awk95 -f test.awk f2.dat > output_dir\f2.dat
awk95 -f test.awk f3.dat > output_dir\f3.dat
[/tt]
- Run the file commands.bat for the command line :
Code:
[blue]C:>[/blue] commands.bat

Feherke.
 
Hi Feherke,

As you suggested, I tried the process:, the generator.awk program is such:

BEGIN {
for (i=1;i<=3;i++) print "awk95 -f test.awk f" i ".dat > c:\temp\test\\f" i ".dat"
}

when I run in the DOS prompt,

C:\temp\test> awk95 -f generator.awk > commands.bat

it generates the command.bat file in the c:\temp\test file

after that, when I run the command.bat file from

c:\temp\test> command.bat

it gives access is denied.
 
Hi Feherke,

As suggested, I again re-ran, hwoever, this time the f1.dat, f2.dat, and f3.dat now turned empty. No adat in it.
 
Hopefully your current directory wasn't c:\temp\test !
 
Hi

I have a suspicion that there is something wrong with the locations. Please give us details about where are the files :
- commands.bat
- f*.dat
and of course, from which directory you run the command.bat file.

And just to be sure, paste in the content of the command.bat ( I hope there are still 3 lines only. )

Feherke.
 
Hi Feherke,

Congratulations! Its works now. It was my mistake, to mistakenly name the output file name as the input file.

I am giving you a star. Thanks for lots of help. Truely appreciate. This site is great!

Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top