Velvet Star Monitor

Standout celebrity highlights with iconic style.

general

Keep grep output on one line?

Writer Andrew Mclaughlin

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?

2 Answers

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.

1

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

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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