Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer 1

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
add comment

Your Answer

 
discard

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.