Advertisement
arsel

Power sell unix command

Mar 25th, 2020
582
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.42 KB | None | 0 0
  1. http://xahlee.info/powershell/PowerShell_for_unixer.html
  2.  
  3.  
  4. PowerShell Tutorial
  5.  
  6.  
  7.  
  8.  
  9. PowerShell vs Bash
  10. By Xah Lee. Date: 2009-07-26. Last updated: 2018-07-24.
  11. This pages shows the equivalent of PowerShell for common unix commands related to text processing.
  12.  
  13. Simple Commands
  14. The following bash commands have PowerShell alias. (but the options may not be the same)
  15.  
  16. cd
  17. pwd
  18. ls
  19. pushd
  20. popd
  21. cp
  22. rm
  23. rmdir
  24. mv
  25. cat
  26. echo
  27. set
  28. ps
  29. kill
  30. clear
  31. man
  32. file content
  33. Purpose Bash + GNU utils PowerShell
  34. create new file touch ff ni ff -type file
  35. ni ↔ New-Item
  36. show file content cat ff cat ff
  37. cat ↔ Get-Content ↔ gc
  38. merge file cat f1 f2 > new.txt cat f1, f2 > new.txt
  39. show first n lines head -n 50 ff cat ff | select -first 50
  40. show last n lines tail cat file | select -last 50
  41. find file
  42. Purpose Bash + GNU utils PowerShell
  43. list dirs find . -type d gci . -Recurse -name
  44. gci ↔ Get-ChildItem
  45. list files find . -type f ?
  46. list html files find . -name "*html" gci . -Recurse -name -include *html
  47. list empty files find . -size 0 ls . -recurse | where {$_.length -eq 0}
  48. delete empty files find . -type f -size 0 -exec rm {} \; ?
  49. search text (grep)
  50. Purpose Bash + GNU utils PowerShell
  51. search text grep xyz f.txt select-string f.txt -pattern xyz -CaseSensitive
  52. search text grep xyz *html select-string *html -pattern xyz -CaseSensitive
  53. search text grep -i select-string without -CaseSensitive
  54. search text grep --invert-match select-string with -NotMatch
  55. search text grep --files-with-matches ◇
  56. cat file_name | where { $_ -match "regex_pattern"}
  57.  
  58. cat file_name | %{$_ -replace "regex_pattern","replace_string"}
  59.  
  60. diff, sort, get column etc
  61. Purpose Bash + GNU utils PowerShell
  62. ◇ cmp diff
  63. diff ↔ compare ↔ Compare-Object
  64. compare file difference diff f1 f2 diff (cat f1) (cat f2)
  65. ◇ sed ?
  66. print nth column awk '{print $12 , $7}' ?
  67. ◇ sort sort (Sort-Object)
  68. ◇ uniq sort -Unique
  69. ◇ wc Measure-Object
  70. (measure ↔ Measure-Object)
  71. ◇ tr ◇
  72. ◇ basename ◇
  73. ◇ dirname ◇
  74. Work In Progress
  75. Note: this page is a mess. It's work in progress. (started when i wished to convert my log processing bash script to PowerShell)
  76.  
  77. todo, show PowerShell equivalent of this:
  78.  
  79. find . -print0 | xargs -0 -l -i echo "{}";
  80. find . -name "*bmp" -print0 | xargs -0 -l -i basename -s ".bmp" "{}" | xargs -0 -l -i convert "{}.bmp" "{}.png".
  81. touch
  82. # creating a new file in current dir
  83. touch myfile.txt
  84. # creating a new file in current dir
  85. new-item -name myfile.txt -type "file"
  86. Redirect “>”
  87. # put content in a file
  88. echo "some" > myfile.txt
  89. echo "some more" >> myfile.txt # append
  90. # put content in a file
  91. "some" > myfile.txt
  92. "some more" >> myfile.txt # append
  93.  
  94.  
  95. 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 convention of "\r\n" (ascii 13 and 10).
  96.  
  97. 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).
  98.  
  99. To create unix style output, use out-file, like this:
  100.  
  101. "1'n2'n3" | out-file -Encoding utf8 -width 999000 myfile.txt
  102. However, the line ending used is still "\r\n". To create unix line ending of just "\n", use:
  103.  
  104.  
  105.  
  106. … | Out-String | %{ $_.Replace("`r`n","`n") } | out-file …
  107. However, the end of the file will still contain a "\r".
  108.  
  109. cat
  110. Unix “cat” can be used to read a file, or join several files. PowerShell equivalent is “get-content” with alias “cat” too.
  111.  
  112. # display a file content. (cat is alias of get-content)
  113. cat myfile.txt
  114. Note that by default, PowerShell assumes ascii. You can set your $OutputEncoding like this:
  115.  
  116. # set $OutputEncoding to utf-8
  117. $OutputEncoding = New-Object -typename System.Text.UTF8Encoding
  118. Thanks to Shivashis Saha for addition on “cat”. He also sends the following:
  119.  
  120. split
  121. For example, if you want to split a line based on “:”, you can use the following line:
  122.  
  123. (given $str is a line with different fields separated by ":")
  124. $temp=@($str -split ":");
  125. Super thanks to Jeffrey Snover of Microsoft for helping on about 10 of the items. (Jeffrey's the inventor of PowerShell)
  126.  
  127. If you have a question, put $5 at patreon and message me.
  128.  
  129. PowerShell
  130. xah powershell logo
  131. Install, Start, Exit
  132. Help Command
  133. as cmd.exe, bash
  134. list Alias
  135. Piping
  136. Env Vars
  137. Automatic Variables
  138. Write Script
  139. PowerShell vs Bash
  140. ∑XAH
  141. © 1995, 2020 Xah Lee.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement