Terminal command that prints out tabs, spaces, and newlines as \t \s and \n

text processingwhitespace

I wrote a terminal program in c that converts tabs to spaces. And I would like to see that it works and how many spaces were converted from tabs. The problem is when I create a file called input.txt and pipe it to the program, it just shows blanks for both tabs and spaces rather than the actual characters \t and \s. So I am wondering if there is an existing terminal command that could take the output of my program and replace tabs and spaces with \t and \s respectively:

cat input7.txt | ./detab
abc  def  

Perhaps something like:

cat input7.txt | ./detab | command
abc\s\s\sdef\s\s\s  

Does such a command exist already?

Best Answer

Under Linux, cat -T shows tabs as ^I. There are other options to make trailing whitespace apparent, to display control characters in a printable form, etc.

If you want to compare the result of your program with the original, you can use diff:

./detab input7.txt | diff input7.txt - | cat -T

You may also want to compare the input of your program with the standard utility expand.

If you want exactly the transformation of spaces into \s and tabs into \t, you can use sed:

sed -e 's/\\/\\\\/g' -e 's/\t/\\t/g' -e 's/ /\\s/g'

(The first expression doubles backslashes, which makes the transformation unambiguous.)

Related Question