I'm trying to replace every space in this text file with a backslash space.
cat prefixedfiles.txt | sed "s![[:space:]]! \!g"
A a a
A\ a\ a
sed 's! !\\ !g' prefixedfiles.txt
Few issues that are fixed:
!symbol and allows referring to backslash as
[[:space:]]matches more than just space; you may want it or not, I used the regular space;
catis superfluous here;
sedcan take file name as an argument (and even if it couldn't, the syntax
sed < file.txtis still better than
cat file.txt | sed).
Escape the backslash character.
cat prefixedfiles.txt | sed "s![[:space:]]! \\!g"
Matches char, where char is one of $, *, ., [, \, or ^. Note that the only C-like backslash sequences that you can portably assume to be interpreted are
\\; in particular
\tis not portable, and matches a ‘t’ under most implementations of sed, rather than a tab character.