In grep
you can use --group-separator
to write something in between group matches.
This comes handy to make it clear what blocks do we have, especially when using -C X
option to get context lines.
$ cat a
hello
this is me
and this is
something else
hello hello
bye
i am done
$ grep -C1 --group-separator="+++++++++" 'hello' a
hello
this is me
+++++++++
something else
hello hello
bye
I learnt in Using empty line as context "group-separator" for grep how to just have an empty line, by saying --group-separator=""
.
However, what if I want to have two empty lines? I tried saying --group-separator="\n\n"
but I get literal \n
s:
$ grep -C1 --group-separator="\n\n" 'hello' a
hello
this is me
\n\n
something else
hello hello
bye
Other things like --group-separator="\nhello\n"
did not work either.
printf
orecho
. In your case,grep -C1 --group-separator=$'hello\nfedorqui' 'hello' a
is equivalent. – fedorqui Jun 16 '15 at 09:38$''
yours would be the good way to go! – fedorqui Jun 16 '15 at 09:46$'$var'
(unable to expand/print value of variable if set by single quote!) right? whereas"$(echo $var)"
can work. – Pandya Jun 16 '15 at 09:50$'"$var"'
. That is$'
+"$var"
+'
. – fedorqui Jun 16 '15 at 09:54$'sting1'"$var"'$string'
– Pandya Jun 16 '15 at 11:16--group-separator=$''"$var"''
works fine and so--group-separator=$"$var"
does. – fedorqui Jun 16 '15 at 11:21