4

I am interested in seeing the contents of a text file in hex mode.

I have tried :%!xxd and the problem is that I can not really understand which hex code represents each letter e.g. when you use an editor like Ultra Edit as you go over the hex code the corresponding letter is highlighted.

So how can I do something similar in my case?

To give you a context of my problem, I have a perl script that creates the file and I want to make sure that each line ends only with a 0x0A (LF).

Strange enough opening the file in vim and using :set list, I can see only $ even if I explicitly did on the script $line .= "\r\n"

2

You can try:

open my $fh, '>', 'test.txt'
    or die "$!";
binmode $fh;
print $fh "QWERTY\n";

You only see $ in vim because by default, listchars for end of line only contains $. From :help listchars:

'listchars' 'lcs'       string  (default "eol:$")
                        global
                        {not in Vi}
        Strings to use in 'list' mode and for the :list command.  It is a
        comma separated list of string settings.
                                                        lcs-eol
          eol:c         Character to show at the end of each line.  When
                        omitted, there is no extra character at the end of the
                        line.

To know actual line ending, you can try file or cat:

$ file test.txt 
test.txt: ASCII text, with CRLF line terminators
$ cat -e test.txt 
QWERTY^M$
  • +1.This works. BTW does adding just a \n translate to a LF in all systems? – Jim Jun 30 '14 at 7:50
  • @Jim:setting binmode to filehandle force \n to LF in all system. – cuonglm Jun 30 '14 at 7:51
  • I don't have an explicit file handle. I contruct the string by doing $data .= $line."\n" and I pass them to another library – Jim Jun 30 '14 at 8:01
  • How do you pass to other library? write to it or require? – cuonglm Jun 30 '14 at 8:02
  • Can you give me the portion of your script which attched data to mail? – cuonglm Jun 30 '14 at 8:11
0

A POSIX portable means of viewing non-printable characters in a text file might be:

sed -n l <file

Besides being portable, sed will also default to printing the line twice if you eschew the -n - once with nonprintable characters represented with C-style or octal escapes immediately followed by another printing of the line as it would normally display.

You might also do:

sed =\;l <file

To separate the pairs of output lines by line number.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.