Escape backslash with sed command when the next character is "t"

788 views Asked by At

I'm using this sed command to replace the string "##INFO=<ID=" with "\t%" :

bcftools view -h /data/ExAC.r1.sites.vep.vcf | grep "^##INFO=<ID=" | sed $'/^##INFO=<ID=/{ s//'\t%INFO' /; s/,.*//; p; }''

but what I get is exactly my desired output, but instead of a backslash, it doesn't print anything. If I remove the single quotes around \t%INFO in the sed command, it automatically tabs the output, so I don't want that either.

How can I escape the backslash so that it just prints a backslash?

2

There are 2 answers

1
gbtimmon On BEST ANSWER
sed $'/^##INFO=<ID=/{ s//'\t%INFO' /; s/,.*//; p; }''

1.)The dollar sign in front doesn't seem to have a point

sed '/^##INFO=<ID=/{ s//'\t%INFO' /; s/,.*//; p; }''

2.)You cant just nest single quotes. I dont know how this "works" I wouldnt expect it to.

sed '/^##INFO=<ID=/{ s//\t%INFO /; s/,.*//; p; }'

3.) This replaces the string with a tab then a %INFO. Then prints it. escape it once.

sed '/^##INFO=<ID=/{ s//\\t%INFO /; s/,.*//; p; }'

4.) This replaces the stinge with a \t%INFO then prints it, resulting in tab %INFO. Escape it again.

sed '/^##INFO=<ID=/{ s//\\\\t%INFO /; s/,.*//; p; }'

5.)This should work.

But there is an easier answer using a capture group. It looks like you are looking for this?

bcftools view -h /data/ExAC.r1.sites.vep.vcf \
| grep "^##INFO=<ID=" \
| sed -E 's/^##INFO=<ID=([^,]*),.*/\\t%INFO \1/' 

To get all on on line:

bcftools view -h /data/ExAC.r1.sites.vep.vcf \
| grep "^##INFO=<ID=" \
| sed -E 's/^##INFO=<ID=([^,]*),.*/\\t%INFO \1/' \
| awk '{printf "%s ", $0}'
0
Nahuel Fouilleul On

Reference Bash Quoting

$'string' is called ANSI C quoting interprets backslash sequence, just use single quote quoting to avoid interpret characters the drawback is that it can't contain single quote, but different string qutoed can be concatenated.