Jump to content

Find string, copy the entire line and delete rest


Recommended Posts

Hello,

I'm having a small problem trying to write a script (I'm a newbie :)) and hope someone can help me out.

I want:

- to open a textfile (always the same file)

- find a string that occurs only once in this textfile

- copy the entire data of the line the string is in

- delete everything in the textfile

- paste the copied data

- save and close the textfile

Example:

The textfile contains the following data:

PosX=0.000

PosY=12.569

PosZ=125.625

MachZ=564.897

CounterMR=1567

XAxis=546.669

ZAxis=65.589

I want the script to find "CounterMR" and then make the textfile containing only:

CounterMR=1567

Thank you for your time :(

Link to comment
Share on other sites

The textfile is a .res

It opens with Notepad.

The file is generated and read by the software of my CMM (measuring machine), so I cannot change the extension.

Edit: Also I cannot fill in extra data like [DATA].

The CMM collects the variables I stored in this file and will not read added data... it will error.

All the variables in this file are numerical variables.

Edited by Wazily
Link to comment
Share on other sites

Wazily,

This will do it :(

Local $strFile = @ScriptDir & '\file.txt' ; Location of file

$hFile = FileOpen($strFile, 0)

$arData = StringRegExp(FileRead($hFile), "CounterMR=([0-9]*)", 1)
$intVal = $arData[0] ; This is the value 1567 in the example

FileClose($hFile)

$hFile = FileOpen($strFile, 2)
FileWrite($hFile, "CounterMR=" & $intVal)
FileClose($hFile)

As you can see, I've used a regular expression to match "CounterMR=" but then we specify we only want it to return the data after it by enclosing the rest in parenthesis.

Hope this works for you.

James

Link to comment
Share on other sites

So if I'm right, the "[0-9]*" means I'm looking for numerical data that comes after the string "CounterMR="? Correct?

That is correct. The parenthesis tell any regular expression parser only to retrieve the data inside them. Anything outside is just to increase the chances of finding the right line or data to search through. If we didn't supply it with the "CounterMR=" we'd end up with all of it because the first line also fits the pattern of numbers.

And for what is the "*"?

If you remove the * from the expression, you'll see that only the first digit is returned in the string. So the * is a repeater, telling it to continue matching the expression till it can't find it any more.

I hope this has been useful for you :(

James

Link to comment
Share on other sites

This has been very helpful, thanks.

Been playing with it to clean up a stringvariable file and came up with this:

Local $strFile = @ScriptDir & '\file2.txt' ; Location of file

$hFile = FileOpen($strFile, 0)

$arData = StringRegExp(FileRead($hFile), "CounterMR=([[:print:]]*)", 1)
$strVal = $arData[0] ; This is the stringvalue after "CounterMR="

FileClose($hFile)

$hFile = FileOpen($strFile, 2)
FileWrite($hFile, "CounterMR=" & $strVal)
FileClose($hFile)

My next question is; can I make the script so that multiple lines are copied?

Example:

The textfile contains the following data:

PosX=0.000

PosY=12.569

PosZ=125.625

MachZ=564.897

CounterMR=1567

XAxis=546.669

ZAxis=65.589

I want the script to make the textfile containing only:

PosX=0.000

PosY=12.569

CounterMR=1567

Like in the previous one, everything after "=" is variable.

After this I think I'm done :(

Link to comment
Share on other sites

Wazily,

James gave you already the solution. Just some paste and copy and some declarations and the job is done:

Local $strFile = @ScriptDir & '\file2.txt' ; Location of file

$hFile = FileOpen($strFile, 0)
$string = FileRead($hFile)
$ardata = StringRegExp($string, "PosX=([[:print:]]*)", 1)
$strposx = $arData[0]
$ardata = StringRegExp($string, "PosY=([[:print:]]*)", 1)
$strposy = $arData[0]
$arData = StringRegExp($string, "CounterMR=([[:print:]]*)", 1)
$strVal = $arData[0] ; This is the stringvalue after "CounterMR="

FileClose($hFile)

$hFile = FileOpen($strFile, 2)
FileWriteLine($hFile, "PosX=" & $strposx)
FileWriteLine($hFile, "PosY=" & $strposy)
FileWriteLine($hFile, "CounterMR=" & $strVal)
FileClose($hFile)
ShellExecute ("notepad.exe", @ScriptDir & '\file2.txt')

;-))

Stefan

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...