Search code examples
regexpowershellawkslash

Regex and Powershell that gets only data between a pre set number of slashes


Imagine I have a line like that below:

/a/b/c/d/e/f/g/h/

I need to get the data between the 2nd and 5th slash, but would like to control this selection regularly in between the code.

This way the substring for the 2nd and 5th would be /b/c/d/

So I've tried: $ cat test.txt | gawk '/\/{2}(.*?)\/{5}/{print $0}' without success. I need a working code line for both regex and powershell.


Solution

  • Using PowerShell, -replace can perform the job. -replace operator uses regex for its matching mechanism.

    '/a/b/c/d/e/f/g/h/' -replace "(/[^/]+){1}((/[^/]+){3}/).*",'$2'
    

    -split and -join can also perform the job.

    "/{0}/" -f (('/a/b/c/d/e/f/g/h/' -split "/")[2..4] -join "/")
    

    For a straight PowerShell regex match, you can use the following:

    ([regex]"(?<=(/[^/]+){1})(/[^/]+){3}/").Match('/a/b/c/d/e/f/g/h/').Value
    

    Any of the above techniques can take input from a variable or reading a file as shown below:

    # Using a variable
    $str = '/a/b/c/d/e/f/g/h/'
    $str -replace "(/[^/]+){1}((/[^/]+){3}/).*",'$2'
    /b/c/d/
    
    # Reading from a file
    ([regex]"(?<=(/[^/]+){1})(/[^/]+){3}/").Match((Get-Content File.txt)).Value
    /b/c/d/