I have a text file that contains a lot of whitespace and some other ASCII characters. I would like to use Bash (preferably via sed) to replace any non-whitespace characters in the file with the letter M. How would I go about doing this?
Thanks for helping.
edit: For the downvoters, I tried running this:
sed -i 's/[^\s]/M/g' file
and this:
sed -i 's/[^\s]/M/' file
but neither of them worked out too well. I'm a little unfamiliar with regex, so apologies if I'm doing something obviously wrong.
You could use POSIX character classes:
sed -i 's/[^[:space:]]/M/g' file
For example:
$ echo 'a b c' | od -a
0000000 a sp b sp ht c nl
0000007
$ echo 'a b c' | sed 's/[^[:space:]]/M/g' | od -a
0000000 M sp M sp ht M nl
0000007
Note:
g flag to the s command, since you want to replace all matches in a lineUse tr
tr -c '[:space:]' M < file