3 ms·
How about some PowerShell instead? # Echo command-line input $args -join " " (Yes, it's a single line.) # Count duplicates in a file $lines = @{}
by hfkwer 3y ago
How about some PowerShell instead?
# Echo command-line input
$args -join " "
(Yes, it's a single line.)
# Count duplicates in a file
$lines = @{}
foreach ($path in $args) {
cat $path | % { $lines[$_] += 1 }
}
$lines.GetEnumerator() | % { if ($_.Value -gt 1) { "$($_.Value)`t$($_.Name)" } }
# GET request
param($url)
irm $url
# Parallel GET requests
$before = get-date
$args | % -parallel {
$res = iwr $_
$after = get-date
$time = ($after - $using:before).TotalSeconds
"${time}s`t$($res.RawContentLength)`t$_"
} -ThrottleLimit 12345
$after = get-date
$time = ($after - $before).TotalSeconds
"${time}s elapsed"
I'm showcasing a few different ways of doing things: using $args, using the param directive; using foreach, or % which is an alias of ForEach-Object; irm for Invoke-RestMethod, iwr for Invoke-WebRequest...
- ptx 3y agoNo thanks. PowerShell seems to have all sorts of crazy gotchas in addition to the weird syntax. For example, if I understand correctly, your "cat $path" won't actually work for all filenames, because "cat" is an alias [1] for "Get-Content -Path" which will expand wildcards in the filename [2][3], so you have to know to use "-LiteralPath" instead of you want it to work properly. And then there's Microsoft's telemetry, which is enabled by default. [4] [1] https://learn.microsoft.com/en-us/powershell/scripting/learn/shell/using-aliases?view=powershell-7.3#compatibility-aliases-in-windows https://learn.microsoft.com/en-us/powershell/scripting/learn... [2] https://stackoverflow.com/questions/33572502/unable-to-get-output-from-get-filehash/39967489#39967489 https://stackoverflow.com/questions/33572502/unable-to-get-o... [3] https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-content?view=powershell-7.3 https://learn.microsoft.com/en-us/powershell/module/microsof... [4] https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_telemetry?view=powershell-7.3 https://learn.microsoft.com/en-us/powershell/module/microsof...
- hfkwer 3y agoA shell language that expands wildcards isn't what I'd call a "crazy gotcha". And let's be real, if you name your files with * or ? in their names, you're just setting yourself up for a bad time. Weird syntax? Weirder than TCL? I frankly don't care about the telemetry.
- Riverheart 3y ago“<Language> seems to have all sorts of crazy gotchas in addition to the weird syntax.” Says everyone about every language.