Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to display spaces and tabs using unix and the "cat" command

Tags:

unix

I'm trying to figure out how to display the contents of a file through unix where the spaces and tabs are marked somehow. I know how to display the files with tabs (aka cat -T filename) but I've been trying to figure out how to show the spaces as well. cat -A filename doesn't work for me, and only replaces tabs with ^I and places $ at the end of the line. How can I utilize cat to print out a file with all tabs and spaces clearly marked?

like image 219
Alex Zhu Avatar asked Nov 19 '16 02:11

Alex Zhu


People also ask

How do I see tabs and spaces in Linux?

Cat only provides -T (show tabs) or -E (show newlines) or -A (show both types).

What is the use of cat command in Unix?

cat is a standard Unix utility that reads files sequentially, writing them to standard output. The name is derived from its function to (con)catenate files (from Latin catenare, "to chain").

How do you cat file in Unix?

Cat(concatenate) command is very frequently used in Linux. It reads data from the file and gives their content as output. It helps us to create, view, concatenate files.

How can I see the tabs present inside a file in Linux?

just use grep "<Ctrl+V><TAB>" , it works (if first time: type grep " then press Ctrl+V key combo, then press TAB key, then type " and hit enter, voilà!)


2 Answers

There's a standard unix tool for character substitution. In this example, I'm replacing spaces for * and tabs for &:

$ cat tmp
space tab   space   tab end
tab space   tab space end
$ cat tmp | tr " " "*" | tr "\t" "&" 
space*tab&space&tab&end
tab&space&tab*space*end
like image 122
palako Avatar answered Sep 30 '22 15:09

palako


The answer to your question: No, cat command can not "show" spaces as a visible characters. It just does not contain such a feature. Cat only provides -T (show tabs) or -E (show newlines) or -A (show both types).

I assume palako was meaning to say this, but instead he he jumped straight to providing you a workaround, which is valid. If you are looking to visually count spaces, that's fine. There are many workarounds, and not all will be appropriate for all users.

If you need something different, I suggest looking into Perl one-liners (because they could adapt easily into existing UNIX scripts), or write something in Python which mimics 'cat' but replaces spaces with another character, much like palako's "tr" example.

like image 37
Scott Prive Avatar answered Sep 30 '22 16:09

Scott Prive