4

I have a bash script that checks database for changes and emails me these changes. Works nice, until it tries to send something non-printable, which 'breaks' the mail.

Is there any handy way how to replace all non-printable characters from a string with their hexadecimal code (something like "abc<1A>def<07>xyz")?

All I can think of is a looong chain of sed commands handing single character each. Which doesn't seem right.

edit: After posting this question, I found cat -v command doing mostly what I wanted. Using this for now, unless someone points something better.

2 Answers 2

5

You can use a simple substitution like

perl -pe 's/([^[:print:]\n])/sprintf("<%02X>", ord($1))/ge'

The less command has an output format quite similar to what you specify, but it prints the control characters in your example string literally for me. Perhaps you have more luck with it (I'm thinking my system is too modern :-). It should not perform paging when output is not to a terminal, so it should just pass through -- hopefully, with the desired substitutions -- when you run it from your crontab.

0

Use an incremental 'for' with ASCII groups.

Those are the hex values you should cover (ASCII): 20-2F 3A-40 5B-60 7B-FF

So basically with just 4 fors, you cover all characters except numbers and letters. Excluding more complicates things (like if you do not wat to remove "-" or "_" for example).

5
  • I don't understand this answer. Do you mean a for loop in the shell?
    – tripleee
    Dec 8, 2017 at 9:51
  • You can use any type of conditional loop you prefer.
    – Overmind
    Dec 8, 2017 at 9:53
  • But so that's what you are saying, loop over ranges of characters in turn? That's going to be hideously inefficient compared to a single regex.
    – tripleee
    Dec 8, 2017 at 9:56
  • As I said, it's up to the preference. As I have all needed ASCII ranges already defined I actually can use a single variable in any syntax.
    – Overmind
    Dec 8, 2017 at 10:20
  • I understand how to write a program to do this but without that program, this doesn't really answer the question.
    – tripleee
    Dec 8, 2017 at 10:25

You must log in to answer this question.

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