I have a bash script which has a variable being set from the output of cat and grep:
result=`cat $file | grep -A2 "$search"`
result contains 3 lines E.G.:
This is Line 1
This is line 2
This is line 3
I need to prefix each line with a space:
This is Line 1
This is line 2
This is line 3
I have tried the following:
result=`echo $result | awk '{print " "$0}'`
and a few different sed commands, all of which result in this:
This is Line 1 This is line 2 This is line 3
they add the spaces, but delete the new lines
Note: this will be saved into a file which needs the line breaks
< $file grep -A2 "$search" | sed -e 's/^/ /'
. If you want to use the text captured in$result
then<<< "$result" sed -e 's/^/ /'
.echo
converts newlines to spaces. – AlexP Nov 17 '17 at 10:54sed
rather thanawk
); their code should work fine if they simply quote$result
to prevent word splitting by the shell i.e.echo "$result" | awk '{print " "$0}'
– steeldriver Nov 17 '17 at 14:31