cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Browse apps to extend the software in the new JMP Marketplace
Choose Language Hide Translation Bar
thickey1
Level III

How do I read in a text file line by line.

It is possible to read in a JSL Script line by line (actually JMP makes a list with each line as a list item) using the code below.

 

myFile= open("$PATH\testScript.jsl");
myCfLines= myFile[scriptBox(1)] << getLines();

 

However, when I try to do this with a text file which has a '*.txt' extension JMP automatically opens this as a data table and so I cannot use this method to get the lines (as it's  not a script box!)

 

Is there another method? I dont want to use the loadTextFile() Function as this scrapes the whold file into a string. Ideally I'd like to open the file, read in each line one at a time and do a regexp to pull out the data I need!

 

1 ACCEPTED SOLUTION

Accepted Solutions
Jeff_Perkinson
Community Manager Community Manager

Re: How do I read in a text file line by line.

JSL doesn't currently have tools to open a file and read it one line at a time.

 

However, if you do use Load Text File() then you can use the Words() function to get in the individual lines into a list.

 

bctxt=load text file ("$SAMPLE_IMPORT_DATA\Bigclass.txt");

lines=words(bctxt,"\!n");
-Jeff

View solution in original post

6 REPLIES 6
ian_jmp
Level X

Re: How do I read in a text file line by line.

As you see, JMP uses the file extensions to try to do an appropriate thing.

Not sure I completely understand your use case, but if a '.jsl' extension gives the behaviour you want, you could consider using 'RenameFile()':

 

renameFile("$DESKTOP/Test.txt", "Test.jsl");
myFile = open("$DESKTOP/Test.jsl");
myCfLines = myFile[scriptBox(1)] << getLines();
renameFile("$DESKTOP/Test.jsl", "Test.txt");

 

Craige_Hales
Super User

Re: How do I read in a text file line by line.

There is a "script" option you can give to open to make this work:

open("$sample_data/../Import Data/bigclass.txt","script")

Now, instead of opening as "text" (which is what the .txt extension is defauilting to) and making a data table, it will open into a script window. Yes, the "text" option was named oddly, but it refers to CSV text. 

Craige
Jeff_Perkinson
Community Manager Community Manager

Re: How do I read in a text file line by line.

JSL doesn't currently have tools to open a file and read it one line at a time.

 

However, if you do use Load Text File() then you can use the Words() function to get in the individual lines into a list.

 

bctxt=load text file ("$SAMPLE_IMPORT_DATA\Bigclass.txt");

lines=words(bctxt,"\!n");
-Jeff
ENTHU
Level IV

Re: How do I read in a text file line by line.

I have a huge text file but want to read and load only a few lines.Basically want to load all lines starting with A_ or B_ or C_ or D_. Can if condition work? 

Re: How do I read in a text file line by line.

JMP does not stream over the text file. You would have to use the solution to load the entire file and then separate lines. You could iterate over the items in the list (individual lines) to determine if they meet your criterion.

 

Alternatively, you could import the text file into an Excel workbook and then use File > Database > Open Table or File > Database > Query Builder to define the WHERE clause to select the rows that you want to extract.

thickey1
Level III

Re: How do I read in a text file line by line.

Have to give the rewared to Jeff on this one despite my initial reticence on the loadTextFile Function. All three methods work so I did a quick time comparison using a 250M, 5M Line text file from one of our testers. Incidentally my use case is that I need to sift though these huge log files, line by line, and filter out some results based on a table of Regular Expressions.

 

I suppose this isn't fair as I did say I wanted to avoud loadTextFile ;-)

 

***Ian ***

myNumSeconds = 66;
N Items(myCfLines) = 5059756;

***Jeff ***

myNumSeconds = 7;
N Items(myLines) = 5059755;

*** Craige ***

myNumSeconds = 67;
N Items(myCfLines) = 5059756;