blob: 9f2956f803c880c7a903d4df86924de9bcc6cb64 [file] [log] [blame]
Rob Landley448c8742014-10-29 18:44:33 -05001#!/bin/echo Run scripts/test.sh
2
3#testing "name" "command" "result" "infile" "stdin"
4
Rob Landleyeece7ed2014-11-09 14:16:33 -06005testing 'sed as cat' 'sed ""' "one\ntwo\nthree" "" "one\ntwo\nthree"
Rob Landleye7835d72014-11-27 20:38:21 -06006# This segfaults ubuntu 12.04's sed. No really.
Rob Landleya64e35b2015-03-28 20:21:03 -05007SKIP_HOST=1 testing 'sed - - twice' 'sed "" - -' "hello\n" "" "hello\n"
Rob Landleye7835d72014-11-27 20:38:21 -06008testing 'sed -n' 'sed -n ""' "" "" "one\ntwo\nthree"
9testing 'sed -n p' 'sed -n p' "one\ntwo\nthree" "" "one\ntwo\nthree"
10testing 'sed explicit pattern' 'sed -e p -n' "one\ntwo\nthree" "" \
11 "one\ntwo\nthree"
Rob Landleyeece7ed2014-11-09 14:16:33 -060012
Rob Landley448c8742014-10-29 18:44:33 -050013# Exploring the wonders of sed addressing modes
14testing '' 'sed -n 1p' "one\n" "" "one\ntwo\nthree"
15testing '' 'sed 2p' "one\ntwo\ntwo\nthree" "" "one\ntwo\nthree"
16testing '' 'sed -n 2p' "two\n" "" "one\ntwo\nthree"
Rob Landley448c8742014-10-29 18:44:33 -050017testing 'sed -n $p' 'sed -n \$p' "three" "" "one\ntwo\nthree"
18testing 'sed as cat #2' "sed -n '1,\$p'" "one\ntwo\nthree" "" "one\ntwo\nthree"
Rob Landleye7835d72014-11-27 20:38:21 -060019testing 'sed no input means no last line' "sed '\$a boing'" "" "" ""
Rob Landleyeece7ed2014-11-09 14:16:33 -060020testing 'sed -n $,$p' 'sed -n \$,\$p' 'three' '' 'one\ntwo\nthree'
Rob Landley448c8742014-10-29 18:44:33 -050021testing '' 'sed -n 1,2p' "one\ntwo\n" "" "one\ntwo\nthree"
22testing '' 'sed -n 2,3p' "two\nthree" "" "one\ntwo\nthree"
23testing '' 'sed -n 2,1p' "two\n" "" "one\ntwo\nthree"
Rob Landleyeece7ed2014-11-09 14:16:33 -060024testing 'sed $ with 2 inputs' 'sed -n \$p - input' "four\n" "four\n" \
Rob Landley448c8742014-10-29 18:44:33 -050025 "one\ntwo\nthree"
Rob Landleyeece7ed2014-11-09 14:16:33 -060026testing '' 'sed -n /two/p' "two\n" "" "one\ntwo\nthree"
27testing '' 'sed -n 1,/two/p' 'one\ntwo\n' '' 'one\ntwo\nthree'
28testing '' 'sed -n /one/,2p' 'one\ntwo\n' '' 'one\ntwo\nthree'
29testing '' 'sed -n 1,/one/p' 'one\ntwo\nthree' '' 'one\ntwo\nthree'
30testing '' 'sed -n /one/,1p' 'one\n' '' 'one\ntwo\nthree'
31testing 'sed -n /two/,$p' 'sed -n /two/,\$p' 'two\nthree' '' 'one\ntwo\nthree'
32
33
34# Fun with newlines!
35testing '' 'sed -n 3p' "three" "" "one\ntwo\nthree"
Rob Landleye7835d72014-11-27 20:38:21 -060036testing 'sed prodigal newline' "sed -n '1,\$p' - input" \
37 "one\ntwo\nthree\nfour\n" "four\n" "one\ntwo\nthree"
38testing 'sed Newline only added if further output' "sed -n 3p - input" "three" \
Rob Landleyeece7ed2014-11-09 14:16:33 -060039 "four\n" "one\ntwo\nthree"
Rob Landley448c8742014-10-29 18:44:33 -050040
Rob Landleyeece7ed2014-11-09 14:16:33 -060041# Fun with match delimiters and escapes
Rob Landley448c8742014-10-29 18:44:33 -050042testing 'sed match \t tab' "sed -n '/\t/p'" "\tx\n" "" "\tx\n"
43testing 'sed match t delim disables \t tab' "sed -n '\t\txtp'" "" "" "\tx\n"
44testing 'sed match t delim makes \t literal t' \
45 "sed -n '\t\txtp'" "tx\n" "" "tx\n"
Rob Landleyb069d8d2014-11-06 16:26:59 -060046testing 'sed match n delim' "sed -n '\n\txnp'" "\tx\n" "" "\tx\n"
Rob Landleyeece7ed2014-11-09 14:16:33 -060047testing 'sed match n delim disables \n newline' "sed -n '\n\nxnp'" "" "" "\nx\n"
Rob Landley1d5f48f2014-11-14 16:44:21 -060048SKIP_HOST=1 testing 'sed match \n literal n' "sed -n '\n\nxnp'" "nx\n" "" "nx\n"
Rob Landleye7835d72014-11-27 20:38:21 -060049testing 'sed end match does not check starting match line' \
Rob Landleyb069d8d2014-11-06 16:26:59 -060050 "sed -n '/two/,/two/p'" "two\nthree" "" "one\ntwo\nthree"
Rob Landleye7835d72014-11-27 20:38:21 -060051testing 'sed end match/start match mixing number/letter' \
Rob Landleyb069d8d2014-11-06 16:26:59 -060052 "sed -n '2,/two/p'" "two\nthree" "" "one\ntwo\nthree"
53testing 'sed num then regex' 'sed -n 2,/d/p' 'b\nc\nd\n' '' 'a\nb\nc\nd\ne\nf\n'
54testing 'sed regex then num' 'sed -n /b/,4p' 'b\nc\nd\n' '' 'a\nb\nc\nd\ne\nf\n'
55testing 'sed multiple regex address match' 'sed -n /on/,/off/p' \
56 'bone\nturtle\scoff\ntron\nlurid\noffer\n' "" \
57 'zap\nbone\nturtle\scoff\nfred\ntron\nlurid\noffer\nbecause\n'
58testing 'sed regex address overlap' 'sed -n /on/,/off/p' "on\nzap\noffon\n" "" \
59 'on\nzap\noffon\nping\noff\n'
60
Rob Landleye7835d72014-11-27 20:38:21 -060061# gGhHlnNpPqrstwxy:=
62# s///#comment
63# abcdDi
64
65testing 'sed prodigaler newline' 'sed -e a\\ -e woo' 'one\nwoo\n' '' 'one'
Rob Landleyb069d8d2014-11-06 16:26:59 -060066testing "sed aci" \
67 "sed -e '3a boom' -e '/hre/i bang' -e '3a whack' -e '3c bong'" \
68 "one\ntwo\nbang\nbong\nboom\nwhack\nfour\n" "" \
69 "one\ntwo\nthree\nfour\n"
Rob Landleye7835d72014-11-27 20:38:21 -060070testing "sed b loop" "sed ':woo;=;b woo' | head -n 5" '1\n1\n1\n1\n1\n' "" "X"
71testing "sed b skip" "sed -n '2b zap;d;:zap;p'" "two\n" "" "one\ntwo\nthree"
72testing "sed b end" "sed -n '2b;p'" "one\nthree" "" "one\ntwo\nthree"
73testing "sed c range" "sed '2,4c blah'" "one\nblah\nfive\nsix" "" \
74 "one\ntwo\nthree\nfour\nfive\nsix"
75testing "sed c {range}" "sed -e '2,4{c blah' -e '}'" \
76 "one\nblah\nblah\nblah\nfive\nsix" \
77 "" "one\ntwo\nthree\nfour\nfive\nsix"
Rob Landleyc09b79d2014-12-21 23:17:06 -060078testing "sed c multiple continuation" \
79 "sed -e 'c\\' -e 'two\\' -e ''" "two\n\n" "" "hello"
Rob Landleye7835d72014-11-27 20:38:21 -060080testing "sed D further processing depends on whether line is blank" \
81 "sed -e '/one/,/three/{' -e 'i meep' -e'N;2D;}'" \
82 "meep\nmeep\ntwo\nthree\n" "" "one\ntwo\nthree\n"
Rob Landleyb069d8d2014-11-06 16:26:59 -060083testing 'sed newline staying away' 'sed s/o/x/' 'xne\ntwx' '' 'one\ntwo'
Rob Landleye7835d72014-11-27 20:38:21 -060084
Rob Landleyb069d8d2014-11-06 16:26:59 -060085# Why on _earth_ is this not an error? There's a \ with no continuation!
Rob Landleye7835d72014-11-27 20:38:21 -060086#testing 'sed what, _really_?' 'sed -e a\\ && echo yes really' \
87# 'one\nyes really\n' '' 'one\n'
Rob Landleyeece7ed2014-11-09 14:16:33 -060088
Rob Landley1d5f48f2014-11-14 16:44:21 -060089# all the s/// test
90
Rob Landleye7835d72014-11-27 20:38:21 -060091testing "sed match empty line" "sed -e 's/^\$/@/'" "@\n" "" "\n"
Rob Landley1d5f48f2014-11-14 16:44:21 -060092
93testing 'sed \1' "sed 's/t\\(w\\)o/za\\1py/'" "one\nzawpy\nthree" "" \
94 "one\ntwo\nthree"
95testing 'sed \1 p' "sed 's/t\\(w\\)o/za\\1py/p'" "one\nzawpy\nzawpy\nthree" \
96 "" "one\ntwo\nthree"
97testing 'sed \1 no newline' "sed 's/t\\(w\\)o/za\\1py/'" "one\nzawpy" "" \
98 "one\ntwo"
99testing 'sed \1 p no newline' "sed 's/t\\(w\\)o/za\\1py/p'" \
100 "one\nzawpy\nzawpy" "" "one\ntwo"
101testing 'sed -n s//\1/p' "sed -n 's/t\\(w\\)o/za\\1py/p'" "zawpy" "" "one\ntwo"
102testing 'sed -n s//\1/p no newline' "sed -n 's/t\\(w\\)o/za\\1py/p'" "zawpy" \
103 "" "one\ntwo"
Rob Landley0c558f02014-11-15 16:16:29 -0600104testing 'sed backref error' \
105 "sed 's/w/ale \2 ha/' >/dev/null 2>/dev/null || echo no" \
Rob Landley1d5f48f2014-11-14 16:44:21 -0600106 "no\n" "" "one\ntwo\nthree"
107testing 'sed empty match after nonempty match' "sed -e 's/a*/c/g'" 'cbcncgc' \
108 '' 'baaang'
109testing 'sed empty match' "sed -e 's/[^ac]*/A/g'" 'AaAcA' '' 'abcde'
Rob Landleye7835d72014-11-27 20:38:21 -0600110testing 'sed s///#comment' "sed -e 's/TWO/four/i#comment'" "one\nfour\nthree" \
Rob Landley0c558f02014-11-15 16:16:29 -0600111 "" "one\ntwo\nthree"
Rob Landleye7835d72014-11-27 20:38:21 -0600112
113testing 'sed N flushes pending a and advances match counter' \
114 "sed -e 'a woo' -e 'N;\$p'" 'woo\none\ntwo\none\ntwo' "" 'one\ntwo'
115testing "sed delimiter in regex [char range] doesn't count" "sed -e 's/[/]//'" \
116 "onetwo\n" "" 'one/two\n'
117testing "sed delete regex range start line after trigger" \
118 "sed -e '/one/,/three/{' -e 'i meep' -e '1D;}'" \
119 "meep\nmeep\ntwo\nmeep\nthree" "" "one\ntwo\nthree"
120testing "sed D further processing depends on whether line is blank" \
121 "sed -e '/one/,/three/{' -e 'i meep' -e'N;2D;}'" \
122 "meep\nmeep\ntwo\nthree\n" "" "one\ntwo\nthree\n"
123
Rob Landley807a50d2014-12-14 13:51:28 -0600124# Different ways of parsing line continuations
125
126testing "" "sed -e '1a\' -e 'huh'" "meep\nhuh\n" "" "meep"
127testing "" "sed -f input" "blah\nboom\n" '1a\\\nboom' 'blah'
128testing "" "sed '1a\
129hello'" "merp\nhello\n" "" "merp"
130#echo meep | sed/sed -e '1a\' -e 'huh'
131#echo blah | sed/sed -f <(echo -e "1a\\\\\nboom")
132#echo merp | sed/sed "1a\\
133#hello"
134
Rob Landleya64e35b2015-03-28 20:21:03 -0500135testing "sed bonus backslashes" \
136 "sed -e 'a \l \x\' -e \"\$(echo -e 'ab\\\nc')\"" \
137 "hello\nl x\nab\nc\n" "" "hello\n"
Rob Landleye7835d72014-11-27 20:38:21 -0600138# -i with $ last line test
139
140exit $FAILCOUNT