0

This question already has an answer here:

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 " "

marked as duplicate by don_crissti, Stephen Kitt, cuonglm, Raphael Ahrens, chaos Jan 26 '16 at 16:16

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

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

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

3

Use just sed instead:

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

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

Example:

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

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