Jump to content

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


Kruxe
 Share

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

Link to comment
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.

Link to comment
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."

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

Link to comment
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)

 

Link to comment
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."

Link to comment
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)

Link to comment
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!!!

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

×
×
  • Create New...