locked
RegEx Search and Replace RRS feed

  • Question

  • Having a little trouble finding an example of what I'm trying to do, hoping someone can help.

    Data is this:

    þ001\ESE 19497.txt
    þ001\ESE 19501.txt
    þ002\ESE 19504.txt
    þ002\ESE 19508.txt
    þ002\ESE 19512.txt
    þ003\ESE 19516.txt
    þ003\ESE 19517.txt
    þ003\ESE 19520.txt

    Need to do a search and replace so that it turns into this:

    þ\\servername\path\001\ESE 19497.txt
    þ\\servername\path\001\ESE 19501.txt
    þ\\servername\path\002\ESE 19504.txt
    þ\\servername\path\002\ESE 19508.txt
    þ\\servername\path\002\ESE 19512.txt
    þ\\servername\path\003\ESE 19516.txt
    þ\\servername\path\003\ESE 19517.txt
    þ\\servername\path\003\ESE 19520.txt

    I'm running into a few problems:

    1. A search string that will handle the changing volume numbers 001 002 003 ...etc.
    2. A replace string that will add the literal characters "\\" and "\" for the server path and,
    3. Also replace the volume numbers 001 002 003 ... etc.

    Thanks

    Friday, February 19, 2016 4:17 PM

Answers

  • Here you go:


     Get-Content "datafile.txt" | ForEach-Object {
       $_ -replace '^þ(\d{3}\\)', 'þ\\servername\path\$1'
     } | Out-File "datafile2.txt"
    


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by Bill_Stewart Friday, March 18, 2016 6:58 PM
    Friday, February 19, 2016 4:45 PM

All replies

  • Hi,

    Personally I'd skip the regex and just do something like this:

    Get-Content .\inputData.txt | ForEach-Object {
    
        $_.Replace('þ','þ\\servername\path\')
    
    } | Out-File .\outputData.txt
    
    Get-Content .\outputData.txt


    Friday, February 19, 2016 4:23 PM
  • Here you go:


     Get-Content "datafile.txt" | ForEach-Object {
       $_ -replace '^þ(\d{3}\\)', 'þ\\servername\path\$1'
     } | Out-File "datafile2.txt"
    


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by Bill_Stewart Friday, March 18, 2016 6:58 PM
    Friday, February 19, 2016 4:45 PM