I have a real quick question for you guys. After looking through the documentation and the site here, I was wondering if it were possible to keep grep from outputting each match on the same line.

In my example, I need to take a string in the format of \xeb\x1a\x5e\x31\xc0\x88\x46\x07... etc. I use grep -oP "x\K(\S\S)") to get each of the hex digit.

The output is correct, however each match is on its own line. This is a problem because I need to feed this output into another program. So again, is there any way to put each match consecutively without any type of padding?


You could pipe the output to tr, e.g.

grep -oP "x\K(\S\S)") | tr -d '\n'

This costs an additional process, i.e. it makes your program slower. If this is a problem, you can use sed or awk.

  • Perfect! worked beautifully. I was planning to later use sed or awk but I've never used them and I am a bit tired to read the docs for those right now. – btf1998 Dec 3 '15 at 3:54

Does it work if you feed it to sed to remove all newline characters (\n)?

echo \xeb\x1a\x5e\x31\xc0\x88\x46\x07 | grep -oP "x\K(\S\S)" | sed ':a;N;$!ba;s/\n//g'

Reference: How can I replace a newline (\n) using sed?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.