Earlier quoted context omitted.
Not OP but to me the best thing about PowerShell is that it recognizes that text is not always the best way to output results from commands if you care about creating pipelines. In short, it passes objects around so there's no need for parsing text.
Two examples from the article translated into PS (sorry, I'm a bit rusty so the second one may not be the shortest possible): PS> "alice", "bob" | echo PS> Get-ChildItem . -Include "*test.cpp","*test.py" -Recurse | foreach { Remove-Item $_.Name } No text parsing in sight, and the object attributes can be tab-completed from the shell (e.g. I tab-completed the `$_.Name`).
An Opinionated Guide to Xargs
101–110 of 135 posts
Re: An Opinionated Guide to Xargs
#102This is only tangentially related, but after all the posts here the last few days about thought terminating cliches, I can’t help but reflect on the “X considered harmful” title cliche
What every X should know about Y, an opinionated take on Z considered harmful
Re: An Opinionated Guide to Xargs
#103Earlier quoted context omitted.
One thing parallel can do better than xargs is collect output. If you use `xargs -P`, all processes share the same stdout and output may be mixed arbitrarily between them. (If the program being executed uses line buffering, lines usually won't be mixed together from multiple invocations, but they can be if they're long enough). In contrast, `parallel` by default doesn't mix together output from different commands at…
OK thanks, looks like there are several features of GNU parallel that users like. For the output interleaving issue, what I do is use the $0 Dispatch Pattern and write a shell function that redirects to a file: do_one() { task_with_stdout > $dir/$task_id.txt } So if there are 10,000 tasks then I get 10,000 files, and I can check the progress with "ls", and I can also see what tasks failed and possibly restart them. Y…
btw, oil looks very cool. I hate how many footguns are in common shells.
[1] eg writing to a tempfile and atomically renaming into place: "task_with_stdout > $dir/task_id.txt.tmp && mv $dir/task_id.txt{.tmp,}"
Re: An Opinionated Guide to Xargs
#104Of xargs, for, and while, I have limited myself to while. It's more typing everytime but saves me from having to remember so many quirks of each command. cat input.file | ... | while read -r unit; do ${unit}; done | ... between 'while read -r unit' and 'while IFS= read -r unit' I can probably handle 90% of the cases. (maybe I should always use IFS since I tend to forget the proper way to use it).
Re: An Opinionated Guide to Xargs
#105Earlier quoted context omitted.
Not OP but to me the best thing about PowerShell is that it recognizes that text is not always the best way to output results from commands if you care about creating pipelines. In short, it passes objects around so there's no need for parsing text.
Two examples from the article translated into PS (sorry, I'm a bit rusty so the second one may not be the shortest possible): PS> "alice", "bob" | echo PS> Get-ChildItem . -Include "*test.cpp","*test.py" -Recurse | foreach { Remove-Item $_.Name } No text parsing in sight, and the object attributes can be tab-completed from the shell (e.g. I tab-completed the `$_.Name`).
Also, expanding the regex into `-Include` parameters is somewhat cheating since `-Include` only takes globs, and it just so happens that that particular regex can be converted into globs.
The general equivalent is:
gci -re | ?{ $_.Name -match '.*_test\.(py|cc)' } | ri
(I used the shorter aliases because someone will probably read yours and reinforce the stereotype that PS is overly verbose.)Re: An Opinionated Guide to Xargs
#106I tend to reach for gnu parallel instead of xargs - https://www.gnu.org/software/parallel/parallel_alternatives.... parallel is probably on the complex side but its also been actively developed, bugfixed and had a lot of road miles from large computing users.
The nagware prompts of parallel are so objectionable that I will do a lot of things to avoid using it at all. So pretentious!
Re: An Opinionated Guide to Xargs
#107awk '{ print your_command }' | bash Never can remember all the -I stuff around xargs
This is like the sed|bash anti-pattern mentioned in the original post, and quoted in the appendix on shell injection. I wouldn't say "never use it", but I would hesitate to ever put it in a script, vs. doing a one-off at the command line.
This is far superior to futzing with xargs interactive or whatever dry run feature they have.
Re: An Opinionated Guide to Xargs
#108Earlier quoted context omitted.
Two examples from the article translated into PS (sorry, I'm a bit rusty so the second one may not be the shortest possible): PS> "alice", "bob" | echo PS> Get-ChildItem . -Include "*test.cpp","*test.py" -Recurse | foreach { Remove-Item $_.Name } No text parsing in sight, and the object attributes can be tab-completed from the shell (e.g. I tab-completed the `$_.Name`).
You don't need to `foreach { Remove-Item $_.Name }` because Remove-Item can take the objects returned by Get-ChildItem directly. Also, expanding the regex into `-Include` parameters is somewhat cheating since `-Include` only takes globs, and it just so happens that that particular regex can be converted into globs. The general equivalent is: gci -re | ?{ $_.Name -match '.*_test\.(py|cc)' } | ri (I used the shorter al…
Re: An Opinionated Guide to Xargs
#109I frequently find myself reaching for this pattern instead of xargs: do_something | ( while read -r v; do . . . done ) I’ve found that it has fewer edge cases (except it creates a subshell, which can be avoided in some shells by using braces instead of parens)
Re: An Opinionated Guide to Xargs
#110Earlier quoted context omitted.
parallel doesn't either, it just nags. I agree about how silly and annoying it is. Imagine if every time the parallel author opened Firefox he got a message reminding him to personally thank me if he uses his web browser for research, or if every time his research program calls malloc he has to acknowledge and cite Ulrich Drepper. Very very silly. Parallel is the better tool but the nagware impairs its reputation.
or every time a process called fork() you had to read some stupid message