Jump to content

Inject Text String At Beginning Of Each Line (Resolved)


Recommended Posts

Greetings!

The standard disclaimer applies, searched all of the forums, read the help file, new to AutoIT, not sure where to begin.

The task at hand is to "inject" different text strings at the start of each line in a text file.

Original file has 30 lines

---

Random Text|More Random Text|Even More Random Text

Random Text|More Random Text|Even More Random Text

Random Text|More Random Text|Even More Random Text

Random Text|More Random Text|Even More Random Text

Random Text|More Random Text|Even More Random Text

The injected file would look like the table below. No line space is required. A line space has been added to indicate where the values in column 4 and 5 change. The line count (column 1, 01-30) is constant, whereas, columns 4 and 5 change after 8, 8, 4, 4, 2, 2, 1, 1.

I have no sample code to provide as I do not know if the solution lies in a FileWrite, IniWrite or an Array.

Any help/guidance would be greatly appreciated.

Thanks!

01|2008|15|FT|1|Random Text|More Random Text|Even More Random Text

02|2008|15|FT|1|Random Text|More Random Text|Even More Random Text

03|2008|15|FT|1|Random Text|More Random Text|Even More Random Text

04|2008|15|FT|1|Random Text|More Random Text|Even More Random Text

05|2008|15|FT|1|Random Text|More Random Text|Even More Random Text

06|2008|15|FT|1|Random Text|More Random Text|Even More Random Text

07|2008|15|FT|1|Random Text|More Random Text|Even More Random Text

08|2008|15|FT|1|Random Text|More Random Text|Even More Random Text

09|2008|15|CF|1|Random Text|More Random Text|Even More Random Text

10|2008|15|CF|1|Random Text|More Random Text|Even More Random Text

11|2008|15|CF|1|Random Text|More Random Text|Even More Random Text

12|2008|15|CF|1|Random Text|More Random Text|Even More Random Text

13|2008|15|CF|1|Random Text|More Random Text|Even More Random Text

14|2008|15|CF|1|Random Text|More Random Text|Even More Random Text

15|2008|15|CF|1|Random Text|More Random Text|Even More Random Text

16|2008|15|CF|1|Random Text|More Random Text|Even More Random Text

17|2008|15|FT|2|Random Text|More Random Text|Even More Random Text

18|2008|15|FT|2|Random Text|More Random Text|Even More Random Text

19|2008|15|FT|2|Random Text|More Random Text|Even More Random Text

20|2008|15|FT|2|Random Text|More Random Text|Even More Random Text

21|2008|15|CF|2|Random Text|More Random Text|Even More Random Text

22|2008|15|CF|2|Random Text|More Random Text|Even More Random Text

23|2008|15|CF|2|Random Text|More Random Text|Even More Random Text

24|2008|15|CF|2|Random Text|More Random Text|Even More Random Text

25|2008|15|FT|3|Random Text|More Random Text|Even More Random Text

26|2008|15|FT|3|Random Text|More Random Text|Even More Random Text

27|2008|15|CF|3|Random Text|More Random Text|Even More Random Text

28|2008|15|CF|3|Random Text|More Random Text|Even More Random Text

29|2008|15|FT|4|Random Text|More Random Text|Even More Random Text

30|2008|15|CF|4|Random Text|More Random Text|Even More Random Text

Edited by NobodyWannabe
Link to comment
Share on other sites

Do a _FileReadToArray()

Then loop through the array and pre-pend you

#Include <File.au3>
Dim $Lines
_FileReadToArray(FILEPATH, $Lines)

Dim $Newlines[$Lines[0]+1]
For $x= 1 to $Lines[0]
Select $x
Case $x <= 8
   ; Modify First Group of lines
$Add = "0"&$x&"|2008|15|FT|1|"
$Newlines[$x] = $add & $Lines[$x]
Case $x > 8 AND $x <= 16 
   ; Modify second Group of lines
EndSelect
Next

I dunno that would be one way to do it :P

Link to comment
Share on other sites

Usually, inserting text before a line in a file requires you to re-create the entire file yourself rather than a programmatic command doing it. Most programming commands I have seen allow you to append a line to the end of a file not to the beginning of a file or line.

Looking at the File commands in AutoIt pertaining to what you want to do seems to agree with the appending scenario.

-FileWriteLine 'Append a line of text to the end of a previously opened text file'. It also adds a CR and LF if it is not already there.

-FileWrite 'Append a line of text to the end of a previously opened text file'. It does NOT add a CR or LF to the end of the line.

What I have done before (not in AutoIt) is this:

Open a temp file for writing.

Read each line of the file you want to modify, writing each line that is read to the temp file until you find the line of text you want.

Save the line of text from the file as a string and format the string so that the old text is appended to the new.

Write the new line of text to the temp file.

Continue reading the original file and writing each successive line to the temp file.

Once the reading is done, delete the original file and rename the temp file as the original file.

You could of course use the AutoIt's INI commands which are handy. I don't remember what kind of limits there are to .ini files though as far as size/line size. I seem to remember there were some (at least in windows).

Ini file commands are pretty handy because the .ini files are slightly structured so you can jump to/modify/delete different sections and valutes in the file without having to recreate the entire file.

Though, it may be that .ini file commands are actually doing things behind the scenes that cause the entire contents of the file to be rewritten so that they just appear to be making changes in the middle of the file.

Anyway, hope that helps. I may be wrong too...maybe there is a way to write into the middle of a file without recreating it.

Good luck.

Be open minded but not gullible.A hammer sees everything as a nail ... so don't be A tool ... be many tools.

Link to comment
Share on other sites

I like Paulie's idea better. It should work faster.

Of course, if the file is huge then you may need to do the temp file or something like compressing the text in memory, blah blah blah.

Be open minded but not gullible.A hammer sees everything as a nail ... so don't be A tool ... be many tools.

Link to comment
Share on other sites

Guys, thanks for your quick answers. An idea has come to mind. What about injecting in reverse using two files. The first file has the first five columns, the second file has the random data.

Based on Paulie's response, I was able to read the Random Text into an array. No idea what to do with it now that it's in an array, though.

That done, would it be easier to append the Random Text from the array to the end of the first file that contains the five columns?

In doing so, would it not be a more simple resolution?

Read the Random Text into an array, inject it at the end of each line of another file.

To LostUser, the file is static and only 30 lines. Size, therefore, is not an issue. Nor is speed.

Thanks for taking the time and sharing your thoughts and expertise.

Edited by NobodyWannabe
Link to comment
Share on other sites

Well that would be a much easier thing to do!

Read them both to 2 different arrays then just merge like this

Fill in the filepaths, the run, and check your desktop for the completed file

#include <File.au3>
Dim $Lines1, $Lines2, $String = ""
_FileReadToArray(FILEPATH, $Lines1) ; this should be the "random data" original file
_FileReadToArray(FILEPATH2, $Lines2) ; this should be the file with the 5 columns

$Result = PrePendMerge($Lines1, $Lines2)
For $x = 1 To UBound($Result) - 1
    $String &= $Result[$x] & @CRLF
Next
$File = FileOpen(@DesktopDir & "\Completed Prepended File.txt", 2)
FileWrite($File, $String)
FileClose($File)

Func PrePendMerge($array1, $array2)
    ;Add each element of $array2 in front of each element in array1
    If UBound($array1) <> UBound($array2) Then Return 0 ;ERROR
    Dim $NewArray[UBound($array1)]
    For $x = 0 To UBound($NewArray) - 1
        $NewArray[$x] = $array2[$x] & $array1[$x]
    Next
    Return $NewArray
EndFunc   ;==>PrePendMerge

EDIT: Tidied, Forgot FileClose() :P

Edited by Paulie
Link to comment
Share on other sites

Paulie,

Thanks a lot! Your script runs, creates the new file, however, it is empty.

Your thoughts?

Thanks again for the fast response, and of course, ideas on how to get this task accomplished quickly and easily.

Odd... Did you catch my edit? I accidentally left out the "Fileclose()" the first time... But i just made my own test files, and it worked flawlessly

Edit, The error could be in the _FileReadToArray() Try using arraydisplay to debug and see where the data gets lost.

Edited by Paulie
Link to comment
Share on other sites

Paulie,

It won't go.

Here's what I've changed...

---

#include <File.au3>

Dim $Lines1, $Lines2, $String = ""

_FileReadToArray("c:\random.txt", $Lines1) ; this should be the "random data" original file

_FileReadToArray("c:\five_col.txt", $Lines2) ; this should be the file with the 5 columns

$Result = PrePendMerge($Lines1, $Lines2)

For $x = 1 To UBound($Result) - 1

$String &= $Result[$x] & @CRLF

Next

$File = FileOpen("c:\combined.txt", 2)

FileWrite($File, $String)

FileClose($File)

Func PrePendMerge($array1, $array2)

;Add each element of $array2 in front of each element in array1

If UBound($array1) <> UBound($array2) Then Return 0 ;ERROR

Dim $NewArray[uBound($array1)]

For $x = 0 To UBound($NewArray) - 1

$NewArray[$x] = $array2[$x] & $array1[$x]

Next

Return $NewArray

EndFunc ;==>PrePendMerge

Thanks for commenting your script, it made a world of difference.

Thanks for your patience and quick help!

Link to comment
Share on other sites

yes, mine does work.

Here is my exact script:

#include <File.au3>
Dim $Lines1, $Lines2, $String = ""
_FileReadToArray(@DesktopDir&"\Test1.txt", $Lines1) ; this should be the "random data" original file
_FileReadToArray(@DesktopDir&"\Test2.txt", $Lines2) ; this should be the file with the 5 columns

$Result = PrePendMerge($Lines1, $Lines2)
For $x = 1 To UBound($Result) - 1
    $String &= $Result[$x] & @CRLF
Next
$File = FileOpen(@DesktopDir & "\Completed Prepended File.txt", 2)
FileWrite($File, $String)
FileClose($File)
Func PrePendMerge($array1, $array2)
    ;Add each element of $array2 in front of each element in array1
    If UBound($array1) <> UBound($array2) Then Return 0 ;ERROR
    Dim $NewArray[UBound($array1)]
    For $x = 0 To UBound($NewArray) - 1
        $NewArray[$x] = $array2[$x] & $array1[$x]
    Next
    Return $NewArray
EndFunc   ;==>PrePendMerge

These are my 2 test files, located on my desktop:

Test1.txt

RANDOMDATA1|RANDOMDATA12|DANDOMDATA13
RANDOMDATA1|RANDOMDATA12|DANDOMDATA13n
RANDOMDATA1|RANDOMDATA12|DANDOMDATA13a
RANDOMDATA1|RANDOMDATA12|DANDOMDATA13s
RANDOMDATA1|RANDOMDATA12|DANDOMDATA13f
RANDOMDATA1|RANDOMDATA12|DANDOMDATA13df
RANDOMDATA1|RANDOMDATA12|DANDOMDATA13d
RANDOMDATA1|RANDOMDATA12|DANDOMDATA13hd
RANDOMDATA1|RANDOMDATA12|DANDOMDATA13gf
RANDOMDATA1|RANDOMDATA12|DANDOMDATA13d
RANDOMDATA1|RANDOMDATA12|DANDOMDATA13dfg
RANDOMDATA1|RANDOMDATA12|DANDOMDATA13fg

test2.txt

APPEND1|
APPEND2|
APPEND3|
APPEND4|
APPEND5|
APPEND6|
APPEND7|
APPEND8|
APPEND9|
APPEND0|
APPEND10|
APPEND22|

======================

My resulting file: As expected:

APPEND1|RANDOMDATA1|RANDOMDATA12|DANDOMDATA13
APPEND2|RANDOMDATA1|RANDOMDATA12|DANDOMDATA13n
APPEND3|RANDOMDATA1|RANDOMDATA12|DANDOMDATA13a
APPEND4|RANDOMDATA1|RANDOMDATA12|DANDOMDATA13s
APPEND5|RANDOMDATA1|RANDOMDATA12|DANDOMDATA13f
APPEND6|RANDOMDATA1|RANDOMDATA12|DANDOMDATA13df
APPEND7|RANDOMDATA1|RANDOMDATA12|DANDOMDATA13d
APPEND8|RANDOMDATA1|RANDOMDATA12|DANDOMDATA13hd
APPEND9|RANDOMDATA1|RANDOMDATA12|DANDOMDATA13gf
APPEND0|RANDOMDATA1|RANDOMDATA12|DANDOMDATA13d
APPEND10|RANDOMDATA1|RANDOMDATA12|DANDOMDATA13dfg
APPEND22|RANDOMDATA1|RANDOMDATA12|DANDOMDATA13fg
Link to comment
Share on other sites

Paulie,

Indeed! One of the files had an extra carriage return. Deleted it and your script ran flawlessly.

Thanks for your expertise (and hand-holding) to help me get this accomplished.

Final question, you mentioned using _ArrayDisplay. I inserted that (as well as #include <Array.au3>) in various places and the ListView did not appear with _ArrayDisplay on top of EndFunc. Below EndFunc, the script blew up stating: WARNING: $NewArray: possibly used before declaration.

Where might _ArrayDisplay be best inserted? This will help me, and perhaps, others in the community better understand arrays.

Thanks, again!

Link to comment
Share on other sites

Paulie,

Indeed! One of the files had an extra carriage return. Deleted it and your script ran flawlessly.

Thanks for your expertise (and hand-holding) to help me get this accomplished.

Final question, you mentioned using _ArrayDisplay. I inserted that (as well as #include <Array.au3>) in various places and the ListView did not appear with _ArrayDisplay on top of EndFunc. Below EndFunc, the script blew up stating: WARNING: $NewArray: possibly used before declaration.

Where might _ArrayDisplay be best inserted? This will help me, and perhaps, others in the community better understand arrays.

Thanks, again!

Haha! no problem, I should have picked up on that being the error the first time :P

If I were to debug my code with _ArrayDisplay It would probably look like this:

#include <File.au3>
#include <array.au3>
Dim $Lines1, $Lines2, $String = ""
_FileReadToArray(@DesktopDir&"\Test1.txt", $Lines1) ; this should be the "random data" original file
_FileReadToArray(@DesktopDir&"\Test2.txt", $Lines2) ; this should be the file with the 5 columns

_ArrayDisplay($Lines1, "Lines1 - Post FileReadToArray")
_ArrayDisplay($Lines2, "Lines2 - Post FileReadToArray")

$Result = PrePendMerge($Lines1, $Lines2)
_ArrayDisplay($Result, "Result of Func PrePendMerge")
For $x = 1 To UBound($Result) - 1
    $String &= $Result[$x] & @CRLF
Next
$File = FileOpen(@DesktopDir & "\Completed Prepended File.txt", 2)
FileWrite($File, $String)
FileClose($File)


Func PrePendMerge($array1, $array2)
    ;Add each element of $array2 in front of each element in array1
    If UBound($array1) <> UBound($array2) Then Return 0 ;ERROR
    Dim $NewArray[UBound($array1)]
    For $x = 0 To UBound($NewArray) - 1
        $NewArray[$x] = $array2[$x] & $array1[$x]
    Next
    _ArrayDisplay($NewArray, "NewArray - End Of Func PrePendMerge")
    Return $NewArray
EndFunc   ;==>PrePendMerge
Link to comment
Share on other sites

Paulie,

Thanks, again!

New AutoIT user, first time poster, and it was a real treat to have you as a coach.

From what I've read on the forum, arrays are very complicated. In changing how to resolve this matter in the easiest/simplest way, light was shed on arrays. Which I think is a great starting point for those new to AutoIT. And for the betterment of the AutoIT community, I might add.

Thanks for sharing your expertise, and doing so quickly, with only one agenda ... to help others.

All my best!

Link to comment
Share on other sites

Paulie,

Thanks, again!

New AutoIT user, first time poster, and it was a real treat to have you as a coach.

From what I've read on the forum, arrays are very complicated. In changing how to resolve this matter in the easiest/simplest way, light was shed on arrays. Which I think is a great starting point for those new to AutoIT. And for the betterment of the AutoIT community, I might add.

Thanks for sharing your expertise, and doing so quickly, with only one agenda ... to help others.

All my best!

Aww :P Thanks!

I'm glad I could help! And for the record, I'm pretty sure you are one of the nicest people I've ever helped, rarely do people give such high complements :P . Best of luck! and should you continue to use AutoIt, please never hesitate to drop me a PM/Instant message(see my profile) if you think there is anything I might be able to help you with :o

What can I say? I'ma sucker for flattery :D:zorro:

Edited by Paulie
Link to comment
Share on other sites

Sorry to put this post at the top again but here is a link I wanted to add because it was so well thought out ... to me anyway.

This guy goes to great lengths in figuring this out and posts it all out. Not that I can understand it all but I get the concepts, and the gist of what he was doing was pretty cool.

Later

Be open minded but not gullible.A hammer sees everything as a nail ... so don't be A tool ... be many tools.

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...