none
Powershell - Pipe values from text file into select-string query

    Question

  • Hi,

    I have a list of values from a command below:

    $a = get-content c:\test.txt | Select-String 'test'

    which outputs;

    test1
    test2
    test3

    I'd like to then perform another select-string query using these values on a different file, similar to

    get-content c:\new.txt | select-string -pattern $a  | add-content c:\report.txt

    Can someone help how I can read in the contents of $a (test1, test2 and test3) a line at a time?

    Thanks

    Thursday, March 06, 2014 8:10 PM

Answers

  • Give it a try
    [string[]]$a = get-content C:\Intel\test1.txt| Select-String 'Test'
    $a | %{$_ = $_.trim()}
    $b = get-content C:\Intel\test2.txt | select-string -Pattern $a -SimpleMatch
    $b | Out-File  -append c:\results.txt


    • Edited by gaff-jakobs Friday, March 07, 2014 12:21 AM
    • Marked as answer by Thumper71 Friday, March 07, 2014 10:39 AM
    Friday, March 07, 2014 12:20 AM

All replies

  • I'm not sure if I understand the question but think you want to do this:
    [array]$a = get-content c:\test.txt | Select-String 'test'
    $a += get-content c:\test2.txt | Select-String 'test' 
    Or this:

    $a = get-content c:\test.txt | Select-String 'test' | foreach{get-content c:\test.txt| select-string $_}
     

    • Edited by gaff-jakobs Thursday, March 06, 2014 8:27 PM
    Thursday, March 06, 2014 8:21 PM

  • PS C:\> help ForEach-Object
    


    -- Bill Stewart [Bill_Stewart]

    Thursday, March 06, 2014 8:22 PM
    Moderator
  • Thanks gaff - I think your second example is close to what I need

    $a = get-content c:\test.txt | Select-String 'test' | foreach{get-content c:\test.txt| select-string $_}
     

    I want $a to search for test in c:\test.txt -  as you wrote

    $a = get-content c:\test.txt | Select-String 'test' 

    If I output $a to the console at this stage it gives test1, test2 and test3 - perfect.  I then want to use these values as search string in a new file c:\test2.txt

    However the line below doesn't seem to output the next set of matches

    I tested as

    $a = get-content c:\test.txt | Select-String 'test' | foreach{get-content c:\test2.txt| select-string $_ | out-file -append c:\results.txt

    Please can you help get me over the last bit?

    Thursday, March 06, 2014 9:15 PM
  • Ok. So you want to take the strings from $a to 'Select-string's from an other file.

    Let's keep it simple...

    [string[]]$a = get-content C:\text1.txt | Select-String 'Test'
    $b = get-content C:\text2.txt | select-string -Pattern $a -SimpleMatch
    $b | Out-File  -append c:\results.txt



    • Edited by gaff-jakobs Thursday, March 06, 2014 10:42 PM
    Thursday, March 06, 2014 10:41 PM
  • Thanks - but I'm still getting blank output with this?

    [string[]]$a = get-content C:\text1.txt | Select-String 'Test'
    $b = get-content C:\text2.txt | select-string -Pattern $a -SimpleMatch
    $b | Out-File  -append c:\results.txt

    To confirm c:\text1.txt contains just the lines test1 and test3 - (which are output when i check and write $a to the console) as:

    test3                                                                                                                                 
    test1

    so far ok.

    c:\text2.txt also contains test3 and test1 lines but these are not being output from $b.  c:\results.txt is appearing as empty.  Any ideas?

    Thursday, March 06, 2014 11:24 PM
  • Give it a try
    [string[]]$a = get-content C:\Intel\test1.txt| Select-String 'Test'
    $a | %{$_ = $_.trim()}
    $b = get-content C:\Intel\test2.txt | select-string -Pattern $a -SimpleMatch
    $b | Out-File  -append c:\results.txt


    • Edited by gaff-jakobs Friday, March 07, 2014 12:21 AM
    • Marked as answer by Thumper71 Friday, March 07, 2014 10:39 AM
    Friday, March 07, 2014 12:20 AM
  • The problem here is one of "contains"

    What you are looking for is that a line inn  A is acceptable if it is "contained" in your list.

    $list -contains $a

    Example:

    PS C:\scripts> $list='test1','test3','test5'
    PS C:\scripts> $lines='test1','test2','test3','test9','test3','test5','test7','test3','test5','test6','test3','test5'
    PS C:\scripts> $lines | ?{$list -contains $_}
    test1
    test3
    test3
    test5
    test3
    test5
    test3
    test5

    We can call this a filter pattern.  It can be used any time we want to use one list to filter another list.  It can also be easily negated.

    This is a mathematically complete solution when implemented correctly.  It is also much easier to use than any other method.


    ¯\_(ツ)_/¯


    • Edited by jrv Friday, March 07, 2014 2:04 AM
    Friday, March 07, 2014 2:03 AM
  • Thanks gaff -  it worked perfectly.  Really appreciate the help
    Friday, March 07, 2014 10:41 AM
  • Really helpful - thanks JRV.  For your information the basic script is written and working.  This an amendment I am trying to implement.  It's nothing to do with trying to get someone to write me a script - you seem obsessed about that...

    This is about me wanting to learn something I don't know how to do - which I think is one of the purposes of a forum.  This is a MS forum by the way, not a forum owned by you so please drop the attitude


    Sorry - I posted that in the wrong forum.  I just deleted it.

    The code method I posted is another way to do this and can overcome many of the issues and performance issues of Select-String.  It is very much more flexible.


    ¯\_(ツ)_/¯

    Friday, March 07, 2014 1:33 PM
  • @jrv

    Thank you too,

    Compared to you, I'm a rookie and you're an Ace.  I just like to help other users because I love PowerShell.

    I found out the select-string cmdlet has some disadvantages but if someone comes up with a script that almost works I stick with it.

     

    Friday, March 07, 2014 4:24 PM
  • Please can  you advise how to achieve below:

    I have text file in which already few lines are present and i have to add new text line string  on the text file if a string found and it should at same line :

    for example below are contents of my file:

    google.com

    tibco.com

    msn.com

    i have to search all the lines and for all values and upon the search if text file has above values present then at respective places it should add the new lines i.e on same line in which search string is found.

    if google.com is found then add  "abc"

    if msn.com found then add   "xyz"

    etc like that for other search values  and if already abc or xyz is added then don't add it again.

    i tried belwo method but no luck.any advise pls

    $RESULT=GET-CONTENT C:\Temp\MyTextFile.txt

    $prependVar = "some text    "

    FOREACH ($LINE in $RESULT) 
              { 

              if ($line.tostring() –eq "google" ) 
                   { 
                   Write-Host $LINE
                   $newlines = $line + "$prependVar$_"
                   write-host ( $newlines ) 
                   $newlines | Out-File $file
                   } 
              }



    Umeed4u

    Friday, April 21, 2017 12:36 PM