how to display spaces and tabs using unix and the “cat” command

前端 未结 2 776
逝去的感伤
逝去的感伤 2021-02-20 16:14

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 c

相关标签:
2条回答
  • 2021-02-20 16:38

    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.

    0 讨论(0)
  • 2021-02-20 16:40

    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
    
    0 讨论(0)
提交回复
热议问题