Snake690

Delete a line with limitations

3 posts in this topic

Hi everyone !

I am facing a problem.

I want to delete line from a text file with some constraints but I do not leave me.

Here is part of the original file:

378;;0;.781459500002;0,00;0,00;0;;1;EXO;;
3781486500006;;0;.781486500006;0,00;0,00;0;;1;EXO;;
4024800173104;;0;.ELI0;0,00;24,00;1;TA;1;EXO;;
3258170733014;;0;.INTERG;0,00;7,40;1;TA;1;EXO;;
8720;;0;.LUCG;0,00;12,30;1;TA;1;EXO;;
5411808067877;;0;.ORG;0,00;7,40;1;TA;1;EXO;;
3258170699419;;0;.WING;0,00;8,60;1;TA;1;EXO;;

And the record that I would like to get:

3781486500006;;0;.781486500006;0,00;0,00;0;;1;EXO;;
4024800173104;;0;.ELI0;0,00;24,00;1;TA;1;EXO;;
3258170733014;;0;.INTERG;0,00;7,40;1;TA;1;EXO;;
5411808067877;;0;.ORG;0,00;7,40;1;TA;1;EXO;;
3258170699419;;0;.WING;0,00;8,60;1;TA;1;EXO;;

It would delete lines that have less than 5 characters, but only the characters that are before the first semicolon.

Knowing that my file can have more than 1000 lines.

I do not know if I made it clear lol but hey I tried a lot of things but as his novice becomes too complicated ...

If you have an idea to solve my problem I would be extremely grateful ;)

Share this post


Link to post
Share on other sites



Snake690,

Welcome to the AutoIt forums.

Not too hard to do:

#include <Array.au3>

; Simulate reading file to an array (_FileReadToArray)
$sText = "378;;0;.781459500002;0,00;0,00;0;;1;EXO;;" & @CR & _
"3781486500006;;0;.781486500006;0,00;0,00;0;;1;EXO;;" & @CR & _
"4024800173104;;0;.ELI0;0,00;24,00;1;TA;1;EXO;;" & @CR & _
"3258170733014;;0;.INTERG;0,00;7,40;1;TA;1;EXO;;" & @CR & _
"8720;;0;.LUCG;0,00;12,30;1;TA;1;EXO;;" & @CR & _
"5411808067877;;0;.ORG;0,00;7,40;1;TA;1;EXO;;" & @CR & _
"3258170699419;;0;.WING;0,00;8,60;1;TA;1;EXO;;"
$aLines = StringSplit($sText, @CR)

_ArrayDisplay($aLines, "Original", Default, 8)

; Now look at each line from the bottom up
For $i = $aLines[0] To 1 Step -1
    ; Look for first ;
    $iPosition = StringInStr($aLines[$i], ";")
    ; If it is less than 6 characters in, then the initial block is less than 5 characters
    If $iPosition < 6 Then
        ; So delete line
        _ArrayDelete($aLines, $i)
        ; And reduce count
        $aLines[0] -= 1
    EndIf
Next

_ArrayDisplay($aLines, "Stripped", Default, 8)

; Now just rewrite the file (_FileWriteFromArray)

Please ask if you have any questions.

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

an other way, using a regular expression  :)

; sample text
$txt = "378;;0;.781459500002;0,00;0,00;0;;1;EXO;;" & @CR & _
"3781486500006;;0;.781486500006;0,00;0,00;0;;1;EXO;;" & @CR & _
"4024800173104;;0;.ELI0;0,00;24,00;1;TA;1;EXO;;" & @CR & _
"3258170733014;;0;.INTERG;0,00;7,40;1;TA;1;EXO;;" & @CR & _
"8720;;0;.LUCG;0,00;12,30;1;TA;1;EXO;;" & @CR & _
"5411808067877;;0;.ORG;0,00;7,40;1;TA;1;EXO;;" & @CR & _
"3258170699419;;0;.WING;0,00;8,60;1;TA;1;EXO;;"

; or read file
; $txt = FileRead("1.txt")

$res = StringRegExpReplace($txt, '(?m)^[^;]{0,4};.*\R?', "")
Msgbox(0,"", $res)

 

Edited by mikell

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