How to remove all white spaces from a given text file

前端 未结 11 1837
无人共我
无人共我 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:00

    Dude, Just python test.py in your terminal.

    f = open('/home/hduser/Desktop/data.csv' , 'r')
    
    x = f.read().split()
    f.close()
    
    y = ' '.join(x)
    f = open('/home/hduser/Desktop/data.csv','w')
    f.write(y)
    f.close()
    
    0 讨论(0)
  • 2020-12-13 02:01

    This answer is similar to other however as some people have been complaining that the output goes to STDOUT i am just going to suggest redirecting it to the original file and overwriting it. I would never normally suggest this but sometimes quick and dirty works.

    cat file.txt | tr -d " \t\n\r" > file.txt
    
    0 讨论(0)
  • 2020-12-13 02:03

    Try this:

    sed -e 's/[\t ]//g;/^$/d' 
    

    (found here)

    The first part removes all tabs (\t) and spaces, and the second part removes all empty lines

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

    Easiest way for me ->

            echo "Hello my name is Donald" | sed  s/\ //g
    
    0 讨论(0)
  • 2020-12-13 02:05

    If you want to remove ALL whitespace, even newlines:

    perl -pe 's/\s+//g' file
    
    0 讨论(0)
  • 2020-12-13 02:10

    I think you may use sed to wipe out the space while not losing some infomation like changing to another line.

    cat hello.txt | sed '/^$/d;s/[[:blank:]]//g'
    
    0 讨论(0)
提交回复
热议问题