Jump to content
Sign in to follow this  
andybiochem

Help! strip a text file...

Recommended Posts

andybiochem

Hi guys!

I'm just getting into programming, so please be gentle!

I hope someone can point me in the right direction to do what I want, I've tried searching the forum but can't find anything similar...

Ok, I have a text file that contains lines of information I need to keep, but also lines of information I want getting rid of.

The only thing in common in the lines I want to keep is an asterix at the end of each line.

Question is, how do I tell Autoit to strip the text file of everything EXCEPT for the lines with an asterix in it.....i.e.

one *

two *

three

four *

five *

...the line with "three" in it is removed from the txt file.

I've tried lots of different functions, but can't get my head round it....like I said, not only am I an autoit newb, but also a general programming newb - I've got no chance!!!!!!!!

Also to throw another spanner into the works, eventually I'll also need to delete certain lines containing the asterix, but also containing another specified variable....i.e. specify "dog"

cat *

horse *

dog *

monkey *

zebu *

........the line with "dog" in it is deleted from the txt file.

Hope someone can help. No need to write out any script for me, just a pointer to which functions could be used to do this!!!!!

Preferably I'd like to attempt this without opening the file with FileOpen(), but if that's the only way, so be it!

Thanks in advance!!!!!


- Table UDF - create simple data tables - Line Graph UDF GDI+ - quickly create simple line graphs with x and y axes (uses GDI+ with double buffer) - Line Graph UDF - quickly create simple line graphs with x and y axes (uses AI native graphic control) - Barcode Generator Code 128 B C - Create the 1/0 code for barcodes. - WebCam as BarCode Reader - use your webcam to read barcodes - Stereograms!!! - make your own stereograms in AutoIT - Ziggurat Gaussian Distribution RNG - generate random numbers based on normal/gaussian distribution - Box-Muller Gaussian Distribution RNG - generate random numbers based on normal/gaussian distribution - Elastic Radio Buttons - faux-gravity effects in AutoIT (from javascript)- Morse Code Generator - Generate morse code by tapping your spacebar!

Share this post


Link to post
Share on other sites
Paulie

Check out StringInStr()

#Include <File.au3>

$NewFile = ""
$File = FileOpen(*File*,1)
For $i = 1 to _FileCountLines(*FilePath*)
$Line = FileReadLine($File,$i)
If StringInStr($line, "*") then
    $NewFile&= $Line&@CRLF
Endif
Next
$Out=FileOpen("Output.txt", 10)
Filewrite($Out, $Newfile)
Fileclose($File)
FileClose($Out)

Untested. Quick and off the top of my head. Check syntax.

Edited by Paulie

Share this post


Link to post
Share on other sites
Nahuel

Here's an example:

#include <File.au3>
#include <Array.au3>
FileWrite("MyText.txt","one*" & @CRLF & "two*" & @CRLF & "three" & @CRLF & "four*")
Dim $FileArray[1]
Dim $FileNew[1]
_FileReadToArray("MyText.txt",$FileArray)
For $i=1 To $FileArray[0]
    If StringInStr($FileArray[$i],"*") Then
        _ArrayAdd($FileNew,$FileArray[$i])
    EndIf
Next
_ArrayDelete($FileNew,0)
_FileWriteFromArray("MyTextNEW.txt",$FileNew)

Share this post


Link to post
Share on other sites
enaiman

- open file in "read-only" mode

- create a new file to hold the result

- read the original file into an array (_FileReadToArray , File.au3 UDF)

- For every element of the array

-> StringInStr for "*"

->-> if "*" found -> StringInStr for "dog"

->->->if "dog" not found -> FileWriteLine to your result file

- FileClose for both files opened

good luck :P


SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Share this post


Link to post
Share on other sites
SmOke_N

Let's try a little RegExp... I'm not sure if it works, but the test string I used did.

_FileStrip("FileToStrip", "dog", "FileOutIfNecessary", "NewPatternIfTheDefaultOneDoesntWork")

Func _FileStrip($sFile, $sDelete = "", $sOutFile = -1, $sPattern = -1)
    If $sOutFile = -1 Then $sOutFile = $sFile
    FileClose(FileOpen($sOutFile, 2))
    If $sPattern = -1 Then $sPattern = "(?s)(?i)^.+?[^\*]\r\n|^(.+?[^(" & _
        $sDelete & ")].+?\*)\r|\n(.+?[^(" & $sDelete & ")].+?\*)\r"
    Return FileWrite($sOutFile, StringRegExpReplace(FileRead($sFile), $sPattern, "\1"))
EndFunc
That should be pretty fast considering everything else you'd need to do with mixing the other string functions.


Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites
andybiochem

Wow! 4 replies - thanks guys!!!

Top marks go to Nahuel - that is exactly what I was after!!!!!

Thanks to everyone else for the help aswell....but perhaps I wasn't clear what I wanted in my first post...

I can't specify what string to replace in the string - in the example I used 'dog', but that could have been any string with varying length and content. My fault. Nahuel's code deals with that though.

I'm really getting into using autoit, also good to see there is an excellent community behind it!!

Keep up the good work!!


- Table UDF - create simple data tables - Line Graph UDF GDI+ - quickly create simple line graphs with x and y axes (uses GDI+ with double buffer) - Line Graph UDF - quickly create simple line graphs with x and y axes (uses AI native graphic control) - Barcode Generator Code 128 B C - Create the 1/0 code for barcodes. - WebCam as BarCode Reader - use your webcam to read barcodes - Stereograms!!! - make your own stereograms in AutoIT - Ziggurat Gaussian Distribution RNG - generate random numbers based on normal/gaussian distribution - Box-Muller Gaussian Distribution RNG - generate random numbers based on normal/gaussian distribution - Elastic Radio Buttons - faux-gravity effects in AutoIT (from javascript)- Morse Code Generator - Generate morse code by tapping your spacebar!

Share this post


Link to post
Share on other sites
enaiman

No need to write out any script for me, just a pointer to which functions could be used to do this!!!!!

lol - I thought this is you're after.

I see - if there is some code then the better :P Anyway the best way to learn is to do it yourself.

... and about "marks" - we're not competing here (our intention was to help you, NOT to pass an "exam" and you being our examiner) ... sometimes being carried away could have "undesired effects". I understand it could happen to anyone and I'm going over it for now.

Good luck,


SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Share this post


Link to post
Share on other sites
andybiochem

... and about "marks" - we're not competing here (our intention was to help you, NOT to pass an "exam" and you being our examiner) ... sometimes being carried away could have "undesired effects". I understand it could happen to anyone and I'm going over it for now.

Good luck,

No, no, I was being pleonastic.........just a term of phrase....sorry if I offended anyway.


- Table UDF - create simple data tables - Line Graph UDF GDI+ - quickly create simple line graphs with x and y axes (uses GDI+ with double buffer) - Line Graph UDF - quickly create simple line graphs with x and y axes (uses AI native graphic control) - Barcode Generator Code 128 B C - Create the 1/0 code for barcodes. - WebCam as BarCode Reader - use your webcam to read barcodes - Stereograms!!! - make your own stereograms in AutoIT - Ziggurat Gaussian Distribution RNG - generate random numbers based on normal/gaussian distribution - Box-Muller Gaussian Distribution RNG - generate random numbers based on normal/gaussian distribution - Elastic Radio Buttons - faux-gravity effects in AutoIT (from javascript)- Morse Code Generator - Generate morse code by tapping your spacebar!

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.