0

Suppose I have a output in stdout like this:

e28f6001
e12fff16
2210
4679
df01
6c656873

How can I pipe this in bash so I can append \x for each byte. I want the the output like this:

\xe2\x8f\x60\x01\xe1\x2f\xff\x16\x22\x10 ...
4
  • 3
    sed -e 's/../\\x&/g' though that won't join the lines. Commented Jul 2, 2015 at 13:50
  • 2
    As usual, what have you tried ? Commented Jul 2, 2015 at 13:51
  • 3
    sed -r 's/([a-fA-F0-9]{2})/\\x\1/g' file | tr -d '\n' Commented Jul 2, 2015 at 13:53
  • 1
    Seriously, show a little effort please. tr -d '\n ' Commented Jul 2, 2015 at 13:59

1 Answer 1

2
tr -d '\n' < File | sed 's/.\{2\}/\\x&/g'

Remove newline first. Then substitute every 2 characters (.\{2\}) with \x followed by the 2 characters (& => the matched pattern which will be those 2 characters).

Sign up to request clarification or add additional context in comments.

1 Comment

You probably picked up the downvote when your answer (which you have since edited within the 5 minute window) consisted of a single line of code with no explanation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.