The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 2,482 other followers

Some links on xargs simulation in PowerShell

Posted by jpluimers on 2021/10/13

On nx, I’m used to xargs which allows to convert from a pipe of output into arguments passed to a command. This is useful, as many commands only accept arguments as parameters.

In PowerShell, you can usually avoid an xargs equivalent because commandlet output is a stream of objects that you can post-process using . I for instance used that in PowerShell: recovering from corrupt empty *.nupkg files after a disk was accidentally full during update.

Here are some xargs equivalency examples:

  • [WayBack] PowerShell tips for bash users, part 1 • Five

    xargs

    Xargs is one of the most powerfull UNIX commands. It is used to build and execute command lines from standard input. For example:
    $ cat dirs | xargs mkdir
    will use cat to take the strings (be it newline or blank character separated) from file ‘dirs’ and pass them through pipe to xargs which will then send one by one line as argument to mkdir which will then create those dirs or complain if those are existent.

    PowerShell equivalent:

    PS> cat dirs | %{mkdir $_}

    There is no ‘xargs’ command in PS, but you can use ‘foreach ‘ loop and pass the piped variable ‘$_’ to the mkdir. Shorthand for ‘foreach’ is ‘%’. This time also only newlines will separate the strings apart. If multiple strings separated by blanks are found in same line, mkdir will create a directory with blanks in the name, while we must quote to have the same in bash:

    $ cat dirs | sed 's|^|"|g' | sed 's|$|"|g' |xargs mkdir

  • [WayBack] What’s the equivalent of xargs in PowerShell? – Stack Overflow

    Q

    The POSIX-defined xargs command takes all of the items it receives from standard input and passes them as command-line arguments to the command it receives on it’s own command line. E.g: grep -rn "String" | xargs rm.

    What’s the equivalent in PowerShell?

    The following questions all ask this:

    but there is no correct answer because all the answers either use ForEach-Object, which will process items one-at-a-time (like xargs -n1) which gets the desired result for the examples given, or store the intermediate result in a variable, which offends my functional commandline-fu.

    A

    There are two ways that I’ve found. The first is probably more idiomatic PowerShell, and the second is more true to the pipe-based spirit of xargs.

    As an example, let’s say we want to pass all our cat pics to myapp.exe.

    Method #1: Command substitution

    You can do something similar to using $(command substitution) in sh by embedding your pipeline in the command string:

    &"myapp.exe" @(Get-ChildItem -Recurse -Filter *.jpg | Another-Step)

    The @(...) creates an array from the command inside it, and PowerShell automatically expands arrays passed to & into seperate command-line parameters.

    However, this does not really answer the question, because it will only work if you have control over the command you want to pass to, which may not be the case.

    Method #2: True piping

    You can also construct a “double pipeline” by having a sub-expression to pipe your objects, collecting them to an array, and then piping the array to your final command.

    ,@(Get-ChildItem -Recurse -Filter *.jpg | Another-Step) | %{&"myapp.exe" $_}

    The @(...) as before collects the items into an array, and the array is then piped to the final command which is invoked using % (ForEach-Object). Ordinarily, this would then loop over each item individually, because PowerShell will automatically flatten the array when it’s piped, but this can be avoided by prepending the , operator. The $_ special variable is then used as normal to embed the passed array.

    So the key is to wrap the pipeline you want to collect in ,@(...), and then pipe that to something in %{...}.

References

–jeroen

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

 
%d bloggers like this: