-4

we have file with many "%" characters in the file

we want to add before every "%" the backslash

as

\%

example

before

%TY %Tb %Td %TH:%TM %P

after

\%TY \%Tb \%Td \%TH:\%TM \%P

how to do it with sed ?

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
yael
  • 13,106

1 Answers1

6

Pretty straightforward

$ echo '%TY %Tb %Td %TH:%TM %P' | sed 's/%/\\%/g'
\%TY \%Tb \%Td \%TH:\%TM \%P

but you can accomplish the same with bash parameter substitution

$ str='%TY %Tb %Td %TH:%TM %P'; backslashed=${str//%/\\%}; echo "$backslashed"
\%TY \%Tb \%Td \%TH:\%TM \%P
glenn jackman
  • 85,964