10-17-2016 04:50 PM
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 like
the text file looks like
Results look like
Thanks fo your help
Solved! Go to Solution.
10-17-2016 04:53 PM
Right click on your string constant and change to '\' Codes Display. And then type in \n. See if that fixes your problem.
10-17-2016 05:05 PM
Also you need to right-click and select 'Regular Expressions' otherwise special codes such as the quantifier will not work.
10-17-2016 05:41 PM
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.
Bob "Frustrated" Schor
10-17-2016 08:00 PM
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 like
the text file looks like
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
10-18-2016 04:00 PM
Bob, your "elegant" suggestion works for me.
10-18-2016 06:05 PM
Why wouldn't you look for \n\n if you have two consecutive end of line characters?
10-18-2016 11:10 PM
@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.