LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Ignoring blank lines in a text file

Using LabVIEW 2009 on Windows XP.

 

Looked through some of the postings...found a few clues, but thought I'd post anyway.

 

I'd like to ignore the blank lines in a text file that I'm reading.

 

Reading the text file line by line and passing it to a for loop with auto indexing enabled.

 

For loop increments...even when the line being passed is blank.

 

I'd like to filter these blank lines before they ever reach the for loop.

 

Thanks for tips/suggestions.

0 Kudos
Message 1 of 6
(5,063 Views)

Disable auto-indexing.  Maintain your own shift register for the array.  Build a new row onto the array if it is not blank, just pass the array through if it is blank.

0 Kudos
Message 2 of 6
(5,057 Views)
I second Ravens Fan's thinking. You will need to read the full file, manipulate that information to take out the blank lines, and then pass it to your For Loop so that it only iterates the number of lines that you care about. The way that I would do it would be to enable Read Lines on your Read From Text File.VI (Right-click option on the VI itself) and then pass the outputted array (it reads the files line by line and packages that up into an array of strings) into a first For Loop and check which lines are blank (String Length.VI or something like that) and build up your own array that doesn't have any blank lines and then pass that "polished" array into your For Loop that you need to iterate the same amount as you have non-blank lines. I have attached a picture showing a rough VI I created that does what I talked about above. Hope this helps!
0 Kudos
Message 3 of 6
(5,034 Views)

I am usually in a Search and Replace kind of mood.  I'd look for multiple newline characters '\n' and replace them with a single one.  In case you were reinventing the wheel in your For Loop, I threw in a Spreadsheet String to Array Function.  Otherwise, I show you how to pass a clean string into a For Loop and pick out the lines.

 

 FixBlankLines.png

0 Kudos
Message 4 of 6
(5,012 Views)

Hi Darin,

 

I like your method of removing the blank lines, very simple. Your posted snippet has 1 slight problem that i noticed when running. The for loop counter needs to have a +1 added otherwise it misses the last line.

 

Darins remove blank line.png

 

I modified your above vi slightly to just use the spreadsheet to array vi to build the array:

 

 

Remove blank lines.png

 

I think the combination is a neat solution.

 

Rgs,

 

Lucither

 

------------------------------------------------------------------------------------------------------
"Everything should be made as simple as possible but no simpler"
0 Kudos
Message 5 of 6
(4,981 Views)

just don't forget do check for \r in some files...

it may be safer (it will take longer) to search and replace multiple:

\r

\n

\r\n

 

Smiley Wink

CLD; LabVIEW since 8.0, Currently have LabVIEW 2015 SP1, 2018SP1 & 2020 installed
Message 6 of 6
(4,972 Views)