PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/ubyubyuby.yml

https://github.com/felixgao/chitsheet
YAML | 211 lines | 90 code | 58 blank | 63 comment | 0 complexity | 9104777c330879f3bb5ff96425696abc MD5 | raw file
  1. ---
  2. ubyubyuby: |-
  3. # double space a file
  4. $ ruby -pe 'puts' < file.txt
  5. # triple space a file
  6. $ ruby -pe '2.times {puts}' < file.txt
  7. # undo double-spacing (w/ and w/o whitespace in lines)
  8. $ ruby -lne 'BEGIN{$/="\n\n"}; puts $_' < file.txt
  9. $ ruby -ne 'BEGIN{$/="\n\n"}; puts $_.chomp' < file.txt
  10. $ ruby -e 'puts STDIN.readlines.to_s.gsub(/\n\n/, "\n")' < file.txt
  11. NUMBERING:
  12. # number each line of a file (left justified).
  13. $ ruby -ne 'printf("%-6s%s", $., $_)' < file.txt
  14. # number each line of a file (right justified).
  15. $ ruby -ne 'printf("%6s%s", $., $_)' < file.txt
  16. # number each line of a file, only print non-blank lines
  17. $ ruby -e 'while gets; end; puts $.' < file.txt
  18. # count lines (emulates 'wc -l')
  19. $ ruby -ne 'END {puts $.}' < file.txt
  20. $ ruby -e 'while gets; end; puts $.' < file.txt
  21. TEXT CONVERSION AND SUBSTITUTION:
  22. # convert DOS newlines (CR/LF) to Unix format (LF)
  23. # - strip newline regardless; re-print with unix EOL
  24. $ ruby -ne 'BEGIN{$\="\n"}; print $_.chomp' < file.txt
  25. # convert Unix newlines (LF) to DOS format (CR/LF)
  26. # - strip newline regardless; re-print with dos EOL
  27. $ ruby -ne 'BEGIN{$\="\r\n"}; print $_.chomp' < file.txt
  28. # delete leading whitespace (spaces/tabs/etc) from beginning of each line
  29. $ ruby -pe 'gsub(/^\s+/, "")' < file.txt
  30. # delete trailing whitespace (spaces/tabs/etc) from end of each line
  31. # - strip newline regardless; replace with default platform record separator
  32. $ ruby -pe 'gsub(/\s+$/, $/)' < file.txt
  33. # delete BOTH leading and trailing whitespace from each line
  34. $ ruby -pe 'gsub(/^\s+/, "").gsub(/\s+$/, $/)' < file.txt
  35. # insert 5 blank spaces at the beginning of each line (ie. page offset)
  36. $ ruby -pe 'gsub(/%/, " ")' < file.txt
  37. FAILS! $ ruby -pe 'gsub(/%/, 5.times{putc " "})' < file.txt
  38. # align all text flush right on a 79-column width
  39. $ ruby -ne 'printf("%79s", $_)' < file.txt
  40. # center all text in middle of 79-column width
  41. $ ruby -ne 'puts $_.chomp.center(79)' < file.txt
  42. $ ruby -lne 'puts $_.center(79)' < file.txt
  43. # substitute (find and replace) "foo" with "bar" on each line
  44. $ ruby -pe 'gsub(/foo/, "bar")' < file.txt
  45. # substitute "foo" with "bar" ONLY for lines which contain "baz"
  46. $ ruby -pe 'gsub(/foo/, "bar") if $_ =~ /baz/' < file.txt
  47. # substitute "foo" with "bar" EXCEPT for lines which contain "baz"
  48. $ ruby -pe 'gsub(/foo/, "bar") unless $_ =~ /baz/' < file.txt
  49. # substitute "foo" or "bar" or "baz".... with "baq"
  50. $ ruby -pe 'gsub(/(foo|bar|baz)/, "baq")' < file.txt
  51. # reverse order of lines (emulates 'tac') IMPROVE
  52. $ ruby -ne 'BEGIN{@arr=Array.new}; @arr.push $_; END{puts @arr.reverse}' <
  53. file.txt
  54. # reverse each character on the line (emulates 'rev')
  55. $ ruby -ne 'puts $_.chomp.reverse' < file.txt
  56. $ ruby -lne 'puts $_.reverse' < file.txt
  57. # join pairs of lines side-by-side (like 'paste')
  58. $ ruby -pe '$_ = $_.chomp + " " + gets if $. % 2' < file.txt
  59. # if a line ends with a backslash, append the next line to it
  60. $ ruby -pe 'while $_.match(/\\$/); $_ = $_.chomp.chop + gets; end' <
  61. file.txt
  62. $ ruby -e 'puts STDIN.readlines.to_s.gsub(/\\\n/, "")' < file.txt
  63. # if a line begins with an equal sign, append it to the previous line (Unix)
  64. $ ruby -e 'puts STDIN.readlines.to_s.gsub(/\n=/, "")' < file.txt
  65. # add a blank line every 5 lines (after lines 5, 10, 15, etc)
  66. $ ruby -pe 'puts if $. % 6 == 0' < file.txt
  67. SELECTIVE PRINTING OF CERTAIN LINES
  68. # print first 10 lines of a file (emulate 'head')
  69. $ ruby -pe 'exit if $. > 10' < file.txt
  70. # print first line of a file (emulate 'head -1')
  71. $ ruby -pe 'puts $_; exit' < file.txt
  72. # print the last 10 lines of a file (emulate 'tail'); NOTE reads entire file!
  73. $ ruby -e 'puts STDIN.readlines.reverse!.slice(0,10).reverse!' < file.txt
  74. # print the last 2 lines of a file (emulate 'tail -2'); NOTE reads entire file!
  75. $ ruby -e 'puts STDIN.readlines.reverse!.slice(0,2).reverse!' < file.txt
  76. # print the last line of a file (emulates 'tail -1')
  77. $ ruby -ne 'line = $_; END {puts line}' < file.txt
  78. # print only lines that match a regular expression (emulates 'grep')
  79. $ ruby -pe 'next unless $_ =~ /regexp/' < file.txt
  80. # print only lines that DO NOT match a regular expression (emulates 'grep')
  81. $ ruby -pe 'next if $_ =~ /regexp/' < file.txt
  82. # print the line immediately before a regexp, but not the regex matching line
  83. $ ruby -ne 'puts @prev if $_ =~ /regex/; @prev = $_;' < file.txt
  84. # print the line immediately after a regexp, but not the regex matching line
  85. $ ruby -ne 'puts $_ if @prev =~ /regex/; @prev = $_;' < file.txt
  86. # grep for foo AND bar AND baz (in any order)
  87. $ ruby -pe 'next unless $_ =~ /foo/ && $_ =~ /bar/ && $_ =~ /baz/' <
  88. file.txt
  89. # grep for foo AND bar AND baz (in order)
  90. $ ruby -pe 'next unless $_ =~ /foo.*bar.*baz/' < file.txt
  91. # grep for foo OR bar OR baz
  92. $ ruby -pe 'next unless $_ =~ /(foo|bar|baz)/' < file.txt
  93. # print paragraph if it contains regexp; blank lines separate paragraphs
  94. $ ruby -ne 'BEGIN{$/="\n\n"}; print $_ if $_ =~ /regexp/' < file.txt
  95. # print paragraph if it contains foo AND bar AND baz (in any order); blank lines
  96. separate paragraphs
  97. $ ruby -ne 'BEGIN{$/="\n\n"}; print $_ if $_ =~ /foo/ && $_ =~ /bar/ && $_
  98. =~ /baz/' < file.txt
  99. # print paragraph if it contains foo AND bar AND baz (in order); blank lines
  100. separate paragraphs
  101. $ ruby -ne 'BEGIN{$/="\n\n"}; print $_ if $_ =~ /(foo.*bar.*baz)/' <
  102. file.txt
  103. # print paragraph if it contains foo OR bar OR baz; blank lines separate
  104. paragraphs
  105. $ ruby -ne 'BEGIN{$/="\n\n"}; print $_ if $_ =~ /(foo|bar|baz)/' < file.txt
  106. # print only lines of 65 characters or greater
  107. $ ruby -pe 'next unless $_.chomp.length >= 65' < file.txt
  108. $ ruby -lpe 'next unless $_.length >= 65' < file.txt
  109. # print only lines of 65 characters or less
  110. $ ruby -pe 'next unless $_.chomp.length < 65' < file.txt
  111. $ ruby -lpe 'next unless $_.length < 65' < file.txt
  112. # print section of file from regex to end of file
  113. $ ruby -pe '@found=true if $_ =~ /regex/; next unless @found' < file.txt
  114. # print section of file based on line numbers (eg. lines 2-7 inclusive)
  115. $ ruby -pe 'next unless $. >= 2 && $. <= 7' < file.txt
  116. # print line number 52
  117. $ ruby -pe 'next unless $. == 52' < file.txt
  118. # print every 3rd line starting at line 4
  119. $ ruby -pe 'next unless $. >= 4 && $. % 3 == 0' < file.txt
  120. # print section of file between two regular expressions, /foo/ and /bar/
  121. $ ruby -ne '@found=true if $_ =~ /foo/; next unless @found; puts $_; exit
  122. if $_ =~ /bar/' < file.txt
  123. SELECTIVE DELETION OF CERTAIN LINES
  124. # print all of file except between two regular expressions, /foo/ and /bar/
  125. $ ruby -ne '@found = true if $_ =~ /foo/; puts $_ unless @found; @found =
  126. false if $_ =~ /bar/' < file.txt
  127. # print file and remove duplicate, consecutive lines from a file (emulates
  128. 'uniq')
  129. $ ruby -ne 'puts $_ unless $_ == @prev; @prev = $_' < file.txt
  130. # print file and remove duplicate, non-consecutive lines from a file (careful of
  131. memory!)
  132. $ ruby -e 'puts STDIN.readlines.sort.uniq!.to_s' < file.txt
  133. # print file except for first 10 lines
  134. $ ruby -pe 'next if $. <= 10' < file.txt
  135. # print file except for last line
  136. $ ruby -e 'lines=STDIN.readlines; puts lines[0,lines.size-1]' < file.txt
  137. # print file except for last 2 lines
  138. $ ruby -e 'lines=STDIN.readlines; puts lines[0,lines.size-2]' < file.txt
  139. # print file except for last 10 lines
  140. $ ruby -e 'lines=STDIN.readlines; puts lines[0,lines.size-10]' < file.txt
  141. # print file except for every 8th line
  142. $ ruby -pe 'next if $. % 8 == 0' < file.txt
  143. # print file except for blank lines
  144. $ ruby -pe 'next if $_ =~ /^\s*$/' < file.txt
  145. # delete all consecutive blank lines from a file except the first
  146. $ ruby -e 'BEGIN{$/=nil}; puts STDIN.readlines.to_s.gsub(/\n(\n)+/,
  147. "\n\n")' < file.txt
  148. # delete all consecutive blank lines from a file except for the first 2
  149. $ ruby -e 'BEGIN{$/=nil}; puts STDIN.readlines.to_s.gsub(/\n(\n)+/,
  150. "\n\n")' < file.txt
  151. # delete all leading blank lines at top of file
  152. $ ruby -pe '@lineFound = true if $_ !~ /^\s*$/; next if !@lineFound' <
  153. file.txt