Advertisement
Guest User

Untitled

a guest
May 12th, 2017
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.54 KB | None | 0 0
  1. #!/bin/sh
  2. # see also `man awk`
  3.  
  4. awk '{ print }' /etc/passwd | head
  5. # root:x:0:0:root:/root:/bin/bash
  6. # daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
  7. # bin:x:2:2:bin:/bin:/usr/sbin/nologin
  8. # sys:x:3:3:sys:/dev:/usr/sbin/nologin
  9. # sync:x:4:65534:sync:/bin:/bin/sync
  10. # games:x:5:60:games:/usr/games:/usr/sbin/nologin
  11. # man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
  12. # lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
  13. # mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
  14. # news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
  15.  
  16. awk '{ print $0 }' /etc/passwd | head
  17. # root:x:0:0:root:/root:/bin/bash
  18. # daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
  19. # bin:x:2:2:bin:/bin:/usr/sbin/nologin
  20. # sys:x:3:3:sys:/dev:/usr/sbin/nologin
  21. # sync:x:4:65534:sync:/bin:/bin/sync
  22. # games:x:5:60:games:/usr/games:/usr/sbin/nologin
  23. # man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
  24. # lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
  25. # mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
  26. # news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
  27.  
  28. head /etc/passwd | awk '{ print $0 }'
  29. # root:x:0:0:root:/root:/bin/bash
  30. # daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
  31. # bin:x:2:2:bin:/bin:/usr/sbin/nologin
  32. # sys:x:3:3:sys:/dev:/usr/sbin/nologin
  33. # sync:x:4:65534:sync:/bin:/bin/sync
  34. # games:x:5:60:games:/usr/games:/usr/sbin/nologin
  35. # man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
  36. # lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
  37. # mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
  38. # news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
  39.  
  40. head /etc/passwd | awk '{ print $1 }'
  41. # root:x:0:0:root:/root:/bin/bash
  42. # daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
  43. # bin:x:2:2:bin:/bin:/usr/sbin/nologin
  44. # sys:x:3:3:sys:/dev:/usr/sbin/nologin
  45. # sync:x:4:65534:sync:/bin:/bin/sync
  46. # games:x:5:60:games:/usr/games:/usr/sbin/nologin
  47. # man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
  48. # lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
  49. # mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
  50. # news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
  51.  
  52. head /etc/passwd | awk -F: '{ print $1 }'
  53. # root
  54. # daemon
  55. # bin
  56. # sys
  57. # sync
  58. # games
  59. # man
  60. # lp
  61. # mail
  62. # news
  63.  
  64. head /etc/passwd | awk -F: '{ printf "%-20s%s\n", $1, $7 }'
  65. # root /bin/bash
  66. # daemon /usr/sbin/nologin
  67. # bin /usr/sbin/nologin
  68. # sys /usr/sbin/nologin
  69. # sync /bin/sync
  70. # games /usr/sbin/nologin
  71. # man /usr/sbin/nologin
  72. # lp /usr/sbin/nologin
  73. # mail /usr/sbin/nologin
  74. # news /usr/sbin/nologin
  75.  
  76. head /etc/passwd | awk 'BEGIN { FS=":" } { print $1 }'
  77. # root
  78. # daemon
  79. # bin
  80. # sys
  81. # sync
  82. # games
  83. # man
  84. # lp
  85. # mail
  86. # news
  87.  
  88. awk 'BEGIN { print ARGC }' /etc/passwd
  89. # 2
  90.  
  91. awk 'BEGIN { for (i = 0; i < ARGC; i++) print ARGV[i] }' /etc/passwd
  92. # awk
  93. # /etc/passwd
  94.  
  95. awk '
  96. BEGIN {
  97. for (i = 0; i < ARGC; i++) {
  98. print ARGV[i]
  99. }
  100. }' /etc/passwd
  101. # awk
  102. # /etc/passwd
  103.  
  104. awk 'BEGIN { print ENVIRON["HOME"] }'
  105. # /home/gokce
  106.  
  107. head /etc/passwd | awk -F: '{ printf "%d - %d\n", NR, NF }'
  108. # 1 - 7
  109. # 2 - 7
  110. # 3 - 7
  111. # 4 - 7
  112. # 5 - 7
  113. # 6 - 7
  114. # 7 - 7
  115. # 8 - 7
  116. # 9 - 7
  117. # 10 - 7
  118.  
  119. cat /etc/passwd | awk -F: '/bash/'
  120. # root:x:0:0:root:/root:/bin/bash
  121. # gokce:x:1000:1000:gokce,,,:/home/gokce:/bin/bash
  122.  
  123. cat /etc/passwd | awk -F: '/bash/ { print $0 }'
  124. # root:x:0:0:root:/root:/bin/bash
  125. # gokce:x:1000:1000:gokce,,,:/home/gokce:/bin/bash
  126.  
  127. cat /etc/passwd | awk -F: '/bash/ { print $1 }'
  128. # root
  129. # gokce
  130.  
  131. cat /etc/passwd | awk -F: '$7 == "/bin/bash" { print $1 }'
  132. # root
  133. # gokce
  134.  
  135. cat /etc/passwd | awk -F: 'NR == 1, NR == 5 { print $1 }'
  136. # root
  137. # daemon
  138. # bin
  139. # sys
  140. # sync
  141.  
  142. cat /etc/passwd | awk '
  143. { numlines++ }
  144. END { print numlines }'
  145. # 41
  146.  
  147. cat /etc/passwd | awk -F: '
  148. { shell[$7]++ }
  149. END { for (i in shell) printf "%-20s%d\n", i, shell[i] }'
  150. # /bin/sync 1
  151. # /bin/bash 2
  152. # /bin/false 22
  153. # /usr/sbin/nologin 16
  154.  
  155. awk 'BEGIN {
  156. "date" | getline line
  157. print line
  158. close("date")
  159. }'
  160. # Thu Oct 13 21:04:58 EEST 2016
  161.  
  162. awk 'BEGIN {
  163. cmd = "date -u"
  164. cmd | getline line
  165. print line
  166. close(cmd)
  167. }'
  168. # Thu Oct 13 18:05:03 UTC 2016
  169.  
  170. awk 'BEGIN {
  171. print "hello world" | "cat"
  172. close("cat")
  173. }'
  174. # hello world
  175.  
  176. awk 'BEGIN {
  177. print "ls -l /bin/echo" | "sh"
  178. close("sh")
  179. }'
  180. # -rwxr-xr-x 1 root root 31376 Feb 18 2016 /bin/echo
  181.  
  182. awk 'BEGIN { system("ls -l /bin/echo") }'
  183. # -rwxr-xr-x 1 root root 31376 Feb 18 2016 /bin/echo
  184.  
  185. awk '
  186. BEGIN {
  187. print fact(5)
  188. }
  189.  
  190. function fact(n) {
  191. if (n < 2)
  192. return 1;
  193. return n * fact(n-1);
  194. }'
  195. # 120
  196.  
  197. # mean
  198. seq 100 | shuf | head | awk '
  199. { sum += $0 }
  200. END { print sum / NR }'
  201. # 72.7
  202.  
  203. # escape '$' characters inside double quotes for aliasing
  204. alias mean="awk '{ sum += \$0 } END { print sum / NR }'"
  205.  
  206. seq 100 | shuf | head | mean
  207. # 49.2
  208.  
  209. # stdev
  210. seq 100 | shuf | head | awk '
  211. { sum += $0; sumsq += $0^2 }
  212. END { print sqrt(NR * sumsq - sum^2) / NR; }'
  213. # 29.3966
  214.  
  215. # escape '$' characters inside double quotes for aliasing
  216. alias stdev="awk '{ sum += \$0; sumsq += \$0^2 } END { print sqrt(NR * sumsq - sum^2) / NR; }'"
  217.  
  218. seq 100 | shuf | head | stdev
  219. # 27.7027
  220.  
  221. # from 20 random words generate a text with 100 random words
  222. shuf /usr/share/dict/words | head -20 | shuf -r | head -100 | fmt
  223. # updating poignantly poignantly lobotomies Intel's seesaw's baas vise's
  224. # hurray's Intel's Grampians Grampians settlement whooshing intertwines
  225. # Terrell lobotomies baas tiny Turing hurray's lobotomies hurray's idea's
  226. # underwriter's Grampians seesaw's Turing Intel's whooshing vise's idea's
  227. # Terrell baas underwriter's whooshing Turing Gorbachev freshets pluckier
  228. # settlement lobotomies baas Terrell settlement pluckier freshets pluckier
  229. # underwriter's Terrell baas lobotomies pluckier poignantly intertwines
  230. # tiny idea's settlement pluckier tiny Terrell baas settlement pluckier
  231. # idea's freshets vise's updating Grampians underwriter's settlement
  232. # intertwines hurray's poignantly vise's underwriter's updating baas idea's
  233. # intertwines Turing underwriter's underwriter's Grampians vise's baas
  234. # Terrell Terrell poignantly Grampians hurray's seesaw's pluckier Turing
  235. # lobotomies poignantly Terrell freshets underwriter's tiny
  236.  
  237. # word frequency (adapted from 'Effective awk Programming' by 'Arnold Robbins')
  238. shuf /usr/share/dict/words | head -20 | shuf -r | head -100 | fmt | awk '
  239. {
  240. $0 = tolower($0)
  241. gsub(/[^[:alnum:]_[:blank:]]/, "", $0)
  242. for (i = 1; i <= NF; i++) {
  243. freq[$i]++
  244. if (maxlen < length($i)) {
  245. maxlen = length($i)
  246. }
  247. }
  248. }
  249.  
  250. END {
  251. sort = "sort -k2 -rn | head -n 5"
  252. for (word in freq) {
  253. printf "%-*s%d\n", maxlen, word, freq[word] | sort
  254. }
  255. close(sort)
  256. }'
  257. # walmarts 11
  258. # hogs 7
  259. # trousseaux 6
  260. # trees 6
  261. # priming 6
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement