6

With grep -o, I have a new line for every match:

# echo "a b a"|grep -o a
  a
  a

How can I obtain the following result?

 # echo "a b a"|grep -o a
  a      a
edwinksl
  • 23,789
user123456
  • 2,378

1 Answers1

4

You can pipe the output of grep to tr that you can then use to translate \n (newline) to \t (tab):

echo "a b a" | grep -o a | tr "\n" "\t"; echo

where the last echo is used to prevent the output of tr from being on the same line as your PS1.

For the specific example you give, this is how it looks like:

$ echo "a b a" | grep -o a | tr "\n" "\t"; echo
a   a
edwinksl
  • 23,789
  • This only works for the case of echoing a single string. This fails for the case of grepping through a file with multiple matches per line. Everything will be collapsed onto a single line. – Gabriel Devenyi Feb 07 '24 at 16:52