1

I want to add an escape (tty control) at the end of each line.

sed '/s/$/foo/g' appends foo to the end of the line. I want to append esc[K to the end of line (erase to end of line) I've tried

sed 's/$/\033[K/g'
sed 's/$/\\033[K/g'
sed 's/$/\e[K/g'
sed 's/$/\\e[K/g'

Neither produces an escape-character, just prints what I enter.

How can I get an escape-character in sed?

2
  • bash or some other shell? Commented Oct 7, 2020 at 20:45
  • bash. I can echo -e \\033[H; at the top of the bash script to move the cursor home.
    – Lenne
    Commented Oct 7, 2020 at 21:02

1 Answer 1

5

Try an escaped string

sed $'s/$/\033[K/g'

The $'…' tells the shell to handle escapes inside what is otherwise a literal single-quoted string

A better approach could be to append the Clear to EOL only if you're writing to a terminal:

test -t 1 && el=$(tput el)
…
sed 's/$/'"$el/"

Notice I switch quoting styles part way through - this helps the $ remain as a literal while allowing $el to be expanded to its value (if any). We could also have used "s/\$/$el/"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .