Xah Lee, 2009-07-26
This pages shows the equivalent of PowerShell for common unix commands related to text processing, such as grep, head, find, sort, uniq, wc etc. This page assumes Bash and GNU version of unix utils.
For simpler things such as “cd”, “mkdir”, “ls”, etc, see PowerShell as Bash.
Here is a quick table of constructs that are roughly of the same purpose.
| Desc | bash | PowerShell |
|---|---|---|
| create new file | touch new.txt | new-item new.txt -type file |
| ◇ | cat f.txt | get-content f.txt |
| ◇ | cat f1 f2 | ? |
| Display file top | head -n 50 ff | get-content -TotalCount 50 ff |
| ◇ | tail | ? |
| ◇ | split | ? |
| ◇ | find . -type d | Get-ChildItem . -Recurse -name |
| ◇ | find . -type f | ? |
| ◇ | find . -name "*html" | get-childitem . -Recurse -name -include *html |
| ◇ | find . -size 0 | ? |
| ◇ | find . -type f -size 0 -exec rm {} \; | ? |
| ◇ | grep xyz f.txt | select-string f.txt -pattern xyz -CaseSensitive |
| ◇ | grep xyz *html | select-string *html -pattern xyz -CaseSensitive |
| ◇ | grep's --ignore-case or -i | select-string without -CaseSensitive |
| ◇ | grep's --invert-match | select-string with -NotMatch |
| ◇ | grep's --files-with-matches | ◇ |
| ◇ | cmp | ? |
| ◇ | diff | ? |
| ◇ | sed | ? |
| ◇ | awk '{print $12 , $7}' | ? |
| ◇ | sort | ? |
| ◇ | uniq | ? |
| ◇ | wc | measure-object |
| ◇ | wc -m | measure-object -character |
| ◇ | wc -l | measure-object -line |
| ◇ | wc -w | measure-object -word |
| ◇ | tr | ◇ |
| ◇ | basename | ◇ |
| ◇ | dirname | ◇ |
find . -print0 | xargs -0 -l -i echo "{}";
find . -name "*bmp" -print0 | xargs -0 -l -i basename -s ".bmp" "{}" | xargs -0 -l -i convert "{}.bmp" "{}.png".
# creating a new file in current dir touch myfile.txt
# creating a new file in current dir new-item -name myfile.txt -type "file"
# put content in a file echo "some" > myfile.txt echo "some more" >> myfile.txt # append
# put content in a file "some" > myfile.txt "some more" >> myfile.txt # append
Note that, by default, the PowerShell redirect operator ">" creates files with little endian utf-16 encoding, and lines are hard-wrapped at 80 chars, and line ending uses Windows convension of "\r\n" (ascii 13 and 10).
On unixes, the conventional file encoding is utf-8, and lines are not hard-wrapped (sometimes truncated (deleted) silently), and line ending uses "\n" (ascii 10).
To create unix style output, use out-file, like this:
"1'n2'n3" | out-file -Encoding utf8 -width 999000 myfile.txt
However, the line ending used is still "\r\n". To create unix line ending of just "\n", use:
... | Out-String | %{ $_.Replace("`r`n","`n") } | out-file ...
However, the end of the file will still contain a "\r".
Unix “cat” can be used to read a file, or join several files. PowerShell equivalent is “get-content” with alias “cat” too.
# display a file content cat myfile.txt
# display a file content. (cat is alias of get-content)
cat myfile.txt
Note that by default, PowerShell assumes ascii. You can set your $OutputEncoding like this:
# set $OutputEncoding to utf-8 $OutputEncoding = New-Object -typename System.Text.UTF8Encoding
#
#
Related essays: