sed.test 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. #!/bin/echo Run scripts/test.sh
  2. #testing "name" "command" "result" "infile" "stdin"
  3. testing 'as cat' 'sed ""' "one\ntwo\nthree" "" "one\ntwo\nthree"
  4. # This segfaults ubuntu 12.04's sed. No really.
  5. testing 'sed - - twice' 'sed "" - -' "hello\n" "" "hello\n"
  6. testing '-n' 'sed -n ""' "" "" "one\ntwo\nthree"
  7. testing '-n p' 'sed -n p' "one\ntwo\nthree" "" "one\ntwo\nthree"
  8. testing 'explicit pattern' 'sed -e p -n' "one\ntwo\nthree" "" \
  9. "one\ntwo\nthree"
  10. # Exploring the wonders of sed addressing modes
  11. testing '' 'sed -n 1p' "one\n" "" "one\ntwo\nthree"
  12. testing '' 'sed 2p' "one\ntwo\ntwo\nthree" "" "one\ntwo\nthree"
  13. testing '' 'sed -n 2p' "two\n" "" "one\ntwo\nthree"
  14. testing '-n $p' 'sed -n \$p' "three" "" "one\ntwo\nthree"
  15. testing 'as cat #2' "sed -n '1,\$p'" "one\ntwo\nthree" "" "one\ntwo\nthree"
  16. testing 'no input means no last line' "sed '\$a boing'" "" "" ""
  17. testing '-n $,$p' 'sed -n \$,\$p' 'three' '' 'one\ntwo\nthree'
  18. testing '' 'sed -n 1,2p' "one\ntwo\n" "" "one\ntwo\nthree"
  19. testing '' 'sed -n 2,3p' "two\nthree" "" "one\ntwo\nthree"
  20. testing '' 'sed -n 2,1p' "two\n" "" "one\ntwo\nthree"
  21. testing '$ with 2 inputs' 'sed -n \$p - input' "four\n" "four\n" \
  22. "one\ntwo\nthree"
  23. testing '' 'sed -n /two/p' "two\n" "" "one\ntwo\nthree"
  24. testing '' 'sed -n 1,/two/p' 'one\ntwo\n' '' 'one\ntwo\nthree'
  25. testing '' 'sed -n /one/,2p' 'one\ntwo\n' '' 'one\ntwo\nthree'
  26. testing '' 'sed -n 1,/one/p' 'one\ntwo\nthree' '' 'one\ntwo\nthree'
  27. testing '' 'sed -n /one/,1p' 'one\n' '' 'one\ntwo\nthree'
  28. testing 'sed -n /two/,$p' 'sed -n /two/,\$p' 'two\nthree' '' 'one\ntwo\nthree'
  29. # Fun with newlines!
  30. testing '' 'sed -n 3p' "three" "" "one\ntwo\nthree"
  31. testing 'prodigal newline' "sed -n '1,\$p' - input" \
  32. "one\ntwo\nthree\nfour\n" "four\n" "one\ntwo\nthree"
  33. testing 'Newline only added if further output' "sed -n 3p - input" "three" \
  34. "four\n" "one\ntwo\nthree"
  35. # Fun with match delimiters and escapes
  36. testing 'match \t tab' "sed -n '/\t/p'" "\tx\n" "" "\tx\n"
  37. testing 'match t delim disables \t tab' "sed -n '\t\txtp'" "" "" "\tx\n"
  38. testing 'match t delim makes \t literal t' \
  39. "sed -n '\t\txtp'" "tx\n" "" "tx\n"
  40. testing 'match n delim' "sed -n '\n\txnp'" "\tx\n" "" "\tx\n"
  41. testing 'match n delim disables \n newline' "sed -n '\n\nxnp'" "" "" "\nx\n"
  42. toyonly testing 'match \n literal n' "sed -n '\n\nxnp'" "nx\n" "" "nx\n"
  43. testing 'end match does not check starting match line' \
  44. "sed -n '/two/,/two/p'" "two\nthree" "" "one\ntwo\nthree"
  45. testing 'end match/start match mixing number/letter' \
  46. "sed -n '2,/two/p'" "two\nthree" "" "one\ntwo\nthree"
  47. testing 'num then regex' 'sed -n 2,/d/p' 'b\nc\nd\n' '' 'a\nb\nc\nd\ne\nf\n'
  48. testing 'regex then num' 'sed -n /b/,4p' 'b\nc\nd\n' '' 'a\nb\nc\nd\ne\nf\n'
  49. testing 'multiple regex address match' 'sed -n /on/,/off/p' \
  50. 'bone\nturtle\scoff\ntron\nlurid\noffer\n' "" \
  51. 'zap\nbone\nturtle\scoff\nfred\ntron\nlurid\noffer\nbecause\n'
  52. testing 'regex address overlap' 'sed -n /on/,/off/p' "on\nzap\noffon\n" "" \
  53. 'on\nzap\noffon\nping\noff\n'
  54. testing 'getdelim with nested [:blah:]' 'sed -n "sa\a[a[:space:]bc]*aXXagp"' \
  55. "ABXXCDXXEFXXGHXXIXX" "" "ABaaCDa EFaa aGHa a Ia "
  56. testing '[ in [[]' "sed 's@[[]@X@g'" "X" "" "["
  57. testing '[[] with ] as delimiter' "sed 's][[]]X]g'" "X" "" "["
  58. testing '[[] with [ as delimiter' "sed 's[\[\[][X['" "X" "" "["
  59. # gGhHlnNpPqrstwxy:=
  60. # s///#comment
  61. # abcdDi
  62. testing 'prodigaler newline' 'sed -e a\\ -e woo' 'one\nwoo\n' '' 'one'
  63. testing "aci" \
  64. "sed -e '3a boom' -e '/hre/i bang' -e '3a whack' -e '3c bong'" \
  65. "one\ntwo\nbang\nbong\nboom\nwhack\nfour\n" "" \
  66. "one\ntwo\nthree\nfour\n"
  67. testing "b loop" "sed ':woo;=;b woo' | head -n 5" '1\n1\n1\n1\n1\n' "" "X"
  68. testing "b skip" "sed -n '2b zap;d;:zap;p'" "two\n" "" "one\ntwo\nthree"
  69. testing "b end" "sed -n '2b;p'" "one\nthree" "" "one\ntwo\nthree"
  70. testing "c range" "sed '2,4c blah'" "one\nblah\nfive\nsix" "" \
  71. "one\ntwo\nthree\nfour\nfive\nsix"
  72. testing "c {range}" "sed -e '2,4{c blah' -e '}'" \
  73. "one\nblah\nblah\nblah\nfive\nsix" \
  74. "" "one\ntwo\nthree\nfour\nfive\nsix"
  75. testing "c multiple continuation" \
  76. "sed -e 'c\\' -e 'two\\' -e ''" "two\n\n" "" "hello"
  77. toyonly testing "c empty continuation" "sed -e 'c\\'" "\n" "" "hello"
  78. testing "D further processing depends on whether line is blank" \
  79. "sed -e '/one/,/three/{' -e 'i meep' -e'N;2D;}'" \
  80. "meep\nmeep\ntwo\nthree\n" "" "one\ntwo\nthree\n"
  81. testing 'newline staying away' 'sed s/o/x/' 'xne\ntwx' '' 'one\ntwo'
  82. # Why on _earth_ is this not an error? There's a \ with no continuation!
  83. #testing 'sed what, _really_?' 'sed -e a\\ && echo yes really' \
  84. # 'one\nyes really\n' '' 'one\n'
  85. # all the s/// test
  86. testing "match empty line" "sed -e 's/^\$/@/'" "@\n" "" "\n"
  87. testing '\1' "sed 's/t\\(w\\)o/za\\1py/'" "one\nzawpy\nthree" "" \
  88. "one\ntwo\nthree"
  89. testing '\1 p' "sed 's/t\\(w\\)o/za\\1py/p'" "one\nzawpy\nzawpy\nthree" \
  90. "" "one\ntwo\nthree"
  91. testing '\1 no newline' "sed 's/t\\(w\\)o/za\\1py/'" "one\nzawpy" "" \
  92. "one\ntwo"
  93. testing '\1 p no newline' "sed 's/t\\(w\\)o/za\\1py/p'" \
  94. "one\nzawpy\nzawpy" "" "one\ntwo"
  95. testing '-n s//\1/p' "sed -n 's/t\\(w\\)o/za\\1py/p'" "zawpy" "" "one\ntwo"
  96. testing '-n s//\1/p no newline' "sed -n 's/t\\(w\\)o/za\\1py/p'" "zawpy" \
  97. "" "one\ntwo"
  98. testing 'backref error' \
  99. "sed 's/w/ale \2 ha/' >/dev/null 2>/dev/null || echo no" \
  100. "no\n" "" "one\ntwo\nthree"
  101. testing 'empty match after nonempty match' "sed -e 's/a*/c/g'" 'cbcncgc' \
  102. '' 'baaang'
  103. testing 'empty match' "sed -e 's/[^ac]*/A/g'" 'AaAcA' '' 'abcde'
  104. testing 's///#comment' "sed -e 's/TWO/four/i#comment'" "one\nfour\nthree" \
  105. "" "one\ntwo\nthree"
  106. testing 's///num off end' 'sed -e s/e//2' 'e\n' '' 'e\n'
  107. testing 'N flushes pending a and advances match counter' \
  108. "sed -e 'a woo' -e 'N;\$p'" 'woo\none\ntwo\none\ntwo' "" 'one\ntwo'
  109. testing "delimiter in regex [char range] doesn't count" "sed -e 's/[/]//'" \
  110. "onetwo\n" "" 'one/two\n'
  111. testing "delete regex range start line after trigger" \
  112. "sed -e '/one/,/three/{' -e 'i meep' -e '1D;}'" \
  113. "meep\nmeep\ntwo\nmeep\nthree" "" "one\ntwo\nthree"
  114. testing "blank pattern repeats last pattern" \
  115. "sed -e '/^three/s//abc&def/'" \
  116. "one two three\nabcthreedef four five\nfive six seven\n" "" \
  117. "one two three\nthree four five\nfive six seven\n"
  118. testcmd "interleave -e and -f" "-e 'a abc' -f input -e 'a ghi'" \
  119. "hello\nabc\ndef\nghi\n" "a def" "hello\n"
  120. # Different ways of parsing line continuations
  121. testing "" "sed -e '1a\' -e 'huh'" "meep\nhuh\n" "" "meep"
  122. testing "" "sed -f input" "blah\nboom\n" '1a\\\nboom' 'blah'
  123. testing "" "sed -f - input" "blah\nboom\n" 'blah' '1a\\\nboom'
  124. testing "" "sed '1a\
  125. hello'" "merp\nhello\n" "" "merp"
  126. testing "" "sed -e '/x/c\' -e 'y'" 'y\n' '' 'x\n'
  127. testing "" "sed -e 's/a[([]*b/X/'" 'X' '' 'a[(b'
  128. toyonly testing "" "sed 'y/a\\bc/de\f/'" "db\f" "" "abc"
  129. testing "[a-a] (for perl)" "sed '"'s/\([^a-zA-Z0-9.:_\-\/]\)/\\\1/g'"'" \
  130. 'he\ llo' "" "he llo"
  131. # Debian bug https://bugs.debian.org/635570 added code to ensure a file
  132. # ends with a newline via "sed -e '$a\'". Apparently all a\ with no additional
  133. # pattern lines after it does (other than making posix throw up) is
  134. # flush the pending newline as _if_ it had added another line. *shrug* Ok?
  135. testing "trailing a\ (for debian)" "sed 'a\\'" "hello\n" "" "hello"
  136. # You have to match the first line of a range in order to activate
  137. # the range, numeric and ascii work the same way
  138. toyonly testing "skip start of range" "sed -e n -e '1,2s/b/c/'" "a\nb\n" "" "a\nb\n"
  139. testing "range +1" "sed -ne '/blah/,+1p'" "blah\n6\n" "" \
  140. "1\n2\n3\n4\n5\nblah\n6\n7\n8\n9\n"
  141. testing "range +0" "sed -ne '/blah/,+0p'" "blah\n" "" \
  142. "1\n2\n3\n4\n5\nblah\n6\n7\n8\n9\n"
  143. testing "range +3" "sed -ne '2,+3p'" "2\n3\n4\n5\n" "" \
  144. "1\n2\n3\n4\n5\nblah\n6\n7\n8\n9\n"
  145. testing "not -s" "sed -n 1p input -" "one" "one" "two"
  146. testing "-s" "sed -sn 1p input -" "one\ntwo" "one\n" "two"
  147. testing "bonus backslashes" \
  148. "sed -e 'a \l \x\' -e \"\$(echo -e 'ab\\\nc')\"" \
  149. "hello\nl x\nab\nc\n" "" "hello\n"
  150. # toybox saying "no }" here broke the AOSP build.
  151. testing "end b with }" "sed -n '/START/{:a;n;/END/q;p;ba}'" "b\nc\n" \
  152. "" "a\nSTART\nb\nc\nEND\nd"
  153. testcmd '-z' '-z "s/\n/-/g"' "a-b-c" "" "a\nb\nc"
  154. testcmd '-z N' '-z N' 'one\0two\0' '' 'one\0two\0'
  155. testcmd 'p noeol' '-z p' 'one\0one' '' 'one'
  156. testcmd '-z N noeol' '-z N' 'one\0two' '' 'one\0two'
  157. testcmd '-z S' "-z 'N;P'" 'one\0one\0two' '' 'one\0two'
  158. testcmd '-z D' "-z 'N;D'" 'two' '' 'one\0two'
  159. testcmd '-z G' "-z 'h;G'" 'one\0one' '' 'one'
  160. testcmd '-z H' "-z 'H;g'" '\0one' '' 'one'
  161. toyonly testcmd '-z x NOEOL' '-z ax' 'abc\0x\0def\0x\0' '' 'abc\0def'
  162. testcmd 's after NUL' 's/t/x/' 'one\0xwo' '' 'one\0two'
  163. testcmd '^ not trigger after NUL' 's/^t/x/' 'one\0two' '' 'one\0two'
  164. # toybox handling of empty capturing groups broke minjail. Check that we
  165. # correctly replace an empty capturing group with the empty string:
  166. testing '\n with empty capture' \
  167. 'sed -E "s/(ARM_)?(NR_)([a-z]*) (.*)/\1\2\3/"' "NR_read" "" "NR_read foo"
  168. # ...but also that we report an error for a backreference to a group that
  169. # isn't in the pattern:
  170. testing '\n too high' \
  171. 'sed -E "s/(.*)/\2/p" 2>/dev/null || echo OK' "OK\n" "" "foo"
  172. toyonly testing 's///x' 'sed "s/(hello )?(world)/\2/x"' "world" "" "hello world"
  173. # Performance test
  174. X=x; Y=20; while [ $Y -gt 0 ]; do X=$X$X; Y=$(($Y-1)); done
  175. testing 'megabyte s/x/y/g (20 sec timeout)' \
  176. "timeout 20 sed 's/x/y/g' | sha1sum" \
  177. '138c1fa7c3f64186203b0192fb4abdb33cb4e98a -\n' '' "$X\n"
  178. unset X Y
  179. testing "w doesn't blank" "sed -e 'w one' -e 'w one' -e d; cat one" \
  180. 'hello\nhello\n' '' 'hello\n'
  181. testing 's i and I' 'sed s/o/0/ig' "f00l F00L" "" "fool FOOL"
  182. testing 's l ignores posix' "sed -n 'N;l'" 'one\\ntwo$\n' '' 'one\ntwo\n'
  183. testing 's l loses missing newline' "sed -n 'N;l'" 'one\\ntwo$\n' '' 'one\ntwo'
  184. testing 's -z l' "sed -zn 'N;l'" 'one\\000two$\0' '' 'one\0two\0'
  185. testing 's -z l missing newline' "sed -zn 'N;l'" 'one\\000two$\0' '' 'one\0two'
  186. # -i with $ last line test
  187. #echo meep | sed/sed -e '1a\' -e 'huh'
  188. #echo blah | sed/sed -f <(echo -e "1a\\\\\nboom")
  189. #echo merp | sed/sed "1a\\
  190. #hello"