What is the best short one-liner in PowerShell to use as a grep alternative?
I wanted to share with you this short one-liner syntax for PowerShell version of
grep, which perhaps is relatively little known.
What is the best short one-liner in PowerShell to use as a grep alternative?
I wanted to share with you this short one-liner syntax for PowerShell version of
grep, which perhaps is relatively little known.
# In Bash, one could write, for example:
grep -Po '^\w+ \w+' myfile
to extract the first two words from a given file.
(sls '^\w+ \w+' myfile).Matches.Value
where sls is the alias for Select-String. See: Get-Alias -Definition Select-String.
Another option:
(cat myfile) -match '^\w+ \w+'; $Matches.0
However, this one is already somewhat complex; and the first part outputs additional True/False, which you may want to discard.
bash about your invocation of grep.cat is short for Get-Content.myfile contains more than 1 line, the -match solution, won't work - unless you add the -Raw switch to cat (Get-Content) in order to read the whole file as a single string.