Jump to content

Convert bash or powershell to AutoIT


Recommended Posts

I really want to use AutoIT to perform a search for lines beginning with a string and then replace the whole line with a substitute. (quickly for an entire file)

In powershell I do it like this:

$ComputerName = $env:COMPUTERNAME

$FilePath = "C:\scripts\data.txt"

$FindIT = "WorkstationID"

$ReplaceIT = ("WorkstationID=" + $ComputerName + "stuff")

#"----------------------"

(Get-Content ($FilePath)) |

Foreach-Object { $_ -replace ('^' + $FindIT + '.*$'), $ReplaceIT } |

Set-Content ($FilePath)

"cat the file to screen to see changes"

"-----------------------------------------------"

Get-Content ($FilePath) | foreach {Write-Output $_}

in bash I do it like this, yes it's Linux but I need to do this type of thing in Windows:

swap()

#------------------------ swap function ------------------------------------------

# this little function is for a find and replace of lines starting with a string

# first parameter is the file to be modified

# second parameter is the line that begins with the string provided (all occurances)

# third parameter is the entire line of data to insert in the place of second parameter

{

#echo first command line parameter (file name) is "$1"

#echo second parameter (search for) is "$2"

#echo third parameter (replace string) is "$3"

if [ ! -f $1 ]

then

echo "A find and replace command was attempted against $1 but the file could not be found!"

return

else

mv $1 $1"-temp1"

varFind="$2"

varReplace="$3"

cat $1"-temp1" | sed -e "s,^$varFind.*,$varReplace,g" >> $1

rm -f $1"-temp1"

echo "Modified $1 with $3"

fi

};

#-------------- end swap ------------------------------------------------------

How do I do this in AutoIT?

Link to comment
Share on other sites

Hi.

The computername is available through the macro @computername.

Read the File Content to an array:

#include <file.au3>
#include <array.au3>

$inFile = "C:\temp\somefile.txt"
$outFile = "C:\temp\midyfied.txt"
$searchstring = "Before"
$replacestring = "After"

Dim $aContent

_FileReadToArray($inFile, $aContent)
_ArrayDisplay($aContent)


For $i = 1 To $aContent[0]
    $aContent[$i] = StringReplace($aContent[$i], $searchstring, $replacestring)
Next

_FileWriteFromArray($outFile, $aContent, 1)

Also have a look at the function "STringRegExREplace()".

Regards, Rudi.

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Link to comment
Share on other sites

Wow, that linux solution you have is very, very overworked. Seems like you could do that with quite an easy oneliner:

my_linuxbox:/tmp> perl -lpwe "s#${regEx}#${replacement}#g" -i ${filespec}

my_linuxbox:/tmp> cat test

abcde

12345abcde

abcde12345

XYZ

my_linuxbox:/tmp> perl -lpwe "s#^abcde.*#REPLACED#g" -i test

my_linuxbox:/tmp> cat test

REPLACED

12345abcde

REPLACED

XYZ

Edited by SadBunny

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

Also have a look at the function "STringRegExREplace()".

It's actually called "StringRexExpReplace" and TS should indeed use it if you ask me. Same replacement as in my perl oneliner above can be done as follows:

$text = "abcde" & @CRLF & "12345abcde" & @CRLF & "abcde12345" & @CRLF & "XYZ" & @CRLF
MsgBox(0,"Before:", $text)
$text = StringRegExpReplace($text, "(?m)^abcde.*$", "REPLACED")
MsgBox(0,"After:", $text)

/edit: the (?m) regex modifier is needed in the au3 script to make the regex function see the @CRLF as triggering the $ (regex for end-of-line). I'm not sure why it does what it does if you leave that out.

/edit2: it also seems more efficient to me to read the file to a single string and do the replacement, than reading the file to an array (which means splitting a large string into array elements) and then going through the array elements and replacing them piece-by-piece if they fit the pattern. Depending on whether you need the split array lines later on for something else, I'd avoid using large text array operations like this wherever possible.

Edited by SadBunny

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

I really want to use AutoIT to perform a search for lines beginning with a string and then replace the whole line with a substitute. (quickly for an entire file)

In powershell I do it like this:

$ComputerName = $env:COMPUTERNAME

$FilePath = "C:\scripts\data.txt"

$FindIT = "WorkstationID"

$ReplaceIT = ("WorkstationID=" + $ComputerName + "stuff")

#"----------------------"

(Get-Content ($FilePath)) |

Foreach-Object { $_ -replace ('^' + $FindIT + '.*$'), $ReplaceIT } |

Set-Content ($FilePath)

"cat the file to screen to see changes"

"-----------------------------------------------"

Get-Content ($FilePath) | foreach {Write-Output $_}

in bash I do it like this, yes it's Linux but I need to do this type of thing in Windows:

swap()

....

How do I do this in AutoIT?

In AutoIt I do it like this:

;$ComputerName = $env:COMPUTERNAME <--- Not needed (its the computer you're using.)
$FilePath = @ScriptDir & "\data.txt" ; "C:\scripts\data.txt"
$FindIT = "WorkstationID"
$ReplaceIT = '("WorkstationID=" + $ComputerName + "stuff")'
;#"----------------------"
;(Get-Content ($FilePath)) |
$Get_Content = FileRead($FilePath)

;Foreach-Object { $_ -replace ('^' + $FindIT + '.*$'), $ReplaceIT } |
$New_Contents = StringRegExpReplace($Get_Content, "(?s)(^.*[^\v]*)(" & $FindIT & ")([^\v]*\v+.*$)", "$1" & $ReplaceIT & "$3")

;Set-Content ($FilePath)
Local $file = FileOpen($FilePath, 2)
FileWrite($file, $New_Contents)
FileClose($file)

;"cat the file to screen to see changes"
;"-----------------------------------------------"
;Get-Content ($FilePath) | foreach {Write-Output $_}
ShellExecute($FilePath)
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...