Keep grep output on one line?
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.
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?