PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/oneline.yml

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