Agnus Dei (jackal) wrote,
Agnus Dei
jackal

How to replace a character in a file with a solid black block (extended ascii character)

$ cat /tmp/foo
apple
bear
cat
dog

$ gsed -i "s/e/\xE2\x96\x88/" /tmp/foo


$ cat /tmp/foo
appl█
b█ar
cat
dog


-----------------------
In the following example I want to match com.f2 in the line and then replace the characters "f2" with two block characters "██".

Here are three different ways to do the same thing:

# Using GUI sed (or default sed on Linux)
gsed -i "/com.f2/ s/f2/\xE2\x96\x88\xE2\x96\x88/" /tmp/foo
gsed -i "/com.f2/ s/com\.f2/com\.\xE2\x96\x88\xE2\x96\x88/" /tmp/foo


# Using BSD sed (or default sed on Mac)
sed -i.bak "/com.f2/ s/f2/$(printf "\xE2\x96\x88\xE2\x96\x88")/g" /tmp/foo
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

  • 0 comments