1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Find a string and set a line

Discussion in 'T&T - Scripting' started by JB2, Jan 9, 2015.

  1. JB2

    JB2

    Joined:
    Jan 9, 2015
    Messages:
    9
    Likes Received:
    0
    I need to find a string in a file (easy enough) and set the line that the string was found in as a variable.

    set comp=%@word[0,%@line[list1.txt,%counter]]
    set comp2=%@word[1,%@line[list1.txt,%counter]]
    set comp3=%@instr[5,14,[2,%@line[list1.txt,%counter]]
    set comp4=%@word[2,%@line[list1.txt,%counter]]
    reg query %comp\HKU > tempkey.txt <this is the command and the file I need to search a string in.
    S-1-5-21-1960230475-814316877 is the string in can search and find , but the line number moves from 5 to 7 depending on which pc I search on.
     
  2. vefatica

    Joined:
    May 20, 2008
    Messages:
    7,883
    Likes Received:
    29
    Just a thought ...
    Code:
    v:\> type waittest.btm
    alias hosts if exist v:\hosts (ren v:\hosts hosts.xxx) else (ren v:\hosts.xxx hosts)
    hosts
    start /wait firefox http://google.com
    echo FireFox has closed
    hosts
    unalias hosts
    
    
    v:\> echo %@strip[^[^],%@word[0,%@execstr[ffind /K /M /L /T"unalias" waittest.btm]]]
    6
    
    v:\> echo %@strip[^[^],%@word[0,%@execstr[ffind /K /M /L /T"closed" waittest.btm]]]
    4
    
     
    JB2 likes this.
  3. JB2

    JB2

    Joined:
    Jan 9, 2015
    Messages:
    9
    Likes Received:
    0
    that looks promising going to try it .ty
     
  4. JB2

    JB2

    Joined:
    Jan 9, 2015
    Messages:
    9
    Likes Received:
    0
    it works and finds it and set the line number but it skips the first one it finds and selectects the second line
    SET COMP5=%@strip[^[^],%@word[0,%@execstr[ffind /K /M /L /T"S-1-5-21-1960230475-814316877" TEMPKEY.TXT]]]


    HKEY_USERS\.DEFAULT
    HKEY_USERS\S-1-5-19
    HKEY_USERS\S-1-5-19_Classes
    HKEY_USERS\S-1-5-20
    HKEY_USERS\S-1-5-20_Classes
    HKEY_USERS\S-1-5-21-1960230475-814316877-1687796506-500 <line 6 it skips this one for some reason the above should find line 6 first?
    HKEY_USERS\S-1-5-21-1960230475-814316877-1687796506-500_Classes <line 7
    HKEY_USERS\S-1-5-18
     
  5. semko

    Joined:
    Jun 10, 2008
    Messages:
    7
    Likes Received:
    0
    Add /F to the parameters. It should stop after the first match then.
     
  6. samintz

    samintz Scott Mintz

    Joined:
    May 20, 2008
    Messages:
    1,188
    Likes Received:
    11
    if you only need the output of REG QUERY to locate the line number you can pipe the output into FFIND instead of having to create a file first.
    Code:
    reg query HKU | ffind /klm /t"S-1-5-21-1960230475-814316877"
    
    or to isolate the line number specifically:
    set linenum=%@word["[]",0,%@execstr[reg query HKU | ffind /klm /t"S-1-5-21-1960230475-814316877"]
    
    And FWIW, on my system I find the first match. I'm not sure how you are skipping it unless the file contents are not in the order you listed.
    -Scott
     
  7. JB2

    JB2

    Joined:
    Jan 9, 2015
    Messages:
    9
    Likes Received:
    0
  8. JB2

    JB2

    Joined:
    Jan 9, 2015
    Messages:
    9
    Likes Received:
    0

Share This Page