Jump to content
Sign in to follow this  
nitekram

Add lines to main line, until next main line?

Recommended Posts

nitekram

I have a file that was pulled from an excel sheet - CVS. I need to add all the lines between the first all lines until the next format - I know clear as mud!

Each main line begins with the following format - 2 letters (abbr of the US State), followed by 3 numbers - the part of the US State.

So Maryland would be MD, and Rockville would be 123 - total format MD123 - I need all the lines including this line, until I reach the next location MD124.

The Excel file was chopped up really good, nothing I have done so far has worked...what I am hoping to do is to grab all the lines between the two locations - some lines include everything needed in the line and some lines have 3-5 lines between locations. - but I need it to be all one line, so that I can grab the info from that line and search it - I do not think the following code is even a good starting point, but this is what I have tried, plus many more - I am at a loss, as I have been trying to do this for two days.

Any help or additional info needed - thanks for your time.

While 1 And $countIT <> 25
  $NewLineNumber = 0
  $countIT += 1
  $line = FileReadLine($File, $iLineNumber) ; read the first line, and continue until end
  If @error = -1 Then
   ;MsgBox('','End of File',$Device & ' Line=' & $iLineNumber)
   ExitLoop
  EndIf
  If $line = '' Then
   $iLineNumber += 1
   ContinueLoop
  Else
   Do
    $sLetters_2 = StringLeft($line, 2)
    $sNumbers_3 = StringMid($line, 3, 3)
    ;
    $sNumbers_3 = Number($sNumbers_3)
    ;
    ConsoleWrite('--> EXISTING LINE ' & $sLetters_2 & $sNumbers_3 & @CRLF)
    ;
    If (IsString($sLetters_2) = True And StringLen($sLetters_2) = 2) And (IsNumber($sNumbers_3) = True And StringLen($sNumbers_3) = 3) Then
     $AddLine = True
    Else
     ConsoleWrite($line & ' -->> does not meet requirement ' & $sLetters_2 & $sNumbers_3 & @CRLF)
     #cs
      
      MsgBox('', '', 'Is String = ' & IsString($sLetters_2) _
      & @CRLF & 'StringLen($sLetters_2) ' & StringLen($sLetters_2) & ' <> 2' _
      & @CRLF & 'IsNumber($sNumbers_3) ' & IsNumber($sNumbers_3) _
      & @CRLF & 'StringLen($sNumbers_3) ' & StringLen($sNumbers_3) & ' <> 3')
     #ce
     $NewLineNumber += 1
     $sTempLine = FileReadLine($File, $iLineNumber + $NewLineNumber)
     If @error = -1 Then
      ;MsgBox('','End of File',$Device & ' Line=' & $iLineNumber)
      ExitLoop 2
     EndIf
     ConsoleWrite('TempLINE ' & $sTempLine & @CRLF)
     $line &= $sTempLine
     ConsoleWrite('LINE ' & $line & @CRLF)
    EndIf

   Until $AddLine
   $iLineNumber += $NewLineNumber
  EndIf
  ConsoleWrite('1 need to search on this line ' & $line & @CRLF)
  WEnd
If $sLocation = '' Then MsgBox('', 'Not Found', 'Location was not found for device ', 1)
FileClose($File)

edit forgot to end my tag

Edited by nitekram

All by me:

"Sometimes you have to go back to where you started, to get to where you want to go." 

"Everybody catches up with everyone, eventually" 

"As you teach others, you are really teaching yourself."

From my dad

"Do not worry about yesterday, as the only thing that you can control is tomorrow."

 

WindowsError.gif

WIKI | Tabs; | Arrays; | Strings | Wiki Arrays | How to ask a Question | Forum Search | FAQ | Tutorials | Original FAQ | ONLINE HELP | UDF's Wiki | AutoIt PDF

AutoIt Snippets | Multple Guis | Interrupting a running function | Another Send

StringRegExp | StringRegExp Help | RegEXTester | REG TUTOR | Reg TUTOT 2

AutoItSetOption | Macros | AutoIt Snippets | Wrapper | Autoit  Docs

SCITE | SciteJump | BB | MyTopics | Programming | UDFs | AutoIt 123 | UDFs Form | UDF

Learning to script | Tutorials | Documentation | IE.AU3 | Games? | FreeSoftware | Path_Online | Core Language

Programming Tips

Excel Changes

ControlHover.UDF

GDI_Plus

Draw_On_Screen

GDI Basics

GDI_More_Basics

GDI Rotate

GDI Graph

GDI  CheckExistingItems

GDI Trajectory

Replace $ghGDIPDll with $__g_hGDIPDll

DLL 101?

Array via Object

GDI Swimlane

GDI Plus French 101 Site

GDI Examples UEZ

GDI Basic Clock

GDI Detection

Ternary operator

Share this post


Link to post
Share on other sites
kylomas

nitekram,

Can you post the file (or a representative portion of it)?

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
nitekram

just a small sample - file is about 100kb, but the general idea is that each main line starts the same way with 2 chars, followed by 3 numbers and a lot of junk and then the start of the next main line - EACH main line starts the same way, just have a lot of stuff in the middle, sometime it is all on the same line as the format, other times there are 1 - 10 lines of data smashed between the two main lines = i would like all of it into one line, do not care if the line is a variable I can search or an index of an array - as long as i get it all in the same line

So in this small sample there are 6 main lines - so at the end of the day even though there might be twenty lines in the example - i end up with 6 lines of data - oh and the two chars and 3 numbers all end with a comma

MD123,

MD124,

MD125,

AK111,

AL210,

AL001,

Thanks for your time...

[file]

MD123,,,,,,test one, test two

, test three,,Maryland, Rockville, (),

MD124,,,IP 1.1.1.1, Maryland,Gaithersburg, USA (gmt),

MD125,,,t one, test two

AK111,,,,,,somewhere AK,Alaska (AKST)(AKDT),

AL210,al210wanrtr01 IP: 1.1.21.1,XXX On-Call person (name of file),"first last:

.555.555.5555 Office

555.555.5555 Mobile","first last

555.555.5555 ",IDS,"site name (xxx Site)

123 any Drive

wherever, AL 5555-5555",Central (CST)(CDT),

,,,,"first last

555.555.5555",,,,

,,,,"first last (a Manager)

555.555.5555

555.555.5555 (cell) ",,,,

,,,,"first last (a Tech.)

555.555.5555 ",,,,

,,,,"Guard Station (from first last)

555.555.5555 ",,,,

AL001,xxx IP:1.1.1.1,xxx On-Call person (a file),"first last:

555.555.5555 Office

555.555.5555 Mobile",,xxx,"any streeet any square Suite 1

some city AL 35801",Central (CST)(CDT),

,,,,,,,,

[/file]

Edited by nitekram

All by me:

"Sometimes you have to go back to where you started, to get to where you want to go." 

"Everybody catches up with everyone, eventually" 

"As you teach others, you are really teaching yourself."

From my dad

"Do not worry about yesterday, as the only thing that you can control is tomorrow."

 

WindowsError.gif

WIKI | Tabs; | Arrays; | Strings | Wiki Arrays | How to ask a Question | Forum Search | FAQ | Tutorials | Original FAQ | ONLINE HELP | UDF's Wiki | AutoIt PDF

AutoIt Snippets | Multple Guis | Interrupting a running function | Another Send

StringRegExp | StringRegExp Help | RegEXTester | REG TUTOR | Reg TUTOT 2

AutoItSetOption | Macros | AutoIt Snippets | Wrapper | Autoit  Docs

SCITE | SciteJump | BB | MyTopics | Programming | UDFs | AutoIt 123 | UDFs Form | UDF

Learning to script | Tutorials | Documentation | IE.AU3 | Games? | FreeSoftware | Path_Online | Core Language

Programming Tips

Excel Changes

ControlHover.UDF

GDI_Plus

Draw_On_Screen

GDI Basics

GDI_More_Basics

GDI Rotate

GDI Graph

GDI  CheckExistingItems

GDI Trajectory

Replace $ghGDIPDll with $__g_hGDIPDll

DLL 101?

Array via Object

GDI Swimlane

GDI Plus French 101 Site

GDI Examples UEZ

GDI Basic Clock

GDI Detection

Ternary operator

Share this post


Link to post
Share on other sites
kylomas

nitekram,

Give this a whirl...

#include <array.au3>


local $a10 = stringsplit(fileread(@scriptdir & '\nitekram.txt'),@crlf,1), $out

_arraydisplay($a10)

for $1 = 1 to $a10[0]
    switch stringregexp($a10[$1],'^\w\w\d\d\d.*',0)
        case 1
            $out &= @crlf & $a10[$1]
        case Else
            $out &= ' ' & $a10[$1]
    EndSwitch
Next

ConsoleWrite($out & @LF)

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
nitekram

I cannot believe you put that together in such a short time. It does work, but now I am not sure if the many ways I tried to get it to work, if one of them was not working the whole time.

When I look at the output in scite, it still looks jumbled, and I was about to show you that your code did not work, but when I pasted it into a notepad window - all the lines were there???

So, what is the issue with scite not showing the whole line and breaking it up into little chunks? Simmilar to what I pasted the example file above?

EDIT - Oh, any my fault - I did not thank you, thanks!!!

Edit, sorry I have a question. I do not really understand how this was done...I see your ForNext loop, but if there was more then one line, how are you doing it...Maybe with the regexp has magic - can you explain? I do not know that function so take it easy.

Edited by nitekram

All by me:

"Sometimes you have to go back to where you started, to get to where you want to go." 

"Everybody catches up with everyone, eventually" 

"As you teach others, you are really teaching yourself."

From my dad

"Do not worry about yesterday, as the only thing that you can control is tomorrow."

 

WindowsError.gif

WIKI | Tabs; | Arrays; | Strings | Wiki Arrays | How to ask a Question | Forum Search | FAQ | Tutorials | Original FAQ | ONLINE HELP | UDF's Wiki | AutoIt PDF

AutoIt Snippets | Multple Guis | Interrupting a running function | Another Send

StringRegExp | StringRegExp Help | RegEXTester | REG TUTOR | Reg TUTOT 2

AutoItSetOption | Macros | AutoIt Snippets | Wrapper | Autoit  Docs

SCITE | SciteJump | BB | MyTopics | Programming | UDFs | AutoIt 123 | UDFs Form | UDF

Learning to script | Tutorials | Documentation | IE.AU3 | Games? | FreeSoftware | Path_Online | Core Language

Programming Tips

Excel Changes

ControlHover.UDF

GDI_Plus

Draw_On_Screen

GDI Basics

GDI_More_Basics

GDI Rotate

GDI Graph

GDI  CheckExistingItems

GDI Trajectory

Replace $ghGDIPDll with $__g_hGDIPDll

DLL 101?

Array via Object

GDI Swimlane

GDI Plus French 101 Site

GDI Examples UEZ

GDI Basic Clock

GDI Detection

Ternary operator

Share this post


Link to post
Share on other sites
kylomas

Curious, all the lines show up for me in the right format...

Your welcome,

kylomas

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
kylomas

nitekram,

SRE version:

local $a10 = stringregexp(fileread(@scriptdir & '\nitekram.txt'),'(?si)(\w\w\d\d\d.*?)(?=\r\n\w\w\d\d\d|\z)',3) _arraydisplay($a10)

(...just because I finally figured one of these son of a bitches out!...)

kylomas

edit: not quite, still fucking with it

edit2: Here it is, the line feed were fucking me up...

local $a10 = stringregexp(fileread(@scriptdir & '\nitekram.txt'),'(?si)(\w\w\d\d\d.*?)(?=\r\n\w\w\d\d\d|\z)',3)
for $1 = 0 to ubound($a10) - 1
 $a10[$1] = stringreplace($a10[$1],@crlf,'')
 ConsoleWrite($a10[$1] & @LF)
next
Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
kylomas

nitekram,

Just now saw your question.

for $1 = 1 to $a10[0]
    switch stringregexp($a10[$1],'^\w\w\d\d\d.*',0)     ; test for our pattern at beginning of string
        case 1                                          ; if 1 is returned (string begins with the pattern that we are looking for)
            $out &= @crlf & $a10[$1]                    ; then output line feed and string  
        case Else                                       ; else
            $out &= ' ' & $a10[$1]                      ; output space and string (no line feed)
    EndSwitch
Next

kylomas

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
nitekram

Thanks for your help and explanation, I will have to learn that function someday...again I thank you.


All by me:

"Sometimes you have to go back to where you started, to get to where you want to go." 

"Everybody catches up with everyone, eventually" 

"As you teach others, you are really teaching yourself."

From my dad

"Do not worry about yesterday, as the only thing that you can control is tomorrow."

 

WindowsError.gif

WIKI | Tabs; | Arrays; | Strings | Wiki Arrays | How to ask a Question | Forum Search | FAQ | Tutorials | Original FAQ | ONLINE HELP | UDF's Wiki | AutoIt PDF

AutoIt Snippets | Multple Guis | Interrupting a running function | Another Send

StringRegExp | StringRegExp Help | RegEXTester | REG TUTOR | Reg TUTOT 2

AutoItSetOption | Macros | AutoIt Snippets | Wrapper | Autoit  Docs

SCITE | SciteJump | BB | MyTopics | Programming | UDFs | AutoIt 123 | UDFs Form | UDF

Learning to script | Tutorials | Documentation | IE.AU3 | Games? | FreeSoftware | Path_Online | Core Language

Programming Tips

Excel Changes

ControlHover.UDF

GDI_Plus

Draw_On_Screen

GDI Basics

GDI_More_Basics

GDI Rotate

GDI Graph

GDI  CheckExistingItems

GDI Trajectory

Replace $ghGDIPDll with $__g_hGDIPDll

DLL 101?

Array via Object

GDI Swimlane

GDI Plus French 101 Site

GDI Examples UEZ

GDI Basic Clock

GDI Detection

Ternary operator

Share this post


Link to post
Share on other sites
kylomas

nitekram,

Version using only string functions. The SRE just made it simpler.

; non SRE version
ConsoleWrite(@LF)
ConsoleWrite('! Non SRE Version' & @LF)
for $1 = 1 to $a10[0]
 if StringIsAlpha(stringleft($a10[$1],2)) and StringIsDigit(stringmid($a10[$1],3,3)) then
   $out &= @crlf & $a10[$1]
 Else
   $out &= ' ' & $a10[$1]
 endif
Next
ConsoleWrite($out & @LF)

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
kylomas

nitekram,

Per guinness in thread:

SciTE has an option to convert EOLs to what ever the preferred choice is. Navigate to Options >> Convert Line End Characters. The preferred choice can be changed by selecting the menu item above. I have it set to @CRLF (like most probably.)

Might be your consolewrite problem.

kylomas

edit: corrected by Jos below

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
Jos

There is no need for this when using the standard SciTE we supply.

A simple Save should be enough since we have this option set:

ensure.consistent.line.ends=1

Jos

Edited by Jos

Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
kylomas

@Jos - I don't understand what you are saying. I thought the OP's problem with

When I look at the output in scite, it still looks jumbled, and I was about to show you that your code did not work, but when I pasted it into a notepad window - all the lines were there???

So, what is the issue with scite not showing the whole line and breaking it up into little chunks? Simmilar to what I pasted the example file above?

might be related to what guinness posted.

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
Jos

Guess I should have quoted the statement of guinness as well which explains how to change the EOL to make them consistent.

All I am saying is that they should be converted to the default EOL setting when you edit a file in SciTE that contains inconsistent EOL characters.

Jos


Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
kylomas

@Jos - My mistake, I misunderstood guinness. The EOl's that guinness is referring to are for whatever SCITE is editing, the OP's problem was with the format of consolewrite output.

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
nitekram

There is no need for this when using the standard SciTE we supply.

A simple Save should be enough since we have this option set:

ensure.consistent.line.ends=1

Jos

I am not sure what you are saying? When I tried @kylomas code listed above and here:

#include <array.au3>


local $a10 = stringsplit(fileread(@scriptdir & '\raytheon.txt'),@crlf,1), $out

_arraydisplay($a10)

for $1 = 1 to $a10[0]
    switch stringregexp($a10[$1],'^\w\w\d\d\d.*',0)
        case 1
            $out &= @crlf & $a10[$1]
        case Else
            $out &= ' ' & $a10[$1]
    EndSwitch
Next

ConsoleWrite($out & @LF)

The listed lines are not directly on one line - and if I grab them and copy them and then paste them in Notepad - the format is fine, and each of the lines are a whole line, which is what i was looking for - but in Scite the lines extend to multiple lines...that was my experience anyway - is there a way to fix that, as I would believe the code would be correct? But maybe I am missing something.


All by me:

"Sometimes you have to go back to where you started, to get to where you want to go." 

"Everybody catches up with everyone, eventually" 

"As you teach others, you are really teaching yourself."

From my dad

"Do not worry about yesterday, as the only thing that you can control is tomorrow."

 

WindowsError.gif

WIKI | Tabs; | Arrays; | Strings | Wiki Arrays | How to ask a Question | Forum Search | FAQ | Tutorials | Original FAQ | ONLINE HELP | UDF's Wiki | AutoIt PDF

AutoIt Snippets | Multple Guis | Interrupting a running function | Another Send

StringRegExp | StringRegExp Help | RegEXTester | REG TUTOR | Reg TUTOT 2

AutoItSetOption | Macros | AutoIt Snippets | Wrapper | Autoit  Docs

SCITE | SciteJump | BB | MyTopics | Programming | UDFs | AutoIt 123 | UDFs Form | UDF

Learning to script | Tutorials | Documentation | IE.AU3 | Games? | FreeSoftware | Path_Online | Core Language

Programming Tips

Excel Changes

ControlHover.UDF

GDI_Plus

Draw_On_Screen

GDI Basics

GDI_More_Basics

GDI Rotate

GDI Graph

GDI  CheckExistingItems

GDI Trajectory

Replace $ghGDIPDll with $__g_hGDIPDll

DLL 101?

Array via Object

GDI Swimlane

GDI Plus French 101 Site

GDI Examples UEZ

GDI Basic Clock

GDI Detection

Ternary operator

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
Sign in to follow this  

×