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
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

  • Similar Content

    • By PeterVerbeek
      This topic give you access to an AutoIt functions library I maintain which is called PAL, Peter's AutoIt Library. The latest version 1.19 contains 194 functions divided into these topics:
      window and screen mouse and GUI GUI controls logics and mathematics string, xml string and file string dialogues and progress bars data lists: lists, stacks, shift registers and key maps (a.ka. dictionaries) miscellaneous: logging/debugging, process and system info A lot of these functions were created in the development of Peace, Peter's Equalizer APO Configuration Extension, which is a user interface for the system-wide audio driver called Equalizer APO.
    • By topgundcp
      Hi, 
      This is my very first post in this forum and am also new with Autoit programming so be easy on me.
      Below is the code that I am trying to get the index of the item selected. No problem getting the text.
      Case $listEdition   ; handle of the list             local $index=0             $item=GUICTRLRead($listEdition)    ; This will return the text in the list             ; ==============   The loop below always fails  ==================             ; Meaning _GUICtrlListView_GetItemSelected($listEdition, $i) always return FALSE. WHY ???????              For $i = 0 To $editionArray[0] - 1   ; The content of the list in an array where editionArray[0] contains total count of items                 If _GUICtrlListView_GetItemSelected($listEdition, $i) Then                     $index = $i                     exitloop                 EndIf              Next              ;===================================================             ConsoleWrite( "Select Edition:     " & $item & "  index: " & $index & @CR) ....... Please look at the comments in the code & the pix attached.
      _GUICtrlListView_GetItemSelected($listEdition, $i) always returns FALSE.
      Another problem is on the list. It also populate  the total count of items from the Array. How can I skip this item from populating to the list ?.
      Please advise and thanks

    • By n0ktar
      is it possible to make a wall chams in rainbow six siege using autoit scripts? tell me please and how . 
    • By Rskm
      Hi, I am using excel as input media for my program. The excel file (i tried with .xls, .xlsx and .xlsm format) has inputs which the autoit script reads during the run and performs few calculations. Some times (not always), after the run, when i try to open the excel file manually, the file doesnt open at all in excel. see the screenshot attached. However, if the execute the autoit script, the scripts still reads the existing data from that excel and performs the calcs. I copied the excel file to another computer and there too, it doesnt open.  So, after this, i cannot edit the excel forever (if i need to change any inputs). It is only this particular file that got affected. other excel files works normal.  What could be the problem here.  please help as this is a new challenge for me during my program development. 

    • By nacerbaaziz
      hello autoit group
      please i've a question
      i had make a function that put some thing into the clipBoard and paste it
      using the send function
      e.g
      ClipPut("hello")
      send("^v")
       
      when the keyboard is english all things work fine
      but when the keyboard is arabic the send command write the ltr v insted of the text in clipboard
      i was tryed to use
      send("{ctrldown}v{ctrlUp}")
      but the same
      please can any one help me
       
×
×
  • Create New...