7

While editing a unix file I'm getting data as below in vi editor.

MGW:^FVMG107
MGW:^FVMG113
MGW:^FVMG108
MGW:^FVMG103

where in above data ^F is not viewable in cat command. I have tried dos2unix & sed also, but it still exists. How can I remove ^F

4 Answers 4

15

^F is vim telling you there is a non-printable character 0x06 there (F is the sixth letter of the alphabet, they range: '^@', '^A', '^B'... '^Y', '^Z'. '^[', '^\', '^]', '^^', '^_')

I had no problem removing it graphically in vim, nano, joe… but if you prefer a command line approach, knowing that it's the character 0x06, you can use sed -i 's/\x06//g' filename to remove it.

PS: I'm afraid polym solution of removing ^F on cat -v will only work if your file doesn't have any other unprintable characters, which would get mangled.

0
2

As Ángel says, ^F in vi or the output of cat -v denotes an 06 character.  Another way of getting rid of these characters is

tr -d "\06" < oldfile > newfile
2

Edit: As Angel mentioned, you shouldn't use this solution, since it might produce undesirable changes.

His solution (hex(^F)==\x06):

sed -i 's/\x06//g' filename

My (inproper) solution:

cat -v oldfile | sed 's/\^F//g' > newfile

should do it.

3
  • 3
    As Ángel says below, this will potentially make other undesirable changes to the contents of the file - it's not a very general answer to the question.
    – godlygeek
    Jun 30, 2014 at 13:42
  • In addition to Ángel’s comment (referring to the functionality of cat -v), this will also cause corruption if the file happens to have any ^ characters that are immediately followed by F characters. Jun 30, 2014 at 15:57
  • @godlygeek and Scott thanks for the info :)!
    – polym
    Jun 30, 2014 at 16:52
0

Given that you're working with the file in vim, you can fix this within vi/vim too. You can type these character with ctrl-v, then ctrl-. For this, it's crtl-v, followed by ctrl-f.

So, you can use a simple find/replace:

:%s/^F//

That would work on all lines (the %), and replace (the s), the first instance of ^F, with nothing, effectively removing it.

If you want to see hidden characters in files, instead of cat, try using od -c:

od -c <file name>

eg, but putting a ^F and two line-endings in a file:

alex@Smiley:/tmp|⇒  cat test


alex@Smiley:/tmp|⇒  od -c test
0000000  006  \n  \n
0000003

This prints out all characters escaped, including tabs, line endings, etc.

If you want to use sed, you can use the same ctrl-V, crtl- trick right on the command-line too. That way you don't need to remember the translation.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .