Sed Alternative with PowerShell

1

On UNIX I would do something like this: sed -n '/início/,/fim/ p' < arquivo.txt

On Windows we have PowerShell, how do you do it with it?

    
asked by anonymous 04.05.2016 / 20:52

3 answers

1

Well, I took the opportunity to learn some PS. Here is a solution to the problem (the language is in powershell).

#se der acesso não autorizado, execute "Set-ExecutionPolicy Unrestricted -Scope CurrentUser"

#$inclusive=$true mostra as linhas com os padrões.
#ao fazer essa função extrai a ideia do seguinte post http://stackoverflow.com/a/25639841/3697611
#sed $início, $fim, $inclusive
function sed {
    $mostrar = $false
    $início = $args[0]
    $fim = $args[1]
    $inclusive = $args[2]
    $input | %{ #pega entrada do pipeline https://technet.microsoft.com/en-us/library/hh847768.aspx
        if ($_ -match $início) { 
            $mostrar = $true; if ($inclusive){echo $_} 
        } elseif ($_ -match $fim) { 
            $mostrar = $false; if ($inclusive){echo $_} 
        } elseif ($mostrar) {
            echo $_ 
        }
    }
}

#teste de unidade
function sedTest {
    ("início'nbloco'nfim".Split("'n")|
        sed "^início$" "^fim$" $false) -eq "bloco"
}

sedTest
    
05.05.2016 / 17:42
1

Short answer

It does not, the Unix / Linux shell is much more powerful than Windows.

Long answer

Because of these things recently Microsoft has announced that it will place bash as a native Windows shell in future releases.

You can still use the GNU libraries compiled for Windows:

link

between them is the SED that you need.

You can also use the Cygwin project which is a port of the * nix environment for windows

link

    
05.05.2016 / 00:26
1

Now, Strawberry-Perl for windows comes with some utilities (gcc, compile tools, libraries, interface to databases, etc.).

Provides a development environment similar to unix perl.

Then an example analogous to your thirst:

perl -0nE 'say for(/inicio.*?fim/gs)' arquivo.txt
    
05.05.2016 / 18:33