Snake690 Posted October 24, 2016 Share Posted October 24, 2016 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 ;) Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted October 24, 2016 Moderators Share Posted October 24, 2016 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 Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
mikell Posted October 24, 2016 Share Posted October 24, 2016 (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 October 24, 2016 by mikell Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now