none
Need help on a match and replace function. RRS feed

  • Question

  • I will try to explain this one the best I can. Basically I need to parse a text file, match a value, replace that value with another value, but only once per line.

    For example, I have a text file that looks like this:

    BOB BUILDING

    BILL DRIVING

    FRANK FLYING

    I need to match on BOB, BILL, or FRANK and replace with BOB IS. For some reason it's appending the original string along with the replacement string. This is the code I have tried to use:

    $a = "c:\input.txt"
    
    (Get-Content $a) |
        foreach-object {
    
                if ($_ -match "BOB"){
                    $_ -replace "BOB" , "BOB IS"
                }
                if ($_ -match "BILL"){
                    $_ -replace "BILL" , "BOB IS"
                    }
                if ($_ -match "FRANK"){
                    $_ -replace "FRANK" , "BOB IS"
                    }
                Else
                {$_ }
                
                } | Set-Content $a


    My resulted output is:

    BOB IS BUILDING
    BOB BUILDING

    BOB IS DRIVING
    BILL DRIVING

    BOB IS FLYING


    What is not explicitly allowed should be implicitly denied



    Tuesday, April 21, 2015 10:04 PM

Answers

All replies

  • You are trying too hard.  Just do it as you stated it:

    Get-Content $a |
         foreach-object {
              $_ -replace 'BOB|BILL|FRANK' , 'BOB IS' 
         } |
          Out-File $a
    


    \_(ツ)_/

    Tuesday, April 21, 2015 10:11 PM
  • Note that the replace always returns the string even if nothing is replaced.

    'XXX' -replace 'BOB','BOB IS'

    This will return 'XXX'.

    'BOB XXX' -replace 'BOB','BOB IS'

    This will return: 'BOB IS XXX'


    \_(ツ)_/

    Tuesday, April 21, 2015 10:13 PM
  • This doesn't seem to work.


    $a = "c:\input.txt"
    Get-Content $a |
         foreach-object {
              $_ -replace 'BOB|BILL|FRANK' , 'BOB IS' 
         } |
          Out-File $a

    It replaces the data with nothing and sets the file as blank.



    What is not explicitly allowed should be implicitly denied

    Tuesday, April 21, 2015 10:29 PM
  • What is in your file?


    \_(ツ)_/

    Tuesday, April 21, 2015 10:42 PM
  • This is what I get when I run it against your example:

    PS C:\scripts> Get-Content $a |
    >>      foreach-object {
    >>           $_ -replace 'BOB|BILL|FRANK' , 'BOB IS'
    >>      }
    >>
    BOB IS BUILDING
    BOB IS DRIVING
    BOB IS FLYING
    
    PS C:\scripts>
    


    \_(ツ)_/

    Tuesday, April 21, 2015 10:44 PM
  • Hi,

    Try it this way:

    $a = '.\input.txt'
    (Get-Content $a) |
         foreach-object {
              $_ -replace 'BOB|BILL|FRANK' , 'BOB IS' 
         } |
    Out-File $a


    EDIT: jrv - I was seeing the same results until I added the parens.

    Don't retire TechNet! - (Don't give up yet - 13,225+ strong and growing)

    Tuesday, April 21, 2015 10:45 PM
  • Here try it this way:

    $results=Get-Content $a |
         foreach-object {
              $_ -replace 'BOB|BILL|FRANK' , 'BOB IS' 
         }
    $results | Out-File $a
    cat $a
    
    


    \_(ツ)_/

    • Proposed as answer by Mike Laughlin Tuesday, April 21, 2015 11:07 PM
    Tuesday, April 21, 2015 10:48 PM
  • The issue is concurrent access to the same file.


    \_(ツ)_/

    Tuesday, April 21, 2015 10:50 PM
  • Hi,

    Try it this way:

    $a = '.\input.txt'
    (Get-Content $a) |
         foreach-object {
              $_ -replace 'BOB|BILL|FRANK' , 'BOB IS' 
         } |
    Out-File $a


    EDIT: jrv - I was seeing the same results until I added the parens.

    Don't retire TechNet! - (Don't give up yet - 13,225+ strong and growing)

    Yes - that is one of at least three ways too avoid concurrency issues.  I forgot when I posted.


    \_(ツ)_/

    Tuesday, April 21, 2015 10:52 PM
  • I am trying to take this 1 step further.

    $a = "c:\input.txt"
    $ABC = "BOB,BOBBY,BOBBER"
    $DEF = "DAN,DANNY,DANIEL"
    (Get-Content $a) |
         foreach-object {
              $_ -replace 'BOB|BILL|FRANK',$ABC 
              $_ -replace 'DAN|PETER|LARRY',$DEF
         } |
    Out-File $a

    My input file looks like this:

    BOB
    PETER
    STEVE
    BRIAN
    GARY
    DAN
    KAREN
    BOB

    My output though is this. it duplicates records that do not match as well as ones that do.

    BOB,BOBBY,BOBBER
    BOB
    PETER
    DAN,DANNY,DANIEL
    STEVE
    STEVE
    BRIAN
    BRIAN
    GARY
    GARY
    DAN
    DAN,DANNY,DANIEL
    KAREN
    KAREN
    BOB,BOBBY,BOBBER
    BOB


    What is not explicitly allowed should be implicitly denied

    Tuesday, April 21, 2015 11:42 PM
  • I think it is time for you to try and learn how to write a script.

    Your question has been answered.  If you have another question please start a new topic.  We are not set up to do individual training in scripting.  This forum is for technicians and for those learning how to write scripts. 


    \_(ツ)_/

    Wednesday, April 22, 2015 12:32 AM
  • Apparently you have no clue because if you did, you would have answered my question correctly the first time. Until then, go troll somebody else.

    What is not explicitly allowed should be implicitly denied

    Wednesday, April 22, 2015 12:46 AM
  • Apparently you have no clue because if you did, you would have answered my question correctly the first time. Until then, go troll somebody else.

    What is not explicitly allowed should be implicitly denied

    Sorry - forum rules.  You can't stack questions and we don't do custom training-

    I know *it is annoying but I didn't make the rules. 

    They do have medication for those who need it what things are not to their liking.  you should- look into it.

    Ciao.


    \_(ツ)_/

    Wednesday, April 22, 2015 12:59 AM