6 ms·
I've been frequently derided for demoing it but I should write something about how to do similar tricks with PowerShell, as I have found it easier to learn than
by jolux 6y ago
I've been frequently derided for demoing it but I should write something about how to do similar tricks with PowerShell, as I have found it easier to learn than jq and an underrated tool in dealing with JSON.
The first example:
ConvertFrom-Json $USERX | ConvertTo-Json | Set-Clipboard
Getting properties by name:
ConvertFrom-Json $ORDER | select order*
- vips7L 6y agoMost of PowerShell is underrated when it comes to pipelines in shell. I've completely rid myself of GNU core utils with just pwsh.
- the_cat_kittles 6y agowhy? are you forced to work on windows?
- djeiasbsbo 6y agoIirc powershell is cross platform.
- dopylitty 6y agoPowerShell is supported on Linux and macOS as well [1] I completely agree with the parent that PowerShell blows the existing shell options out of the water. Having pipelines with objects rather than just text makes everything so much easier. Instead of spending an hour futzing with awk or regex or applicatioons like jq to parse values from command results you can just access what you want directly and get on with your work. 1. https://docs.microsoft.com/en-us/powershell/scripting/install/installing-powershell?view=powershell-7.1 https://docs.microsoft.com/en-us/powershell/scripting/instal...
- altdatathrow 6y agoI'm glad you're productive in your CLI tool usage but just because it takes you an hour futzing with basic *nix tools doesn't mean it's the same for everyone else. Powershell is extremely cumbersome for me due to the sheer verbosity of each command and the apparent MO of "let's come up with a new name for everything" and "sensible defaults? what's that?"
- fiddlerwoaroof 6y agoYes, awk and jq “just work” for me: whether it’s because I’ve trained myself through constant usage or whatever, I’ve yet to understand the supposed benefits of a richer IPC mechanism than byte streams.
- tracker1 6y agoI just don't want to have to type that much physically, let alone the verbose commands and slightly odd syntax you often have to memorize. I know some love it, but I'm much happier with just using bash everywhere, even though msys/git bash on windows has its' own quirks, WSL ftw though.
- vips7L 6y agoYou don't have to type the full commands. Most installs come with a full list of aliases: https://pastebin.com/eHsTUaRQ https://pastebin.com/eHsTUaRQ For instance on my system: Get-ChildItem -> gci or ls Select-String -> grep
- emj 6y agoWith tabcompletion you do not need to have aliases, but you need to name your commands in a sane way, having the meaningless words forst like powershell does is really cumbersome for tabbing. Examples: Where-, Get-, Remove-, Export-, From-, Select-, Invoke- Nice list of aliases though.
- vips7L 6y agoMenu completion is even better if you haven't tried it!
- emj 6y agoFWIW I'm talking about CLI, I have little experience with menu completion, but how does menu completion solve the Get-<TAB> problem it will give too many hits and parsing that will take too long. Menu-completion in Intellij, and it's ilk, is more about discoverablity than fast typing then you can take your time parsing the output.
- vips7L 6y agoNo, but I'm a gamer at my core and I develop on the JVM so I have no need to interface with the kernel. Windows just works for me.
- Jestar342 6y agoOne difference in usage of PWSH I can't wrap my head around is when I want to do something akin to: grep -rl $pattern | $othercommand So far I've got this (deliberately avoiding aliases for this example) in the above grep's command's stead: Get-ChildItem -Recurse -Path * | Select-String $pattern But I just cannot fathom how to break from the table format to pass the lines to $othercommand. How does one pass a pwsh list to a single command? Using %{}/For-Each {} wouldn't work because it would invoke $othercommand for each line.
- GSGBen 6y agoEverything in powershell is an object, but sometimes the output format doesn't make it clear. Select-String basically returns a match object with some nested results, but the output formatting makes it awkward to see. Pipe it to | format-list * to investigate it, and you'll see a bunch of members. In this case if what you want is just the matched string, you want to expand the `Matches` property of the object, then expand the value property of each of those. In this case the term 'expand' is key: if you just use `select` it'll return you the property, name and all. Use `-expand` to return the plain array. So something like: Get-ChildItem -Recurse -Path * | Select-String $pattern | select -expand matches | select -expand value which you can then use how you like. An alternate form is (Get-ChildItem -Recurse -Path * | Select-String $pattern).Matches.Value because the . notation for member access works on all items in an array (Matches in this case).
- asimops 6y agoSince Powershell isn't working on text but on objects, you might just want to use grep. Anyways, this should reproduce the output of grep in powershell: Get-ChildItem -Recurse -File | Select-String "$pattern" -List -SimpleMatch -CaseSensitive | Select-Object -ExpandProperty path You will want to only search in files for this to not ouput some lines of InputStream if there is a match in the actual path. If $pattern is an actual regex you will want to drop SimpleMatch. In case you want some proper powershell objects you might want to pipe this into Get-Item.
- jolux 6y agoI don't think I entirely understand the question, is $othercommand doing something with all of the lines together?
- spaetzleesser 6y agoIf it only wasn't dog slow and chewed up memory when working with large files. Large files often force me to go back to cygwin.
- vips7L 6y agoIdk that's just not something I do with the shell. At work I process a lot of large files, but typically those require hand written code to use off heap or file/db backed collections (I work in the JVM).
- sequoia 6y agoYou definitely should! Is it alright if I copy this comment onto my blog underneath this post in the "comments" section? If you do write such a post make sure to let me know so I can link it from mine! You could write a response "here's how I'd do all the stuff in that post in Powershell with no extra tools" that would be very cool :)
- jolux 6y agoI'll try to remember!
- Arnavion 6y agoI haven't used pwsh in a few years, but does `ConvertTo-Json` still have that terrible `-Depth` parameter that you have to remember to set so that it doesn't mangle your JSON? A quick web search indicates that it does. Edit: Yes, it does. '{ "a": { "b": { "c": { "d": { "e": 5 } } } } }' | ConvertFrom-Json | ConvertTo-Json { "a": { "b": { "c": "@{d=}" } } } So you have to remember to set `-Depth 100` for every invocation of `ConvertTo-Json`. You also can't set it higher because it has a hard-coded limit of 100, so hopefully you never deal with JSON that has more nesting than that. Yeesh. I like PS, but this one commandlet's design has always baffled me.
- voxic11 6y agoThat API is being reworked now at least https://github.com/PowerShell/PowerShell/pull/11198 https://github.com/PowerShell/PowerShell/pull/11198
- jolux 6y agoI was wondering about that, because the docs say it uses Json.NET, which while an excellent library is being replaced with System.Text.Json in the BCL/FCL as far as I know.
- kbd 6y agoNu shell is also really handy for these types of format conversions (and querying): > echo '{ "a": { "b": { "c": { "d": { "e": 5 } } } } }' | from json | to toml [a.b.c.d] e = 5
- GSGBen 6y agoYep. Love powershell, disliked this decision, but now understand it: objects returned from native powershell commands have a loooooooooooooooooooot of nesting. Try: gci | select -first 1 | ConvertTo-Json -Depth 2 gci | select -first 1 | ConvertTo-Json -Depth 3 gci | select -first 1 | ConvertTo-Json -Depth 4 one at a time.
- Arnavion 6y ago