LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Remove multiple new lines in Search and replace string

Solved!
Go to solution

Hi I'm importing a text file with multiple new lines.

I'm trying to replace multiple new lines (\n) with only one new line (\n) and I'm not successful with it so far.

I have tried multiple combinations for the search string pattern including:

\n{2}

[\n]{2}

\n{2,}

[\n]{2,}

Nothing works so far. I still get multiple \n.

 

The block diagram look likeblock diagram.PNG

the text file looks like 

text.PNG

 

Results look like

results.PNG

 

Thanks fo your help

0 Kudos
Message 1 of 8
(5,427 Views)

Right click on your string constant and change to '\' Codes Display.  And then type in \n.  See if that fixes your problem.

aputman
------------------
Heads up! NI has moved LabVIEW to a mandatory SaaS subscription policy, along with a big price increase. Make your voice heard.
0 Kudos
Message 2 of 8
(5,426 Views)
Solution
Accepted by topic author Z.Wei

Also you need to right-click and select 'Regular Expressions'  otherwise special codes such as the quantifier will not work.

Message 3 of 8
(5,421 Views)

Well, I had an "almost elegant" way to do this, but stumbled on the last step.  If you read your file with Read from Text File, but turn Read Lines on, it reads the file into an array of "lines".  A simple For Loop with conditional indexing can transform this to an Array where the empty lines have been eliminated.

 

All that remains is to reassemble the Array into a String separated by EOLs.  Oops, this was harder than it looked.  I tried Array to Spreadsheet (which says it returns the Array as a String separated by EOLs, but that didn't work for me, don't know why).  I ended up doing a Brute Force re-concatenation, which really lacks elegance ...  Oh, well.

Remove Blank Lines.png

 

Bob "Frustrated" Schor

 

 

0 Kudos
Message 4 of 8
(5,413 Views)

Z.Wei a écrit :

Hi I'm importing a text file with multiple new lines.

I'm trying to replace multiple new lines (\n) with only one new line (\n) and I'm not successful with it so far.

I have tried multiple combinations for the search string pattern including:

\n{2}

[\n]{2}

\n{2,}

[\n]{2,}

Nothing works so far. I still get multiple \n.

 

The block diagram look likeblock diagram.PNG

the text file looks like 

text.PNG

 

Results look like

 

 

Thanks fo your help


You have 2 problems as specified by aputman and Darin. First you need to right-click Search and Replace String.vi and select Regular Expression (a small star will appear near the top left of the icon). Then your replace string (only) need to be set to code display, when set to code display you type \n. Note that the regular expression string must be set to normal display to use \n{2,} this will replace 2 or more \n. You don't need to put \n in a character class ([]) since you only have one character (althought it is not an error).

 

If you don't set the replace string to code display you can use the line feed constant as an alternative.

 

Ben64

0 Kudos
Message 5 of 8
(5,396 Views)

Bob, your "elegant" suggestion works for me.  

Example_VI.png

aputman
------------------
Heads up! NI has moved LabVIEW to a mandatory SaaS subscription policy, along with a big price increase. Make your voice heard.
0 Kudos
Message 6 of 8
(5,349 Views)

Why wouldn't you look for \n\n if you have two consecutive end of line characters?

0 Kudos
Message 7 of 8
(5,340 Views)

@natasftw wrote:

Why wouldn't you look for \n\n if you have two consecutive end of line characters?


Your method works for what he has shown to want (specifically, two LF characters); the others work for what he said he wanted (multiple LF).  I was thinking the same thing as you were.

Bill
CLD
(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
0 Kudos
Message 8 of 8
(5,330 Views)