mIRC Home    About    Download    Register    News    Help

Print Thread
#216769 02/12/09 01:33 PM
Joined: Dec 2009
Posts: 2
P
Proxx Offline OP
Bowl of petunias
OP Offline
Bowl of petunias
P
Joined: Dec 2009
Posts: 2
Hello mirc users

i have proberly a simple question!
it a long time since i used mirc so i think some thing are changed since then.

i have a file +/- 1000 lines all i want is to count the number of lines i thought the command was //echo numbers: $read(file.txt,0)
but it doesnt work.

well if that question is awnserd the next shocked

i want to search the file for all lines with one or more searchstrings and copy the lines to a different file

is that possible?

maybe with $read or /filter ?

Thanx in advance

Last edited by Proxx; 02/12/09 01:33 PM.
Proxx #216770 02/12/09 01:45 PM
Joined: Jun 2007
Posts: 933
5
Hoopy frood
Offline
Hoopy frood
5
Joined: Jun 2007
Posts: 933
See /help $lines

And yes, the /filter command would be ideal for your second point.

5618 #216772 02/12/09 02:30 PM
Joined: Dec 2009
Posts: 2
P
Proxx Offline OP
Bowl of petunias
OP Offline
Bowl of petunias
P
Joined: Dec 2009
Posts: 2
oke $lines is oke

i dont think i can use multiple wildcards in one /filter command so im trying to acheve the same with an loop

var %l = $lines(file.txt)
while (%i <= 0) {

; here i want to search $read(file.txt,%l) for 1 or more strings
string001
stringtwo
string003
stringfour
dec %i
}
if one or more of the 4strings is found the line has to be copyed to output.txt


thanx for the quick post 5618

Last edited by Proxx; 02/12/09 02:31 PM.
Proxx #216773 02/12/09 02:55 PM
Joined: Nov 2006
Posts: 1,559
H
Hoopy frood
Offline
Hoopy frood
H
Joined: Nov 2006
Posts: 1,559
You could filter with a regular expression as the matchtext, though this would require 1) transformation of "regular" wildcards into their regex equivalents and 2) escaping of all other possible metachars, or \Q\E-quoting for everything except the "wildcards".

Example: if scanning for the strings
Code:
test
something else
a single wildc?rd
*ltiple wildc*s
With some $replacex and $+'s, you could create the regex:
Code:
/\Qtest\E|\Qsomething else\E|\Qa single wildc\E.\Qrd\E|.*\Qltiple wildc\E.*\Qs\E/i


As an alternative, especially if you're not into regular expressions, why not:
1) filter matches for every search string into separate files (or hidden windows, or a single hidden window)
2) merge the results (if not using a single hidden window) and remove duplicate lines with a loop. If you decide to use hidden windows (or a single hidden window), /aline -n could be handy. (The cleaned window content could be dumped into a textfile with /filter or /savebuf.)

This may sound a bit long winded, but it should perform much faster than a $read-loop on a large file, especially if the No. of matched lines will usually be a fraction of the original file only:
- /filter itself is extremely fast (...compared to all other scanning/matching methods available in mIRC)
- loops on the lines of hidden windows will be faster than loops on physical files
smile

Proxx #216790 03/12/09 12:55 AM
Joined: Jan 2003
Posts: 2,523
Q
Hoopy frood
Offline
Hoopy frood
Q
Joined: Jan 2003
Posts: 2,523
An alternative way is to use /filter twice: one pass to grab the matching lines and another to remove matching lines from the file (using the -x switch), so that the search for the next string is done on the remaining lines (excluding the previous matches). Here's an example:
Code:
alias multsearch {
  write -c outfile.txt
  window -h @ms1
  loadbuf @ms1 infile.txt
  tokenize 124 $1-
  multsearch2 $*  
  close -@ @ms1
}

alias multsearch2 {
  filter -wf @ms1 outfile.txt * $+ $1-*
  filter -wwcx @ms1 @ms1 $1- * $+ $1-*
}
Change infile.txt and outfile.txt accordingly and test with /multsearch string 1|string 2|string 3|string 4

Note that the matches in outfile.txt are ordered according to which string matched them (and then according to their position in infile.txt), unlike Horstl's methods, so use whichever method suits you.


/.timerQ 1 0 echo /.timerQ 1 0 $timer(Q).com
qwerty #216813 03/12/09 12:49 PM
Joined: Nov 2006
Posts: 1,559
H
Hoopy frood
Offline
Hoopy frood
H
Joined: Nov 2006
Posts: 1,559
Good point, this avoids a line-based loop altogether and again imrproves performance smile

I found an old snippet of mine, modified it and added comments, to illustrate the $replace-stuff required for a regex approach (which will keep lines in order)
Code:
alias examplesearch {
  var %infile = versions.txt, %outfile = search results.txt
  var %reg

  ; prompt for (next) search string
  while ($input(Enter $iif(%reg,next) search string or nothing to $iif(%reg,start,abort) the search:,eog,Search for one or more strings in %infile) != $null) {

    ; transform text string into regex string
    ; 1) $regsubex:   reduce consecutive asterisks (not mandatory, bot won't hurt)
    ; 2) $replacexcs: 
    ;    a) replace wildcard chars "asterisk" and "questionmark" with regex equivalents
    ;    b) replace whitespaces with regex equivalents (else you'll loose single trailing- and consecutive whitespaces)
    ;    c) "quote" all other text (else chars like "[ ] { } ^ | \ . +" wouldn't be treated literally and likely would corrupt the regex)
    ; 3) $removecs:   clean regex by removing empty (redundant) quotings "\Q\E" (not mandatory)
    
    var %s = $removecs( $+(\Q, $replacexcs($regsubex($!,/\*+/g,*), *,\E.*\Q, ?,\E.\Q, $chr(32),\E\x20\Q, \E,\E\\E\Q) ,\E) ,\Q\E)

    ; add string to the regex
    var %reg = $iif(%reg,$v1 $+ |) $+ %s
  }

  ; one or more search terms to process
  if (%reg) {
    ; filter infile to outfile (regex enclosed in /<regex>/ and "i" switch added for a case insensitive search)
    filter -ffcg $qt(%infile) $qt(%outfile) $+(/,%reg,/i)

    ; report result, open outfile (if any matches and not locked)
    echo -agc info Lines of %infile matching your search string(s): $filtered
    if ($filtered) && (!$lock(run)) { run $qt(%outfile) }
  }
}
...maybe it's of use.

Last edited by Horstl; 03/12/09 12:51 PM.

Link Copied to Clipboard