Jump to content
Sign in to follow this  
Rskm

replace a particular text in a file - (Moved)

Recommended Posts

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

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 etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
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

Always carry a towel.

Share this post


Link to post
Share on other sites

@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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By cruisepandey
      Hi, 
      I have a string like this : 
      Global $Msga = "urrent directory is /send.  (Submission of file with log number 29381077284 is confirmed)";
      I want to extract the number 29381077284  from the string. I did StringSplit to split based on "(" and then use space to reach there, But it's not a good choice. 
      Can anyone help me with regular expression to find the number from String using AutoIT. TIA
    • By DannyJ
      I have this example string:
       
      2020-06-08 09:23:33 : abcdefghifjklm How to trim just the right part? Or how to trim this text right to left?
      I want to trim this string right to left until ' : '.
      In ohter words: I need the 'abcdefghifklm' text.
      I have tried StringTrimRight, but that requires 
      The number of characters to trim. Unfortunately StringSplit trims the left part of the strings.
       
       
    • By careca
      This is my take on string triggers, triggers on specific strings.
      Able to simple text pasting,
      opening links (as long as there's a www. http:\\ or https:\\ at the beggining)
      and is able to open applications, if there is a parameter in the parameter field, it uses it.
      Shows your external, lan, and gateway ip's.
      Able to change system volume by a set percentage, reading from the inputbox the number the user sets, if 0 or empty uses system default.
      I made this because the existing string trigger applications didn't do it for me.
      I did this for me, but if someone finds it useful, all the better.
    • By Colduction
      Hi guys!, i have a problem to convert Python code to AutoIt code, in fact i had not coded with Python yet!, this code is about permutation a string's case, i will be happy with your comments :)❤;

      Python code:
       
      # Python code to print all permutations # with respect to cases # Function to generate permutations def permute(inp): n = len(inp) # Number of permutations is 2^n mx = 1 << n # Converting string to lower case inp = inp.lower() # Using all subsequences and permuting them for i in range(mx): # If j-th bit is set, we convert it to upper case combination = [k for k in inp] for j in range(n): if (((i >> j) & 1) == 1): combination[j] = inp[j].upper() temp = "" # Printing current combination for i in combination: temp += i print(temp), # Driver code permute("Hello") # This code is contributed by Sachin Bisht
      My code in AutoIt:
      ; https://www.geeksforgeeks.org/permute-string-changing-case/ _PermuteCase("ABC") Func _PermuteCase($sText) If StringRegExp($sText, "^[A-Za-z]{1,}$") Then Local $iLength = StringLen($sText) ; Get length of the text. Local $iMaxPerm = 2 ^ $iLength ; Number of permutations is 2^n Local $sLow_Text = StringLower($sText) ; Converting string to lower case Local $asChrs = StringToASCIIArray($sLow_Text) ; Split the text into array of chars. For $i = 1 To $iMaxPerm Step 1 For $j = 0 To $asChrs[0] ;................................................... Next Next Else Return SetError(-1, 0, "Error: Input is incorrect!") EndIf EndFunc ;==>_PermuteCase  
       
       
       
       
       
      ====================== SOLUTION by @TheXman ======================
       
    • By jmp
      i am trying to get number from string using this code :
      #include <IE.au3> $oIE = _IEAttach ("Edu.corner") Local $aName = "Student name & Code:", $iaName = "0" Local $oTds = _IETagNameGetCollection($oIE, "td") For $oTd In $oTds If $oTd.InnerText = $aName Then $iaName = $oTd.NextElementSibling.InnerText $iGet = StringRegExpReplace($iaName, "\D", "") EndIf Next MsgBox(0, "", $iGet) it was get number like 52503058
      But, I want to get only student code 5250. (Different student have different code, sometime its 3 digits, Sometime 4)

       
×
×
  • Create New...