Jump to content

Filtering A String From A File


fry
 Share

Recommended Posts

Basicaly I've got a line written to a text file.Now I want to clean that same line of all non numeric characters or do the stringtrimleft/right thing.Example of what I need to do is below.

blah^&@toishnfnf5643456hjdlasnbf_+!~dhfue*#

I want to filter that line down to just 5643456 and then right it to a file.I already know how to right it to a file,but just cant get past the filtering part.Just so you guys don't think i'm doing anything evil i'll tell you exactly what i'm doing.I'm copying,then renaming a XML file to a txt,then copying a few lines out of that text file.The reason I only want numbers and nothing else is it contains the download and upload totals of the program.Then i'm going to have to make some simple(i hope) math equations and then right the correct amounts of uploaded/download and total amount in Gigabytes.All that comes later though rightnow i'm stuck on filtering a few lousy lines :D

Link to comment
Share on other sites

I can try that I guess,i'm new to autoit 3,so you'll see why I never even thought about that as all the if then else stuff is new in v3.If thats the best way to do it then i'll try it out and thanks for the quick reply. :huh2:

Oh I just thought about your method and if I do it that way i'll have the numbers 64 infront of the rest,so then i'd have to make it alway remove the first two numbers wich are 6 and 4 :D

Edited by fry
Link to comment
Share on other sites

Oh I just thought about your method and if I do it that way i'll have the numbers 64 infront of the rest,so then i'd have to make it alway remove the first two numbers wich are 6 and 4 :D

How do you figure the number 64 would appear before the rest? You'd end up with the string 5643456. And why would the 64 need to be removed?

Jeff

Link to comment
Share on other sites

Very simple with VIM editor...

just hit

:%s/\D//g

where:

: -> enter command mode

% -> for all lines in the file

s -> substitute

/ -> start of pattern

\D -> non-digit character

/ -> end of pattern and start of replacing pattern

/ -> end of replacing pattern (every non-digit will be cancelled in this way)

g -> in all the occurrences in a line

Sorry if it was an off-topic answer

Ciao!

Ric

Link to comment
Share on other sites

How do you figure the number 64 would appear before the rest? You'd end up with the string 5643456. And why would the 64 need to be removed?[/QOUTE]

Jeff to answer your question simply just look at the line below

<TotalUpload type="int64">25766891049</TotalUpload>

As I said it was a xml file and see the "int64",even when stripping all non numeric carachters 64 would be left and i'd have to remove the first 2 numbers(64) so it doesnt throw off the totals.

I dunno what that cim thingo is ric but thanks for offering,i'm trying to do all this in AutoIt.

Edit:Ahh VIM is another text editor,thanks again ric but using VIM in this case isn't possible,because it reads the line,writes the line to text,then will strip it,then will do some math and eventually and hopefully show the upload/download statistics of a program that uses XML to keep it's records in. :D

Edited by fry
Link to comment
Share on other sites

Try using StringReplace with every possible character you want to strip. Then, if things are as you say, and 64 will be at the beginning of every line, then do a StringTrimLeft by 2.

Example (Simplified):

$line = "int64blah^&@toishnfnf5643456hjdlasnbf_+!~dhfue*#"
For $i = 1 To 255
    If $i = 48 Then    ; Have to skip over numbers
        $i = 57
        ContinueLoop
    EndIf
    $line = StringReplace($line, Chr($i), "")
Next
$line = StringTrimLeft($line, 2)
MsgBox(4096, "", $line)
Link to comment
Share on other sites

Thanx Valik,I had just realized I could do it that way and was reading through the help to pin down exactly how i wanted to do it and how it would best be done and you showed me a great way.Sadly this is only the first handful of lines in this "little" project thats growing like crazy. :D

Link to comment
Share on other sites

Before Valik posted "his method" i also was using the stringinstr and had some success.Valik could you do me a favor and explain why $i = 48 and the $i = 57

I know it's to make it skip numbers but i'm trying to learn here,not just copy and paste,so could you explain what the 48 and 57 are for/represent?I'm more than likely reading something wrong and god knows i'm no genious and I didn't sleep lastnight,but it seems that the if $i = whatever is the part that is used to compare against,like if 48 or 4 or 8,but obviously thats not it because this code works perfectly.I also checked the ASCII codes and niether 48 or 57 are something you could use to tell that the rest of the thread up to this point has been filtered and after this point is numbers.Basicaly,since i'm making no sense whatsoever just maybe comment explain your code a bit more for this ignorant n00b. :D

Link to comment
Share on other sites

Before Valik posted "his method" i also was using the stringinstr and had some success.Valik could you do me a favor and explain why $i = 48 and the $i = 57

I know it's to make it skip numbers but i'm trying to learn here,not just copy and paste,so could you explain what the 48 and 57 are for/represent?I'm more than likely reading something wrong and god knows i'm no genious and I didn't sleep lastnight,but it seems that the if $i = whatever is the part that is used to compare against,like if 48 or 4 or 8,but obviously thats not it because this code works perfectly.I also checked the ASCII codes and niether 48 or 57 are something you could use to tell that the rest of the thread up to this point has been filtered and after this point is numbers.Basicaly,since i'm making no sense whatsoever just maybe comment explain your code a bit more for this ignorant n00b. :D

48-57 are the ASCII character codes of numbers 0-9. They must be skipped in order for them to appear in the output.

Jeff

Link to comment
Share on other sites

  • Developers

Or if you want to skip all stuff between < > then you could do it this way:

$A = '<TotalUpload type="int64">25766891049</TotalUpload>'
$R = ""
For $X = 1 To StringLen($A)
   If StringMid($A, $X, 1) = "<" Then
    ; find ending > skip the stuff in between
      $X = $X + StringInStr(StringTrimLeft($A, $X), ">")
      ContinueLoop
   EndIf
   $R = $R & StringMid($A, $X, 1)
Next
MsgBox(0, "", $R)
Edited by JdeB

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

In

$line = '<TotalUpload type="int64">25766891049</TotalUpload>'

if

'<TotalUpload type="int64">' and '</TotalUpload>'

will always be the same, then it should be as easy as:

$line = '<TotalUpload type="int64">25766891049</TotalUpload>'
$line = StringReplace($line, '<TotalUpload type="int64">', "")
$line = StringReplace($line, '</TotalUpload>')

But if those changes or anything, then it would be better to just use JdeB's method to read between the opening and closing tags.

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