blob: 75d6ec5d25c87b36536ce1b0f23d152fbf7b7d44 [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 Landleyc7f0d6c2016-07-21 16:37:03 -05005testing '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 Landleyc7f0d6c2016-07-21 16:37:03 -05008testing '-n' 'sed -n ""' "" "" "one\ntwo\nthree"
9testing '-n p' 'sed -n p' "one\ntwo\nthree" "" "one\ntwo\nthree"
10testing 'explicit pattern' 'sed -e p -n' "one\ntwo\nthree" "" \
Rob Landleye7835d72014-11-27 20:38:21 -060011 "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 Landleyc7f0d6c2016-07-21 16:37:03 -050017testing '-n $p' 'sed -n \$p' "three" "" "one\ntwo\nthree"
18testing 'as cat #2' "sed -n '1,\$p'" "one\ntwo\nthree" "" "one\ntwo\nthree"
19testing 'no input means no last line' "sed '\$a boing'" "" "" ""
20testing '-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 Landleyc7f0d6c2016-07-21 16:37:03 -050024testing '$ 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 Landleyc7f0d6c2016-07-21 16:37:03 -050036testing 'prodigal newline' "sed -n '1,\$p' - input" \
Rob Landleye7835d72014-11-27 20:38:21 -060037 "one\ntwo\nthree\nfour\n" "four\n" "one\ntwo\nthree"
Rob Landleyc7f0d6c2016-07-21 16:37:03 -050038testing '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 Landleyc7f0d6c2016-07-21 16:37:03 -050042testing 'match \t tab' "sed -n '/\t/p'" "\tx\n" "" "\tx\n"
43testing 'match t delim disables \t tab' "sed -n '\t\txtp'" "" "" "\tx\n"
44testing 'match t delim makes \t literal t' \
Rob Landley448c8742014-10-29 18:44:33 -050045 "sed -n '\t\txtp'" "tx\n" "" "tx\n"
Rob Landleyc7f0d6c2016-07-21 16:37:03 -050046testing 'match n delim' "sed -n '\n\txnp'" "\tx\n" "" "\tx\n"
47testing 'match n delim disables \n newline' "sed -n '\n\nxnp'" "" "" "\nx\n"
48SKIP_HOST=1 testing 'match \n literal n' "sed -n '\n\nxnp'" "nx\n" "" "nx\n"
49testing '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 Landleyc7f0d6c2016-07-21 16:37:03 -050051testing '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"
Rob Landleyc7f0d6c2016-07-21 16:37:03 -050053testing 'num then regex' 'sed -n 2,/d/p' 'b\nc\nd\n' '' 'a\nb\nc\nd\ne\nf\n'
54testing 'regex then num' 'sed -n /b/,4p' 'b\nc\nd\n' '' 'a\nb\nc\nd\ne\nf\n'
55testing 'multiple regex address match' 'sed -n /on/,/off/p' \
Rob Landleyb069d8d2014-11-06 16:26:59 -060056 'bone\nturtle\scoff\ntron\nlurid\noffer\n' "" \
57 'zap\nbone\nturtle\scoff\nfred\ntron\nlurid\noffer\nbecause\n'
Rob Landleyc7f0d6c2016-07-21 16:37:03 -050058testing 'regex address overlap' 'sed -n /on/,/off/p' "on\nzap\noffon\n" "" \
Rob Landleyb069d8d2014-11-06 16:26:59 -060059 'on\nzap\noffon\nping\noff\n'
Rob Landley337c0722016-09-06 00:14:24 -050060testing 'getdelim with nested [:blah:]' 'sed -n "sa\a[a[:space:]bc]*aXXagp"' \
61 "ABXXCDXXEFXXGHXXIXX" "" "ABaaCDa EFaa aGHa a Ia "
Rob Landley64cbbab2016-10-17 23:52:51 -050062testing '[ in [[]' "sed 's@[[]@X@g'" "X" "" "["
63testing '[[] with ] as delimiter' "sed 's][[]]X]g'" "X" "" "["
64testing '[[] with [ as delimiter' "sed 's[\[\[][X['" "X" "" "["
Rob Landleyb069d8d2014-11-06 16:26:59 -060065
Rob Landleye7835d72014-11-27 20:38:21 -060066# gGhHlnNpPqrstwxy:=
67# s///#comment
68# abcdDi
69
Rob Landleyc7f0d6c2016-07-21 16:37:03 -050070testing 'prodigaler newline' 'sed -e a\\ -e woo' 'one\nwoo\n' '' 'one'
Rob Landley336c44a2016-03-02 15:20:04 -060071testing "aci" \
Rob Landleyb069d8d2014-11-06 16:26:59 -060072 "sed -e '3a boom' -e '/hre/i bang' -e '3a whack' -e '3c bong'" \
73 "one\ntwo\nbang\nbong\nboom\nwhack\nfour\n" "" \
74 "one\ntwo\nthree\nfour\n"
Rob Landley336c44a2016-03-02 15:20:04 -060075testing "b loop" "sed ':woo;=;b woo' | head -n 5" '1\n1\n1\n1\n1\n' "" "X"
76testing "b skip" "sed -n '2b zap;d;:zap;p'" "two\n" "" "one\ntwo\nthree"
77testing "b end" "sed -n '2b;p'" "one\nthree" "" "one\ntwo\nthree"
78testing "c range" "sed '2,4c blah'" "one\nblah\nfive\nsix" "" \
Rob Landleye7835d72014-11-27 20:38:21 -060079 "one\ntwo\nthree\nfour\nfive\nsix"
Rob Landley336c44a2016-03-02 15:20:04 -060080testing "c {range}" "sed -e '2,4{c blah' -e '}'" \
Rob Landleye7835d72014-11-27 20:38:21 -060081 "one\nblah\nblah\nblah\nfive\nsix" \
82 "" "one\ntwo\nthree\nfour\nfive\nsix"
Rob Landley336c44a2016-03-02 15:20:04 -060083testing "c multiple continuation" \
Rob Landleyc09b79d2014-12-21 23:17:06 -060084 "sed -e 'c\\' -e 'two\\' -e ''" "two\n\n" "" "hello"
Rob Landleyaf510342016-03-24 16:20:36 -050085SKIP_HOST=1 testing "c empty continuation" "sed -e 'c\\'" "\n" "" "hello"
Rob Landley336c44a2016-03-02 15:20:04 -060086testing "D further processing depends on whether line is blank" \
Rob Landleye7835d72014-11-27 20:38:21 -060087 "sed -e '/one/,/three/{' -e 'i meep' -e'N;2D;}'" \
88 "meep\nmeep\ntwo\nthree\n" "" "one\ntwo\nthree\n"
Rob Landleyc7f0d6c2016-07-21 16:37:03 -050089testing 'newline staying away' 'sed s/o/x/' 'xne\ntwx' '' 'one\ntwo'
Rob Landleye7835d72014-11-27 20:38:21 -060090
Rob Landleyb069d8d2014-11-06 16:26:59 -060091# Why on _earth_ is this not an error? There's a \ with no continuation!
Rob Landleye7835d72014-11-27 20:38:21 -060092#testing 'sed what, _really_?' 'sed -e a\\ && echo yes really' \
93# 'one\nyes really\n' '' 'one\n'
Rob Landleyeece7ed2014-11-09 14:16:33 -060094
Rob Landley1d5f48f2014-11-14 16:44:21 -060095# all the s/// test
96
Rob Landley336c44a2016-03-02 15:20:04 -060097testing "match empty line" "sed -e 's/^\$/@/'" "@\n" "" "\n"
Rob Landley1d5f48f2014-11-14 16:44:21 -060098
Rob Landleyc7f0d6c2016-07-21 16:37:03 -050099testing '\1' "sed 's/t\\(w\\)o/za\\1py/'" "one\nzawpy\nthree" "" \
Rob Landley1d5f48f2014-11-14 16:44:21 -0600100 "one\ntwo\nthree"
Rob Landleyc7f0d6c2016-07-21 16:37:03 -0500101testing '\1 p' "sed 's/t\\(w\\)o/za\\1py/p'" "one\nzawpy\nzawpy\nthree" \
Rob Landley1d5f48f2014-11-14 16:44:21 -0600102 "" "one\ntwo\nthree"
Rob Landleyc7f0d6c2016-07-21 16:37:03 -0500103testing '\1 no newline' "sed 's/t\\(w\\)o/za\\1py/'" "one\nzawpy" "" \
Rob Landley1d5f48f2014-11-14 16:44:21 -0600104 "one\ntwo"
Rob Landleyc7f0d6c2016-07-21 16:37:03 -0500105testing '\1 p no newline' "sed 's/t\\(w\\)o/za\\1py/p'" \
Rob Landley1d5f48f2014-11-14 16:44:21 -0600106 "one\nzawpy\nzawpy" "" "one\ntwo"
Rob Landleyc7f0d6c2016-07-21 16:37:03 -0500107testing '-n s//\1/p' "sed -n 's/t\\(w\\)o/za\\1py/p'" "zawpy" "" "one\ntwo"
108testing '-n s//\1/p no newline' "sed -n 's/t\\(w\\)o/za\\1py/p'" "zawpy" \
Rob Landley1d5f48f2014-11-14 16:44:21 -0600109 "" "one\ntwo"
Rob Landleyc7f0d6c2016-07-21 16:37:03 -0500110testing 'backref error' \
Rob Landley0c558f02014-11-15 16:16:29 -0600111 "sed 's/w/ale \2 ha/' >/dev/null 2>/dev/null || echo no" \
Rob Landley1d5f48f2014-11-14 16:44:21 -0600112 "no\n" "" "one\ntwo\nthree"
Rob Landleyc7f0d6c2016-07-21 16:37:03 -0500113testing 'empty match after nonempty match' "sed -e 's/a*/c/g'" 'cbcncgc' \
Rob Landley1d5f48f2014-11-14 16:44:21 -0600114 '' 'baaang'
Rob Landleyc7f0d6c2016-07-21 16:37:03 -0500115testing 'empty match' "sed -e 's/[^ac]*/A/g'" 'AaAcA' '' 'abcde'
116testing 's///#comment' "sed -e 's/TWO/four/i#comment'" "one\nfour\nthree" \
Rob Landley0c558f02014-11-15 16:16:29 -0600117 "" "one\ntwo\nthree"
Rob Landleye7835d72014-11-27 20:38:21 -0600118
Rob Landleyc7f0d6c2016-07-21 16:37:03 -0500119testing 'N flushes pending a and advances match counter' \
Rob Landleye7835d72014-11-27 20:38:21 -0600120 "sed -e 'a woo' -e 'N;\$p'" 'woo\none\ntwo\none\ntwo' "" 'one\ntwo'
Rob Landley336c44a2016-03-02 15:20:04 -0600121testing "delimiter in regex [char range] doesn't count" "sed -e 's/[/]//'" \
Rob Landleye7835d72014-11-27 20:38:21 -0600122 "onetwo\n" "" 'one/two\n'
Rob Landley336c44a2016-03-02 15:20:04 -0600123testing "delete regex range start line after trigger" \
Rob Landleye7835d72014-11-27 20:38:21 -0600124 "sed -e '/one/,/three/{' -e 'i meep' -e '1D;}'" \
125 "meep\nmeep\ntwo\nmeep\nthree" "" "one\ntwo\nthree"
Rob Landley336c44a2016-03-02 15:20:04 -0600126testing "blank pattern repeats last pattern" \
Rob Landley88d20772015-12-04 15:18:15 -0600127 "sed -e '/^three/s//abc&def/'" \
128 "one two three\nabcthreedef four five\nfive six seven\n" "" \
129 "one two three\nthree four five\nfive six seven\n"
Rob Landleye7835d72014-11-27 20:38:21 -0600130
Rob Landley807a50d2014-12-14 13:51:28 -0600131# Different ways of parsing line continuations
132
133testing "" "sed -e '1a\' -e 'huh'" "meep\nhuh\n" "" "meep"
134testing "" "sed -f input" "blah\nboom\n" '1a\\\nboom' 'blah'
Rob Landleyf9b9f8a2016-04-11 11:32:36 -0500135testing "" "sed -f - input" "blah\nboom\n" 'blah' '1a\\\nboom'
Rob Landley807a50d2014-12-14 13:51:28 -0600136testing "" "sed '1a\
137hello'" "merp\nhello\n" "" "merp"
Rob Landley8132ad22015-10-29 01:30:58 -0500138
139testing "" "sed -e '/x/c\' -e 'y'" 'y\n' '' 'x\n'
Rob Landley769341f2015-11-05 00:13:27 -0600140testing "" "sed -e 's/a[([]*b/X/'" 'X' '' 'a[(b'
Rob Landley32b35872016-02-17 19:21:44 -0600141testing "" "sed 'y/a\\bc/de\f/'" "db\f" "" "abc"
Rob Landley336c44a2016-03-02 15:20:04 -0600142testing "[a-a] (for perl)" "sed '"'s/\([^a-zA-Z0-9.:_\-\/]\)/\\\1/g'"'" \
Rob Landley32b35872016-02-17 19:21:44 -0600143 'he\ llo' "" "he llo"
Rob Landley8132ad22015-10-29 01:30:58 -0500144
Rob Landleyaf510342016-03-24 16:20:36 -0500145# Debian bug https://bugs.debian.org/635570 added code to ensure a file
146# ends with a newline via "sed -e '$a\'". Apparently all a\ with no additional
147# pattern lines after it does (other than making posix throw up) is
148# flush the pending newline as _if_ it had added another line. *shrug* Ok?
149testing "trailing a\ (for debian)" "sed 'a\\'" "hello\n" "" "hello"
150
Rob Landley2081ec62016-01-21 13:04:51 -0600151# You have to match the first line of a range in order to activate
152# the range, numeric and ascii work the same way
Rob Landley336c44a2016-03-02 15:20:04 -0600153testing "skip start of range" "sed -e n -e '1,2s/b/c/'" "a\nb\n" "" "a\nb\n"
Rob Landley2081ec62016-01-21 13:04:51 -0600154
Rob Landley807a50d2014-12-14 13:51:28 -0600155#echo meep | sed/sed -e '1a\' -e 'huh'
156#echo blah | sed/sed -f <(echo -e "1a\\\\\nboom")
157#echo merp | sed/sed "1a\\
158#hello"
159
Rob Landley336c44a2016-03-02 15:20:04 -0600160testing "bonus backslashes" \
Rob Landleya64e35b2015-03-28 20:21:03 -0500161 "sed -e 'a \l \x\' -e \"\$(echo -e 'ab\\\nc')\"" \
162 "hello\nl x\nab\nc\n" "" "hello\n"
Rob Landleye7835d72014-11-27 20:38:21 -0600163# -i with $ last line test