none
Read file and then replace word from csv file RRS feed

  • Question

  • I want to replace words in config files, an replace the valor with value that are in csv file for example:

    User      New User

    abc        qwr

    wer       123456

    I want to search the word  "abc" and replace it for "qwr"

    I write this but i do not know how to add

    $configFiles=get-childitem . *.config -rec
    foreach ($file in $configFiles)
    {
    $file.PSPath >> log.log
    $a= "" + $file.PSPath + ".bkp"
    cp $file.PSPath $a
    (Get-Content $file.PSPath) |
    Foreach-Object {$_ -replace "Decchi", "mono"} |
    Set-Content $file.PSPath

    }


    adecchi

    Friday, June 7, 2013 6:01 PM

All replies

  • Can you try and explain that a little better?  Once you run the replace you just have to write the data to a file.  A more exact explanation of your problem would be helpful.

    Hope that helps! Jason

    Friday, June 7, 2013 6:06 PM
  • im confused, whats the issue?

    this part works

    (Get-Content $file.PSPath) | 
    Foreach-Object {$_ -replace "Decchi", "mono"} | 
    Set-Content $file.PSPath

    Friday, June 7, 2013 6:07 PM
  • I wrote this part to :

    1- Backup the file

    2- Replace word that i wrote in the terminal

    Now I want to add to my powershell sentences to be able to read a config file and search the word need to be reaplce from csv file. If the word is found in the config file i want to be replace it from the csv file.

    for example  parametros.csv

    User      New User

    abc        qwr

    wer       123456

    In the app.ini search the word "abc" and reaplce it for "qwr".

    sorry my english. :)

    Thz a lot to try to help me.


    adecchi

    Friday, June 7, 2013 6:19 PM
  • $configFiles=get-childitem c:\test -Filter *.config
    $csv = Import-Csv C:\test\asdas.csv
    foreach ($file in $configFiles)
    {
    "$($file.PSPath)" >> log.log
    $a= "" + $file.PSPath + ".bkp"
    cp $file.PSPath $a
    for($x=0;$x -lt $csv.Count; $x++){
    (Get-Content $file.PSPath) | 
    Foreach-Object {$_ -replace $csv.user[$x], $csv.'new user'[$x]} | 
    Set-Content $file.PSPath
    }
    }

    play with this, change locations

    Friday, June 7, 2013 6:40 PM
  • I have this error ""Cannot index into a null array,""

    adecchi

    Friday, June 7, 2013 7:50 PM
  • is your csv empty? maybe field names are different? also you will need to change path C:\test\asdas.csv

    post your script and full error if you cant figure it out


    Friday, June 7, 2013 8:25 PM
  • The csv has data and the path is correct.

    Here is the error.

    Cannot index into a null array.
    En C:\Users\t.ps1: 13 Carácte
    r: 23
    + write-host $csv.BDARG[ <<<< $x]
        + CategoryInfo          : InvalidOperation: (1:Int32) [], RuntimeException
        + FullyQualifiedErrorId : NullArray

    $configFiles=get-childitem . *.config -rec
    $csv= import-csv ./parametros.csv -delimiter ";"

    foreach ($file in $configFiles)
    {
    "($file.PSPath)" >> log.log
    $a= "" + $file.PSPath + ".bkp"
    #cp $file.PSPath $a
    for($x=0;$x -lt $csv.Count; $x++){
    #(Get-Content $file.PSPath) |
    #Foreach-Object {$_ -replace $csv.a[$x], $csv.b[$x]} |
    #Set-Content $file.PSPath
    write-host $csv.BDARG[$x]

    }
    }


    adecchi

    Friday, June 7, 2013 9:02 PM
  • Thz to all I can do it! using foreach. Now i am working to replace the exactly word becasue if the word to replace is concatenated its replace :(

    $configFiles=get-childitem . *.config -rec
    $csv= import-csv ./pa.csv -delimiter ";"
    
    foreach ($file in $configFiles)
    {
    $file.PSPath >> log.log
    $a= "" + $file.PSPath + ".bkp"
    cp $file.PSPath $a
    foreach ($param in $csv){
    $A=$param.D
    $K=$param.C
    (Get-Content $file.PSPath) | 
    Foreach-Object {$_ -replace $A, $K} | 
    Set-Content $file.PSPath
    }
    
    }
    


    adecchi

    Friday, June 7, 2013 9:50 PM
  • What's the format of your config-files? You could probably use regular expressions. The replace-operator you use, can also use regular expressions. Also note that the replace operator is case-insensitive by default, "creplace" is the case-sensitive variant (should you need it).

    Saturday, June 8, 2013 1:09 PM
  • You could maybe use a thing called word-boundaries; see this page: http://www.regular-expressions.info/wordboundaries.html

    There's an example in the page: "\bword\b".

    Saturday, June 8, 2013 1:12 PM