Re: Input, output troubles me ...
- To: mathgroup at smc.vnet.net
- Subject: [mg70042] Re: Input, output troubles me ...
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Sun, 1 Oct 2006 04:08:43 -0400 (EDT)
- Organization: The Open University, Milton Keynes, UK
- References: <efldl4$d6s$1@smc.vnet.net>
luigirmailbox-mathematica at yahoo.it wrote: > Dear Friends > > I have been using Mathematica for text file processing for a long time. > I found it's a valuable tool for text complex manipulations. > > The strategy I was used to apply is well condensed in this childish > example: > > --------------------------------- > (*loading the content*) > dummy = Import["c:\\myDir\\inputFile.txt", "Lines"]; > (*processing it*) > dummy = Select[dummy, (# != "a") &]; > (*exporting it*) > Export["c:\\myDir\\outpuFile.txt", dummy, "Lines"]; > --------------------------------- > > Now I have to face huge file (up to 5,6 Gb sized) and the previous > technique mostly crashes my systems. > > I would like to process a single line, rather than the whole load, > doing something similar what a classical programmig language does: > > --------------------------------- > Open "c:\\myDir\\inputFile.txt" for Input As #1 > Open "c:\\myDir\\outputFile.txt" for Input As #2 > > Do While Not Eof(#1) > Line Input #1, dummy$ > If dummy$ <> "a" Then Print #1, dummy$ > Loop > > Close #1 > Close #2 > --------------------------------- > > I tried this (and many others!) trick: > > --------------------------------- > stream1 = OpenRead["c:\\myDir\\inputFile.txt"]; > stream2 = OpenWrite["c:\\myDir\\outputFile.txt"]; > While[ > dummy != EndOfFile, > dummy = Read[stream1, "String"]; > If[dummy != "a", Write[stream2, dummy]]; > ] > Close[stream1]; > Close[stream2]; > --------------------------------- > > But it doesn't work: when inputFile is > > a > b > c > d > > outputFile is void, not > > b > c > d > > as expected. > > Plaese can someone help me? > > Thanks for your attention. > Goobye. > Two things: . The variable dummy must be initialize to something before entering the loop. . Use UnsameQ (shorthand =!= ) rather than not equal to test the end of file. stream1 = OpenRead["c:\\temp\\inputFile.txt"]; stream2 = OpenWrite["c:\\temp\\outputFile.txt"]; dummy = Read[stream1, "String"]; While[dummy =!= EndOfFile, If[dummy != "a", Write[stream2, dummy]]; dummy = Read[stream1, "String"]; ] Close[stream1]; Close[stream2]; Regards, Jean-Marc