Jump to content
Kruxe

Copying Specific lines of code from a .txt file and pasting them over lines in another .txt file

Recommended Posts

Hello Everyone,

I am very green when it comes to AutoIT, I have recently started using it to automate a job that i perform using MS Excel and that is working great. Recently I have been faced with a challenge I which i need to copy a specific set of lines from one .txt file and past them, overwriting a specific set of lines in a different .txt file. I honestly have no clue how I can copy the specified lines and transfer them. What functions / commands could I use and how could I lay them out I AutoIT? Any and all help will be appreciated. 

Thanks!

Kruxe

Share this post


Link to post
Share on other sites

Depends how big are those text files.  If they are not too large, you could use _FileReadToArray to get the source and target files, then modify target array from source lines and copy back to target using _FileWriteFromArray.

Share this post


Link to post
Share on other sites
52 minutes ago, Kruxe said:

i need to copy a specific set of lines from one .txt file and past them, overwriting a specific set of lines in a different .txt file.

As @Nine already pointed out, there are several possible approaches. It would be helpful, if you could post two sample texts (source, target). Then we would have an idea what you mean by 'specific set of lines'.


Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Share this post


Link to post
Share on other sites
5 minutes ago, Musashi said:

As @Nine already pointed out, there are several possible approaches. It would be helpful, if you could post two sample texts (source, target). Then we would have an idea what you mean by 'specific set of lines'.

@Nine, @Musashi

Thank you guys for the quick replies! I attached the 2 files that are part of this process. what I am looking to do is open O8000.txt and copy lines 8-46 then Open O1800.txt and past the lines from O8000 and overwrite lines 22-29 in O1800. Thank you so much for the help guys, I really appreciate it!

O8000.txt O1800.txt

Share this post


Link to post
Share on other sites

Since files are small, I would go with the array approach. Here something to start you up :

#include <File.au3>
#include <String.au3>

Opt ("MustDeclareVars", 1)

Local $aSource, $aTarget, $aExtract
_FileReadToArray ("O8000.txt",$aSource,$FRTA_NOCOUNT)
_ArrayDisplay ($aSource, "source file")
_FileReadToArray ("O1800.txt",$aTarget,$FRTA_NOCOUNT)
_ArrayDisplay ($aTarget, "before delete")
_ArrayDelete ($aTarget, "21-28")
_ArrayDisplay ($aTarget, "after delete")
$aExtract = _ArrayExtract ($aSource, 7, 45)
_ArrayDisplay ($aExtract,"extracted lines")
Local $sRange = StringTrimRight (_StringRepeat ("21;", UBound ($aExtract)),1)
_ArrayInsert ($aTarget, $sRange, $aExtract)
_ArrayDisplay ($aTarget,"new target")
_FileWriteFromArray ("O1800 New.txt", $aTarget)

 

Share this post


Link to post
Share on other sites

@Kruxe : a little additional explanatory note :

_FileReadToArray (..., $FRTA_NOCOUNT) means : the arrays are 0-based ==> Line 1 therefore has the index 0 ! So don't get confused by the fact, that the "line numbers" are 1 smaller ;).

--> _ArrayExtract ($aSource, 7, 45) ==> your lines 8 - 46 in O8000.txt

--> _ArrayDelete ($aTarget, "21-28") ==> your lines 22 - 29 in O1800.txt

Edited by Musashi

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Share this post


Link to post
Share on other sites
2 hours ago, Kruxe said:

a specific set of lines

A pretty vague definition!

If I guess correctly you want to replace a tooling sequence by another one.  Does this do the job?

Local $sToBeCopied = StringRegExpReplace(FileRead("O8000.txt"), "(?s)(.*?\RN1.*?\R)(.*?\RM99\R)(.*)", "$2")
Local $sModified = StringRegExpReplace(FileRead("O1800.txt"), "(?s)(.*?\RN1.*?\R)(.*?\RM50\R(?=\h*\R))(.*)", "$1" & $sToBeCopied & "$3")
FileDelete("O1800_new.txt")
FileWrite("O1800_new.txt", $sModified)

 


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

Holy Cow!!! You guys are awesome!!! Thank you so much for all the help i sincerely appreciate it. As someone that is completely new to AutoIT, it feels really good to know that there is such an great community of people willing to help you learn and grow! I will try all of your examples and see what would work best. Again, thanks a ton!!!

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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By shelly
      I have to press tab to reach a selected button then double click on that
      Here's my script but its not working:
      WinActivate("Policy Decisions -- Webpage Dialog","")
      Sleep(3000)
      Send("{TAB}")
      Send("{TAB}")
      Send("{TAB}")                    // at this point its working according to my need  but double click is not working
      Mouseclick("Left",510, 320, 2, 0)

    • By TheDcoder
      See this thread for info:
    • By nacerbaaziz
      hello evrybody
      here is an example about how to split your texts using a delimiter with the ability to select how much of delimiters shows in each colum  with $i_number
      e.g
      you have a long text and you want to split it in an array
      that evry colum have a number (n) of lines
      i made a function that do that for you
      just call it with a three params
      $s_text
      your text
      $i_number
      the number that you want to put in each col
      $s_siparator
      the siparator
      default is "|"
      here is the function with example
      i hope that it will be useful for you
       
      ****
       
      #include <Array.au3> $s_txt = "some text1some text2|some text3|some text4|some text5|some text6" $array = splitText($s_txt, 2) _ArrayDisplay($array) Func splitText($s_text, $i_number, $s_siparator = "|") Local $a_TXT = StringSplit($s_text, $s_siparator) Local $a_Return[$a_TXT[0] + 1] If ($a_TXT[0] <= $i_number) Or ($i_number <= 0) Then ReDim $a_Return[2] $a_Return[0] = 1 $a_Return[1] = $s_text Return $a_Return EndIf Local $i_Processed = 1, $i_arrayProcessed = 1 Do For $i = $i_Processed To ($i_Processed + $i_number) - 1 If ($a_TXT[0] < $i) Then ExitLoop If Not ($a_Return[$i_arrayProcessed]) Then $a_Return[$i_arrayProcessed] = $a_TXT[$i] Else $a_Return[$i_arrayProcessed] &= $s_siparator & $a_TXT[$i] EndIf $i_Processed += 1 Next $i_arrayProcessed += 1 Until ($a_TXT[0] < $i_Processed) ReDim $a_Return[$i_arrayProcessed] $a_Return[0] = $i_arrayProcessed - 1 Return $a_Return EndFunc ;==>splitText
      accept my greetings
      thanks to
      @Dan_555
      for his notes
       
    • By nooneclose
      Why can I not click on either of my input fields?
      #Region ### START Koda GUI section ### Form= Global $Form1 = GUICreate("Form1", 730, 437, 192, 124) ;GUISetFont(14, 800, 0, "MS Sans Serif") GUISetBkColor(0xC0DCC0) Global $Label1 = GUICtrlCreateLabel("Enter your Password. UserName is auto filled", 62, 40, 604, 41, $SS_CENTER) GUICtrlSetFont(-1, 26, 800, 0, "MS Sans Serif") Global $Label2 = GUICtrlCreateLabel("User Name:", 120, 130, 604, 31, $SS_LEFT) GUICtrlSetFont(-1, 20, 800, 0, "MS Sans Serif") Global $Label3 = GUICtrlCreateLabel("Password:", 135, 210, 604, 31, $SS_LEFT) GUICtrlSetFont(-1, 20, 800, 0, "MS Sans Serif") Global $Input1 = GUICtrlCreateInput(@UserName, 274, 130, 185, 32) GUICtrlSetFont(-1, 14, 800, 1, "MS Sans Serif") Global $Input2 = GUICtrlCreateInput("Password", 274, 210, 185, 32, $ES_PASSWORD) GUICtrlSetFont(-1, 14, 800, 0, "MS Sans Serif") Global $OK = GUICtrlCreateButton("OK", 274, 270, 185, 57) GUICtrlSetFont(-1, 20, 800, 0, "MS Sans Serif") GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $OK Global $UserName = GUICtrlRead($Input1) Global $PassWord = GUICtrlRead($Input2) ExitLoop EndSwitch WEnd ; Delete the GUI window GUIDelete()  
    • 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
×
×
  • Create New...