Jump to content
Rskm

replace a particular text in a file - (Moved)

Recommended Posts

Rskm

Hi, I have the following line in a text file 'input.txt'. I know the line number - say '6'. I wish to replace the text 'WWW' in the below line with a random number (I can generate that with random()).

WERIS  WWWJP   3.83  8.330  1.000                1097.RAXX 

The WWW is a 3 digit integer (could be any number between 0 to 999), I can use stringtrimleft and get the numerical value of WWW in this file

so, basically, I know the string to replace (ie; WWW stored in a variable), I know the line number to work on and the file location/name and the replacement variable (through random()). My requirement is to fill that 3 spaces with my random number (which Is a integer between 1 and 999)

please put ur suggestions

 

Share this post


Link to post
Share on other sites
Jos

Moved to the appropriate forum, as the AutoIt Example Scripts forum very clearly states:

Quote

Share your cool AutoIt scripts, UDFs and applications with others.


Do not post general support questions here, instead use the AutoIt Help and Support forums.

Moderation Team


SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
ModemJunki
Posted (edited)
11 hours ago, Rskm said:

please put ur suggestions

Something like:

#include <String.au3>

$s_oldstring = "WERIS  WWWJP   3.83  8.330  1.000                1097.RAXX"

ConsoleWrite($s_oldstring & @CRLF)

$s_newstring = StringReplace($s_oldstring, 8, _stringPad(random(0, 999, 1), '0', 3, 0))
ConsoleWrite($s_newstring & @CRLF)

; #FUNCTION# ====================================================================================================================
; Name ..........: _stringPad
; Description ...: Pads a string to a specified length with a specified character.
; Syntax ........: _stringPad($string[, $pad = ' '[, $count = 10[, $opt = 1]]])
; Parameters ....: $string              - the string to pad.
;                  $pad                 - the character to use as the padding. Default is ' '.
;                  $count               - the total width of the padded output. Default is 10.
;                  $opt                 - left (1) or right (0) justification of the input string. Default is 1.
; Return values .: String with padded values
; Author ........: Xenobiologist
; Modified ......: 31-07-2018
; Remarks .......: requires #include <string.au3>
; Related .......:
; Link ..........: https://www.autoitscript.com/forum/topic/69925-how-do-i-pad-a-string/
; Example .......: No
; ===============================================================================================================================
Func _stringPad($string, $pad = ' ', $count = 10, $opt = 1)
    If $opt = 1 Then
        Return $string & _StringRepeat($pad, $count - StringLen($string))
    Else
        Return _StringRepeat($pad, $count - StringLen($string)) & $string
    EndIf
EndFunc   ;==>_stringPad

The function _stringPad created in 2008 by @Xenobiologist

Edited by ModemJunki
prettified _stringPad

Share this post


Link to post
Share on other sites
Rskm

@Jos, Sorry for posting on wrong window

@ModemJunki, that really helped me. I was trying a lot with stringtrim and other options.. that piece of code just nailed it.. thanks a lot

 

Share this post


Link to post
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

  • Similar Content

    • nooneclose
      By nooneclose
      I need help turning this string "20180913221626" into a formatted time string.
      I need to go from this: 20180913221626
      to this: 09/13/2018 10:16 PM
      I do not always know what the date will be so I can not just use a variable I need to actually convert/format. 
      I did see an older post in the forms that was basically the same question only the other guy did not post the working code and I can not figure out how to use _AD_GetObjectProperties properly to get what I want. 
      As always any help would be appreciated. 
      Here is the code I use to find the date, but it always gives me the unformatted version. 
      ;retrieve the items object $oItem = $oOutlook.Session.GetItemFromID($aItems[1][0], Default) $oItem.GetInspector $eSentOn = $oItem.SentOn ; When was the email sent? MsgBox("", "Sent On of the email", $eSentOn) ;******************************************************************************* ; Formats the date and time from the email ;******************************************************************************* ;Local $fDatenTime = _DateTimeFormat($eSentOn, 1) ;MsgBox("", "Formatted email time", $fDatenTime) $aProperties = _AD_GetObjectProperties($eSentOn) _ArrayDisplay($aProperties, "Did the conversion work?")  
    • Miliardsto
      By Miliardsto
      I got that func
      Func makeHelpImgGUI($title,$width,$height,$img) $img = GUICtrlCreatePic("",20,40,$width,$height) _ResourceSetImageToCtrl($img, "HERE") EndFunc and I call this func like that
      makeHelpImgGUI("Image",1190, 800,$SETTINGS_JPG)  
      so what is the problem in the parameter where is - "HERE" I need value of img but passed as string
      so $img = $SETTINGS_JPG and how make it "SETTINGS_JPG"
       
      I tried something like that but not work
      Func makeHelpImgGUI($title,$width,$height,$img) $name_str = String($img) $name_str = StringTrimLeft ($name_str, 1 ) $img = GUICtrlCreatePic("",20,40,$width,$height) _ResourceSetImageToCtrl($img, $name_str) EndFunc  
    • liagason
      By liagason
      Hello everyone,
      How can I display in ascending  sequence some numbers stored in a string variable?
      $str = "18,03,48,23" MsgBox(0,"test",$str) I would like it to display "03,18,23,48"
    • FrancescoDiMuro
      By FrancescoDiMuro
      Good evening everyone
      I am working with Word UDF ( thanks @water! ), and, especially, with the function _Word_DocFindReplace().
      The replace does work everywhere in the document, but, it does not work in Headers or Footers.
      Am I missing something or am I forced to use the code below?
      I have already looked in the Help file ( about _Word_DocFindReplace() ), but there are no mentions about replace text in Headers/Footers.
      Sub FindAndReplaceFirstStoryOfEachType() Dim rngStory As Range For Each rngStory In ActiveDocument.StoryRanges With rngStory.Find .Text = "find text" .Replacement.Text = "I'm found .Wrap = wdFindContinue .Execute Replace:=wdReplaceAll End With Next rngStory End Sub Thanks everyone in advance


      Best Regards.
    • nacerbaaziz
      By nacerbaaziz
      Hello Members of this best Forum
      i have a question please
      for example if i have a long string
      and i want to extract a text between two tag
      what i can do to make that?
      note :
      i know that there is a
      StringRegExp function
      it's do that work
      but it result is be as an array
      i want the result to be a string
      is there any function on autoit can do that?
      Thanks in advance.
×