Jump to content

How to split text file having paras using empty lines as limiter


Recommended Posts

Hi all,
can i get solution for this???

i want to split paragraphs with empty lines as Limiter to split.
How can i do this??

StringSplit ( $Read , "      " ,$STR_ENTIRESPLIT )

this didn't work
and this too didn't work
$Sp = StringSplit($Read , @CRLF , 1)

any solutions plzz

attached reference txt file

Spaces between paras are not same....but i have to split them  using empty space as limiter

Link to comment
Share on other sites

I searched a lot but couldn't find the 'attached reference txt file'
So this example uses a simple multi-paragraphs string

#Include <Array.au3>

$txt = "AutoIt v3 is a freeware BASIC-like scripting language designed for automating the Windows GUI and general scripting." & _ 
@crlf & "            " & @crlf & @crlf & _ 
"It uses a combination of simulated keystrokes, mouse movement and window/control manipulation in order to automate tasks in a way not possible or reliable with other languages (e.g. VBScript and SendKeys). " & _ 
@crlf & "                  " & @crlf & _ 
"AutoIt is also very small, self-contained and will run on all versions of Windows out-of-the-box with no annoying runtimes required!         end"  

Msgbox(0,"", $txt)

$res = StringRegExp($txt, '(?s)(.+?)(?:\R\s*\R|$)', 3)
_ArrayDisplay($res)

 

Link to comment
Share on other sites

I found the attachment:
New Text Document (2).txt

#   Name   Score
1   ssss   10
2   aaaa   20
3   hhhh   30
4   qqqq   40
<5>1 wwww   50
<6>2 ffff   60 
<7>3 qqqq   10
<8>4 rrrr   20 
<9>5 yyyy   30 
<10>6 nnnn   40
<11>7 llll   90
<12>7 iiii   80 


1   ssss   10
2   aaaa   20
3   hhhh   30
4   qqqq   40
<5>1 wwww   50
<6>2 ffff   60 
<7>3 qqqq   10
<8>4 rrrr   20 
<9>5 yyyy   30 
<10>6 nnnn   40
<11>7 llll   90
<12>7 iiii   80 



1   ssss   10
2   aaaa   20
3   hhhh   30
4   qqqq   40
<5>1 wwww   50
<6>2 ffff   60 
<7>3 qqqq   10
<8>4 rrrr   20 
<9>5 yyyy   30 
<10>6 nnnn   40
<11>7 llll   90
<12>7 iiii   80 


1   ssss   10
2   aaaa   20
3   hhhh   30
4   qqqq   40
<5>1 wwww   50
<6>2 ffff   60 
<7>3 qqqq   10
<8>4 rrrr   20 
<9>5 yyyy   30 
<10>6 nnnn   40
<11>7 llll   90
<12>7 iiii   80 


1   ssss   10
2   aaaa   20
3   hhhh   30
4   qqqq   40
<5>1 wwww   50
<6>2 ffff   60 
<7>3 qqqq   10
<8>4 rrrr   20 
<9>5 yyyy   30 
<10>6 nnnn   40
<11>7 llll   90
<12>7 iiii   80 


1   ssss   10
2   aaaa   20
3   hhhh   30
4   qqqq   40
<5>1 wwww   50
<6>2 ffff   60 
<7>3 qqqq   10
<8>4 rrrr   20 
<9>5 yyyy   30 
<10>6 nnnn   40
<11>7 llll   90
<12>7 iiii   80 


1   ssss   10
2   aaaa   20
3   hhhh   30
4   qqqq   40
<5>1 wwww   50
<6>2 ffff   60 
<7>3 qqqq   10
<8>4 rrrr   20 
<9>5 yyyy   30 
<10>6 nnnn   40
<11>7 llll   90
<12>7 iiii   80

 

Edited by VIP
post attachment!

Regards,
 

Link to comment
Share on other sites

for all paragraphs :

#include <Array.au3>
Global $iFilePath = @ScriptDir & "\New Text Document (2).txt"
Global $iFileContent = FileRead($iFilePath)
Global $iArrayParagraphs = StringSplit($iFileContent, @CRLF & @CRLF & @CRLF, 1)

For $x = 1 To $iArrayParagraphs[0]
    Local $iArrayLine = StringSplit($iArrayParagraphs[$x], @CRLF, 1)
    Local $Array3D[UBound($iArrayLine) + 1][3]

    For $i = 0 To UBound($iArrayLine) - 1
        If StringStripWS($iArrayLine[$i], 8) <> "" Then
            Local $XXX = $iArrayLine[$i]
            While StringInStr($XXX, " " & " ")
                $XXX = StringReplace($XXX, " " & " ", " ")
            WEnd
            ConsoleWrite($XXX & @CRLF)
            Local $XNXX = StringSplit($XXX, " ", 2)
            If UBound($XNXX) > 2 Then
                $Array3D[$i][0] = $XNXX[0]
                $Array3D[$i][1] = $XNXX[1]
                $Array3D[$i][2] = $XNXX[2]
            EndIf
        EndIf
    Next

    _ArrayDisplay($Array3D)

Next

for 1  paragraphs :

#include <Array.au3>
Global $iFilePath = @ScriptDir & "\New Text Document (2).txt"
Global $iArrayLine = FileReadToArray($iFilePath)
Global $Array3D[UBound($iArrayLine) + 1][3]

For $i = 0 To UBound($iArrayLine) - 1
    If StringStripWS($iArrayLine[$i], 8) <> "" Then
        Local $XXX = $iArrayLine[$i]
        While StringInStr($XXX, " " & " ")
            $XXX = StringReplace($XXX, " " & " ", " ")
        WEnd
        ConsoleWrite($XXX & @CRLF)
        Local $XNXX = StringSplit($XXX, " ", 2)
        If UBound($XNXX) > 2 Then
            $Array3D[$i][0] = $XNXX[0]
            $Array3D[$i][1] = $XNXX[1]
            $Array3D[$i][2] = $XNXX[2]
        EndIf
    EndIf
Next

_ArrayDisplay($Array3D)

 

Edited by VIP
PS: Do not forget to like if your problem is solved 

Regards,
 

Link to comment
Share on other sites

For the fun ... VIP do you like it ?  :D

#Include <Array.au3>

$txt = FileRead(@ScriptDir & "\New Text Document (2).txt")

$res = StringRegExp($txt, '(?m)^<?(\d+)>?\d?\h+(\S+)\h+(\d+)\h*$', 3)
Local $n = UBound($res), $k = 3, $res2D[Ceiling($n/$k)][$k]
For $i = 0 To $n - 1
    $res2D[Int($i / $k)][Mod($i, $k)] = $res[$i]
Next
_ArrayDisplay($res2D)

 

Link to comment
Share on other sites

On 8/14/2017 at 8:53 PM, VIP said:

for all paragraphs :

#include <Array.au3>
Global $iFilePath = @ScriptDir & "\New Text Document (2).txt"
Global $iFileContent = FileRead($iFilePath)
Global $iArrayParagraphs = StringSplit($iFileContent, @CRLF & @CRLF & @CRLF, 1)

For $x = 1 To $iArrayParagraphs[0]
    Local $iArrayLine = StringSplit($iArrayParagraphs[$x], @CRLF, 1)
    Local $Array3D[UBound($iArrayLine) + 1][3]

    For $i = 0 To UBound($iArrayLine) - 1
        If StringStripWS($iArrayLine[$i], 8) <> "" Then
            Local $XXX = $iArrayLine[$i]
            While StringInStr($XXX, " " & " ")
                $XXX = StringReplace($XXX, " " & " ", " ")
            WEnd
            ConsoleWrite($XXX & @CRLF)
            Local $XNXX = StringSplit($XXX, " ", 2)
            If UBound($XNXX) > 2 Then
                $Array3D[$i][0] = $XNXX[0]
                $Array3D[$i][1] = $XNXX[1]
                $Array3D[$i][2] = $XNXX[2]
            EndIf
        EndIf
    Next

    _ArrayDisplay($Array3D)

Next

for 1  paragraphs :

#include <Array.au3>
Global $iFilePath = @ScriptDir & "\New Text Document (2).txt"
Global $iArrayLine = FileReadToArray($iFilePath)
Global $Array3D[UBound($iArrayLine) + 1][3]

For $i = 0 To UBound($iArrayLine) - 1
    If StringStripWS($iArrayLine[$i], 8) <> "" Then
        Local $XXX = $iArrayLine[$i]
        While StringInStr($XXX, " " & " ")
            $XXX = StringReplace($XXX, " " & " ", " ")
        WEnd
        ConsoleWrite($XXX & @CRLF)
        Local $XNXX = StringSplit($XXX, " ", 2)
        If UBound($XNXX) > 2 Then
            $Array3D[$i][0] = $XNXX[0]
            $Array3D[$i][1] = $XNXX[1]
            $Array3D[$i][2] = $XNXX[2]
        EndIf
    EndIf
Next

_ArrayDisplay($Array3D)

 

Can u help me in understanding the logic behind it??

I am unable to get any output??

Link to comment
Share on other sites

On 8/15/2017 at 1:24 AM, mikell said:

For the fun ... VIP do you like it ?  :D

#Include <Array.au3>

$txt = FileRead(@ScriptDir & "\New Text Document (2).txt")

$res = StringRegExp($txt, '(?m)^<?(\d+)>?\d?\h+(\S+)\h+(\d+)\h*$', 3)
Local $n = UBound($res), $k = 3, $res2D[Ceiling($n/$k)][$k]
For $i = 0 To $n - 1
    $res2D[Int($i / $k)][Mod($i, $k)] = $res[$i]
Next
_ArrayDisplay($res2D)

 

can u help me understand the logic behind it??

 

Link to comment
Share on other sites

5 hours ago, sree161 said:

can u help me understand the logic behind it??

Sorry. Here is the logic :
My script assumes that the 3 things you want to get in each line are : number, name, score
Meaning, in this line  "4   qqqq   40", get  "4, qqqq, 40"
and in this one :  "<5>1 wwww   50", get  "5, wwww, 50"
So I used a regular expression to parse each concerned line, get the wanted parts and store them in a 1D array ($res)
The For/Next loop after is used to order the parts nicely by building a 2D array ($res2D) with a row per line and 3 columns

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