Jump to content

Recommended Posts

Posted

Could someone point to the correct logic/process to do this.   I have a text file and within that file I have blocks of text I would like to format.  

Beginning block of text

88305 data data

Line 2  data

Line 3 data data data

Line 4 data data

Line 5 data

86244 data data

Line 2  data

Line 3 data data data

Line 4 data data

Line 5 data

End of block of text. 

Results after formatting

Beginning block of text

88305 data data Line 2  data Line 3 data data data Line 4 data data Line 5 data

86244 data data Line 2  data Line 3 data data data Line 4 data data Line 5 data

End of block of text. 

 

So within each block of text the data can vary.   Some would only have a resulting finished format of one line and others have 2 or more.  The 5 digit code(88305 and this varies) is always at the beginning of a line.  

Not looking for anyone to write the code just get me pointed to the correct logic to do this task.   Anything would help just really kind of brain cramping on this one.  Thank you

 

Posted

Pseudocode (untested):

Open InputFile
Open OutputFile
While
    Read line from InputFile into ReadBuffer
    If error Then ; End-of-file reached
        If WriteBuffer <> "" Then Write WriteBuffer to OutputFile ; Write last block to output file
        ExitLoop
    EndIf
    If ReadBuffer starts with "Line " Then 
        concat ReadBuffer to WriteBuffer
    Else  ; Start new block
        If WriteBuffer <> "" Then Write WriteBuffer to Output file ; Write previous block to output file
        WriteBuffer = ReadBuffer
    EndIf
WEnd
Close InputFile
Close OutputFile

 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

  • Moderators
Posted

xcaliber13,

My suggestion (I added a second section just to prove the concept):

#include <Array.au3> ; Just for display

$sStart = "Beginning block of text "
$sEnd = "End of block of text"

; Simulate reading file into an array
Global $aText[] = ["Beginning block of text ", _
                    "88305 data data ", _
                    "Line 2  data ", _
                    "Line 3 data data data ", _
                    "Line 4 data data ", _
                    "Line 5 data ", _
                    "86244 data data ", _
                    "Line 2  data ", _
                    "Line 3 data data data ", _
                    "Line 4 data data ", _
                    "Line 5 data ", _
                    "End of block of text", _
                    "Beginning block of text ", _
                    "89345 data data ", _
                    "Line 2  data ", _
                    "Line 3 data data data ", _
                    "Line 4 data data ", _
                    "Line 5 data ", _
                    "87654 data data ", _
                    "Line 2  data ", _
                    "Line 3 data data data ", _
                    "Line 4 data data ", _
                    "Line 5 data ", _
                    "End of block of text"]

; Just for display
 _ArrayDisplay($aText, "", Default, 8)

$sData = ""

For $i = 0 To UBound($aText) - 1

    $sLine = $aText[$i]

    If $sLine = $sStart Or $sLine = $sEnd Then
        $sData &= @CRLF & $sLine
    Else
        If StringLeft($sLine, 4) <> "Line" Then
            $sData &= @CRLF & $sLine
        Else
            $sData &= $sLine
        EndIf
    EndIf

Next

ConsoleWrite($sData & @CRLF)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Posted

I really am trying to learn regex.   I have been using www.myregextester.com to test different regex patterns.   But I am having an issue when trying to use a successful pattern that I tested at  myregextester.com  in AutoIt.   I have a text file that I read to an array,  then search the array for the pattern but it does not find the correct string.  I am trying to find every  line that begins with 5 numbers .  Here is the code I have been trying to make work.   My  _ArrayFindAll is not returning the correct lines from the text file.  Is my regex pattern wrong?  Or should I be doing this a different way?  Thank you for any help.

Global $avArray
_FileReadToArray('c:\Temp\temp.txt', $avArray, $FRTA_NOCOUNT)
;_ArrayDisplay($avArray)

$search = StringRegExp($avArray, '(?ms)^\d{5}', 3)
Local $iIndex =_ArrayFindAll($avArray, $search, 0, 0, 0, 3)
_ArrayDisplay($iIndex)
for $i = 0 to UBound($iIndex)-1 step 1
    $avArray[$iIndex[$i]] = "CPT CODE"&$avArray[$iIndex[$i]]

Next
_ArrayDisplay($avArray)

 

Posted

Don't know? Maybe something like this?

 

#include <File.au3>
#include <Array.au3>
#include <FileConstants.au3>
Global $avArray
_FileReadToArray('temp.txt', $avArray, $FRTA_NOCOUNT)
For $i = UBound($avArray) - 1 To 0 Step -1
    ConsoleWrite($i & " " & $avArray[$i] & @crlf)
    If Not StringRegExp($avArray[$i], '(?s)^\d{5}', $STR_REGEXPMATCH) Then
;~      _ArrayDelete($avArray, $i) ; optional
    Else
        $avArray[$i] = "CPT CODE" & $avArray[$i]
    EndIf
Next
_ArrayDisplay($avArray)

 

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Posted (edited)

@xcaliber13
There are many ways to do this:

#include <Array.au3>
#include <StringConstants.au3>

Global $strString = "88305 data data" & @CRLF & _
                    "Line 2  data" & @CRLF & _
                    "Line 3 data data data" & @CRLF & _
                    "Line 4 data data" & @CRLF & _
                    "Line 5 data" & @CRLF & _
                    "86244 data data" & @CRLF & _
                    "Line 2  data" & @CRLF & _
                    "Line 3 data data data 56789" & @CRLF & _
                    "Line 4 data data" & @CRLF & _
                    "Line 5 data" & @CRLF & _
                    "End of block of text. ", _
      $arrResult[0], _
      $strResult = ""
      
MsgBox(0, 'Before:', $strString)

_ArrayAdd($arrResult, StringRegExpReplace($strString, '(?m)(?!\A)^(\d{5})', '|$1'))

For $i = 0 To UBound($arrResult) - 1 Step 1
    $strResult &= StringStripWS(StringReplace($arrResult[$i], @CRLF, " "), BitOR($STR_STRIPLEADING, $STR_STRIPTRAILING, $STR_STRIPSPACES)) & @CRLF & ($i < UBound($arrResult) - 1 ? "---------------" & @CRLF : "")
Next

MsgBox(0, 'After:', $strResult)

Bet @mikell and @jchd will join the party soon.
By the way, both @water and @Melba23 provided you a solution without receiving by you any kind of reply; that's not so much respectful in their comparisons :)

Edited by FrancescoDiMuro

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Posted
2 hours ago, xcaliber13 said:

I am trying to find every  line that begins with 5 numbers

I don't understand the reason why you overcomplicate. While StringRegExp does the job by itself, you shouldn't need anything more

Global $strString = "88305 data data" & @CRLF & _
                    "Line 2  data" & @CRLF & _
                    "Line 3 data data data" & @CRLF & _
                    "Line 4 data data" & @CRLF & _
                    "Line 5 data" & @CRLF & _
                    "86244 data data" & @CRLF & _
                    "Line 2  data" & @CRLF & _
                    "Line 3 data data data 56789" & @CRLF & _
                    "Line 4 data data" & @CRLF & _
                    "Line 5 data" & @CRLF & _
                    "End of block of text. "
      
$res = StringRegExp($strString, '(?m)^\d{5}.*$', 3)
_ArrayDisplay($res)

BTW StringRegExp acts on strings, not arrays. This is not good :

_FileReadToArray('c:\Temp\temp.txt', $avArray, $FRTA_NOCOUNT)
;_ArrayDisplay($avArray)

$search = StringRegExp($avArray, '(?ms)^\d{5}', 3)

 

  • xcaliber13 changed the title to REG Ex problem SOLVED
  • xcaliber13 changed the title to Text formatting help SOLVED
Posted (edited)
1 hour ago, mikell said:

While StringRegExp does the job by itself

Little hairy cat (:muttley:), if you would have paid more attention to the OP request, you would have noticed that he was asking to merge the lines where the five digits number were encountered, and the ones following that too, until a new five digits number was encountered, so, the script you posted is not currently doing what the OP requested :D

Edited by FrancescoDiMuro

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Posted

Merge ? where on the earth did you see such a mention ?  :blink:
"I am trying to find every  line that begins with 5 numbers"  is clear, and their code ends with "_ArrayDisplay($avArray)" so the expected result is an array (probably)
Oh yes, to keep as close as possible to the initial post/question I should have written

$avArray = StringRegExp($file, 'db\.(?|server.name|database.name|instance.name|port)=(\N*)', 3)
for $i = 0 to UBound($avArray)-1
    $avArray[$i] = "CPT CODE"&$avArray[$i]
Next
_ArrayDisplay($avArray)

:)

Posted

I don't care of other threads. I answered to a question asked in post #1 of the current thread. If this answer doesn't fit so the question was wrong, period.

  • Moderators
Posted

@xcaliber13 you have been around long enough to know better than this multi-post spamming because you want an answer now. Please stick to one thread per topic in the future, as you can see what kind of confusion you caused.

 

3 minutes ago, FrancescoDiMuro said:

This answer was referred to this thread, and not to this one, so, low your fur and just read carefully :)

Not sure if you think you're being humorous (you're not), but you can't expect someone to come in and answer based off there being multiple threads, each with half the information you would need. So, I would suggest, rather than you telling an MVP to back up and take a moment, that you take your own advice.

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Posted
Just now, JLogan3o13 said:

but you can't expect someone to come in and answer based off there being multiple threads

No, that's why I pointed him to let him know there were another post in which the user were explaining much better what he was asking, providing an example too.
The fact is that he is (if you follow the "this" stream) referring to another thread which were not linked in any way to these ones.
And then, MVP or not, just a little "Hey, read carefully before answering" :)

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

  • Moderators
Posted

Your statements to him, perhaps through translation, come across as condescending. As we have discussed in the past, your willingness to help is great - we could do without some of the side commentary.

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Posted

Try this example.

#cs
Could someone point to the correct logic/process to do this.   I have a text file and within that file I have blocks of text I would like to format.
Beginning block of text
88305 data data
Line 2  data
Line 3 data data data
Line 4 data data
Line 5 data
86244 data data
Line 7  data
Line 8 data data data
Line 9 data data
Line 10 data
86660 data data
Line 12  data
Line 13 data data data
Line 14 data data
Line 15 data
End of block of text
Text after first block.

Beginning block of text
88333 data data
Line 20  data
Line 21 data data data
Line 22 data data
Line 23 data
End of block of text
Text after second block.
#ce

Local $sText = StringRegExpReplace(FileRead(@ScriptFullPath), "(?is)^.*?#cs\s+|\s+#ce.*$", "") ; Get test text from comment block in this script.
Local $sStart = "Beginning block of text"
Local $sEnd = "End of block of text"

ConsoleWrite(Execute("'" & StringRegExpReplace($sText, "(?si)(.*?" & $sStart & ")(.+?)(\R" & $sEnd & ".*?)", "$1' & StringRegExpReplace('$2', '\\R(?!\\d{5})', ' ') & '$3") & "'") & @CRLF)

 

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.
×
×
  • Create New...