Alternative to sed with Powershell

Asked

Viewed 1,228 times

2

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

In Windows we have Powershell, how do you do it with it?

  • you want to read a file take the result of regex and play in a new file?

  • For now just visualise.

  • Maybe not everyone understood the meaning of the command, maybe you could explain better, I imagine it is possible to translate to powershell.

  • @rray thanks for the support, I already managed to see http://answall.com/a/127000/43845 but I don’t know why they gave down vote...

  • Maybe we can simplify this code.

  • i don’t know much about PS, to learning even is today, if you can simplify it @rray

  • when you install the powershell in windows, you get something "like" Unix bash. To handle regular expressions in the powerful mode you have in Unix, you should join the corresponding utilities (sed, awk, grep, perl, cut, Paste, etc)

Show 2 more comments

4 answers

1


Well, I took the opportunity to learn a little 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
  • 1

    Guys, I asked the question myself. Why did you vote no if it was the only post that was put code in Powershell? Should give negative votes for answers that do not show code in the requested language.

  • 1

    +1. Depending on what you want to do from to use the gc, in that post of msdn also shows other forms.

1

By the way, the Strawberry-Perl for windows comes with some utilities (gcc, Compile tools, libraries, database interface, etc).

Provides a Unix perl-like development environment.

then an example analogous to your sed:

perl -0nE 'say for(/inicio.*?fim/gs)' arquivo.txt
  • Interesting friend, long ago I played with perl on linux. However John, I would like examples in the requested language, Powershell.

  • 1

    after installing Strawberry, in principle, this line runs on all shells (powershell, cmd, in the shell perl installs )

  • thanks for trying to help @Jjoao.

  • 1

    my friend @Jjoao learned perl again these days and saw how practical this language is! rs

1

Short answer

No, the Unix/Linux shell is much more powerful than Windows.

Long answer

Because of these things recently Microsoft announced that it will put bash as the native Windows shell in future versions.

You can also use GNU libraries compiled for Windows:

http://unxutils.sourceforge.net/

among them is the SED that you need.

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

https://www.cygwin.com/

  • thanks for the comment, really Unix/linux utilities are very practical, there would hardly be a replacement the height

  • 2

    Grateful @Jjoao, I try to do the best

1

You could do:

(Get-Content arquivo.txt).replace('valorAtual','valorFuturo') | Set-Content arquivo.txt

Browser other questions tagged

You are not signed in. Login or sign up in order to post.