Dave
Dave

Reputation: 567

How to remove invisible chars in bash (tr and sed not working)

I want to remove invisible chars from a response:

Here is my code:

test_id=`clasp run testRunner`
echo "visible"
echo "$test_id"
echo "invisible"
echo "$test_id" | cat -v
echo "invisible2"
echo "$test_id" | tr -dc '[:print:]' | cat -v
echo "invisible3"
echo "$test_id" | sed 's/[^a-zA-Z0-9]//g' | cat -v
echo "invisible4"
printf '%q\n' "$test_id"

Here's the output:

visible
1d5422fb
invisible
^[[2K^[[1G1d5422fb
invisible2
[2K[1G1d5422fbinvisible3
2K1G1d5422fb
invisible4
$'\E[2K\E[1G1d5422fb'

Upvotes: 0

Views: 374

Answers (3)

vvvvv
vvvvv

Reputation: 31720

echo "solution"
echo "$test_id" | perl -pe 's/\e([^\[\]]|\[.*?[a-zA-Z]|\].*?\a)//g' | cat -v

as per @Dave's edit on his own question.

Upvotes: 0

Diego Torres Milano
Diego Torres Milano

Reputation: 69368

Instead of removing the escape sequences prevent them from being generated, which I guess you can do with

test_id=$(TERM=dumb clasp run testRunner)

Upvotes: 0

melpomene
melpomene

Reputation: 85837

The following code works with your example:

shopt -s extglob
test_id=$'\e[2K\e[1G1d5422fb'
test_id="${test_id//$'\e['*([^a-zA-Z])[a-zA-Z]}"
echo "$test_id" | cat -v

The crucial part is the third line, which applies a string substitution to the expanded variable. It matches (and removes) all occurrences of the pattern

  • $'\e[' - a single Esc character followed by [
  • *( ... ) - (this is what extglob is needed for) zero or more occurrences of ...
    • [^a-zA-Z] - a single non-alphabetic character
  • [a-zA-Z] - a single alphabetic character

In your example this gets rid of the two escape sequences \e[2K (erase line) and \e[1G (move cursor to column 1).

Upvotes: 1

Related Questions