I need grep's output to be indented with tabs/spaces.

This is the plain, un-indented version: MyCmd | grep "id:"

I tried this without success:MyCmd | grep "id:" | echo " "

  • 1
    Such questions shall always come with example input and example output. – Hauke Laging Jan 26 '16 at 12:41

You could do it with awk instead of grep if that's acceptable:

MyCmd | awk '/id:/ {print "   " $0}'

or if you need grep, sed could help:

MyCmd | grep "id:" | sed -e 's/^/   /'

The awk version does its own pattern match for lines that contain "id:" and then will print the spaces before the line. The sed version does the grep as you already did it but then replaces the start of each line (regex ^ matches the start of a line) with the spaces


Use just sed instead:

MyCmd | sed 's/^.*id:/\t&/'

The will match id: and introduce a tab at the start of the matched lines.


% sed 's/^foo/\t&/' <<<'foobar' 

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