From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Replace some texts in a text file for selected set of lines

Hi,

 

I want to replace some texts in a text file for a selected set of lines.

 

For eg:

 

if my text file contains the following data:- numbers within braces are line numbers..rest is the actual data.

 

(1)   aaaaa

(2)   bbbbb

(3)   bbbbb

(4)   ccccc

(5)   ddddd

(6)   eeeee

(7)   ggggg

(8)   hhhhh

(9)   hhhhh

(10) hhhhh

 

i want to replace "hhhhh" by "ccccc" choosing a set of lines, between (5-9), so it becomes

 

(5)   ddddd

(6)   eeeee

(7)   ggggg

(8)   ccccc

(9)   ccccc

 

after replacing the data, it has to be written back to the file in the same position..

 

changed file output..

 

(1)   aaaaa

(2)   bbbbb

(3)   bbbbb

(4)   ccccc

(5)   ddddd

(6)   eeeee

(7)   ggggg

(8)   ccccc

(9)   ccccc

(10) hhhhh

 

 

Can this be done in labview ? any examples?

 

Kindly help.

0 Kudos
Message 1 of 28
(6,524 Views)

You can try something like this:

StringReplace.PNG

 

/Y

G# - Award winning reference based OOP for LV, for free! - Qestit VIPM GitHub

Qestit Systems
Certified-LabVIEW-Developer
Message 2 of 28
(6,511 Views)

hi Yamaeda,

 

Thanks for your reply.

 

But I wanted to read a file which is of huge size 40 or 50 MB (2 lakh lines - data recorded for a single day) and the operation is repeated many times within a short span of time, which in this case is difficult to achieve.

 

 

 

0 Kudos
Message 3 of 28
(6,507 Views)

I assume you have atleast 2000 Mb of memory, so it should work. 😉

Else you'll have to change it slightly to use Set File position to only read and write the specific lines.

 

/Y

G# - Award winning reference based OOP for LV, for free! - Qestit VIPM GitHub

Qestit Systems
Certified-LabVIEW-Developer
0 Kudos
Message 4 of 28
(6,493 Views)

The Set File Position or Get File Position work with bytes offset but how to get this in terms of lines ?

 

Is there a way to get to a specific line in a file ?

0 Kudos
Message 5 of 28
(6,487 Views)

@Arvinth wrote:

The Set File Position or Get File Position work with bytes offset but how to get this in terms of lines ?

 

Is there a way to get to a specific line in a file ?


Read line X amount of times? Smiley Surprised I dont think there's any other way. Then you might as well copy the file line by line to another file and perform the work on the selected lines, or simply do it at once like i suggested. I think it's the absolutely fastest. 🙂

 

/Y

G# - Award winning reference based OOP for LV, for free! - Qestit VIPM GitHub

Qestit Systems
Certified-LabVIEW-Developer
0 Kudos
Message 6 of 28
(6,481 Views)

See if either of these address your needs.

Jim

LV 2020
0 Kudos
Message 7 of 28
(6,473 Views)

hi Jim,

 

Is the Read lines from file.vi is part of LabView 8.6.1 ? Can you tell me where it is placed in the functions palette?

 

Regards,

Arvinth

0 Kudos
Message 8 of 28
(6,448 Views)

Fast and easy are not synonymous in this particular case.  Fast will also depend on how much memory you are willing to eat.  The fastest method I know, given your comments above, is to do the following.

 

 

  1. Open the file
  2. Query the length of the file
  3. Create a string of that length in LabVIEW.  Do this by initializing an array of U8s to the length you need, then converting that to a string.
  4. Read the file from disk in 65,000 byte chunks.  As you read each chunk, write it to your internal array at the appropriate location.
  5. Use Search and Replace String on the final string to replace all your instances.
  6. Rewind the file to the start and write the string to it in 65,000 byte chunks.

 

You can replace steps 2-4 with simply reading the entire file from disk, but it will be a LOT slower (50MBytes should not take more than about 5 seconds to get from disk, and half that on a modern disk).  Similarly, you can simply write the entire string to disk all at once, but it will be a LOT slower.

 

You can trade memory for speed by reading a 65,000 byte chunk, replacing, and rewriting it.  Be careful with partial line reads if you do this.  This will be slower because the disk will need to keep rewinding to the start of the 65,000 byte chunks on write.  65,000 bytes is the optimum chunk size for streaming to and from disk on Windows systems (NTFS and FAT32).  Note that it is a broad optimum, so a bit more or less is fine if that lines up with a line boundary.

 

Let us know if you need more help.

0 Kudos
Message 9 of 28
(6,424 Views)

When I see these kind of question I always think what would a solution using regex would look like. So attached is my try (LV2009), it might not be the most effective solution but it was fun doing it 😉

 

Ben64

 

 

0 Kudos
Message 10 of 28
(6,417 Views)