- escaped=
- while :; do
- case $i in
- *'
-'*)
- char='
-' repl='\\n';;
- *'&'*) char='&' repl='\&';;
- *'\'*) char='\\' repl='\\';;
- *'|'*) char='|' repl='\|';;
- *) break;;
- esac
- up_to_first_char="\\([^$char]*\\)"
- after_first_char="[^$char]*$char\\(.*\\)"
- escaped=$escaped`expr "X$i" : "X$up_to_first_char"`$repl
- i=`expr "X$i" : "$after_first_char"`
- done
- if test $with_filename -eq 1; then
- sed_script="s|[^:]*|$escaped$i|"
- else
- sed_script="s|^|$escaped$i:|"
- fi
-
- # Fail if either grep or sed fails.
- # Bash has ${PIPESTATUS[0]}, but that's not portable.
- exec 3>&1
- r=`
+ case $i in
+ (*'
+'* | *'&'* | *'\'* | *'|'*)
+ i=$(printf '%s\n' "$i" |
+ sed '
+ $!N
+ $s/[&\|]/\\&/g
+ $s/\n/\\n/g
+ ');;
+ esac
+ sed_script="s|^|$i:|"
+
+ # Fail if grep or sed fails.
+ r=$(