Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

Does such a command exist already?

share|improve this question
up vote 5 down vote accepted

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.)

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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