How to remove all white spaces from a given text file

前端 未结 11 1838
无人共我
无人共我 2020-12-13 01:34

I want to remove all the white spaces from a given text file. Is there any shell command available for this ? Or, how to use sed for this purpose.

I wa

相关标签:
11条回答
  • 2020-12-13 02:11

    Try this:

    tr -d " \t" <filename
    

    See the manpage for tr(1) for more details.

    0 讨论(0)
  • 2020-12-13 02:12

    This is probably the simplest way of doing it:

    sed -r 's/\s+//g' filename > output
    mv ouput filename
    
    0 讨论(0)
  • 2020-12-13 02:12

    hmm...seems like something on the order of sed -e "s/[ \t\n\r\v]//g" < hello.txt should be in the right ballpark (seems to work under cygwin in any case).

    0 讨论(0)
  • 2020-12-13 02:17
    $ man tr
    NAME
        tr - translate or delete characters
    
    SYNOPSIS
        tr [OPTION]... SET1 [SET2]
    
    DESCRIPTION
       Translate, squeeze, and/or delete characters from standard 
       input, writing to standard output.
    

    In order to wipe all whitespace including newlines you can try:

    cat file.txt | tr -d " \t\n\r" 
    

    You can also use the character classes defined by tr (credits to htompkins comment):

    cat file.txt | tr -d "[:space:]"
    

    For example, in order to wipe just horizontal white space:

    cat file.txt | tr -d "[:blank:]"
    
    0 讨论(0)
  • 2020-12-13 02:17

    Much simpler to my opinion:

    sed -r 's/\s+//g' filename
    
    0 讨论(0)
提交回复
热议问题