[ACCEPTED]-Delete whitespace in each begin of line of file, using bash-removing-whitespace

Accepted answer
Score: 30

sed -i 's/ //g' your_file will do it, modifying the file inplace.

To 5 delete only the whitespaces at the beginning 4 of one single line, use sed -i 's/^ *//' your_file

In the first expression, we 3 replace all spaces with nothing. In the 2 second one, we replace at the beginning 1 using the ^ keyword

Score: 16

tr(delete all whitespaces):

$ tr -d ' ' <input.txt >output.txt
$ mv output.txt input.txt

sed(delete leading whitespaces)

$ sed -i 's/^ *//' input.txt

0

Score: 4

use can use perl -i for in place replacement.

perl -p -e 's/^ *//' file 

0

Score: 1

To delete the white spaces before start 4 of the line if the pattern matches. Use 3 the following command. For example your 2 foo.in has pattern like this

      This is a test                                
                    Lolll
                       blaahhh
      This is a testtt

After issuing 1 following command

sed -e '/This/s/ *//' < foo.in > foo.out

The foo.out will be

This is a test
             Lolll
                blaahhh
This is a testtt
Score: 1

"Whitespace" can include both spaces AND tabs. The solutions 8 presented to date will only match and operate 7 successfully on spaces; they will fail if the 6 whitespace takes the form of a tab.

The below 5 has been tested on the OP's specimen data 4 set with both spaces AND tabs, matching 3 successfully & operating on both:

sed 's/^[[:blank:]]*//g' yourFile

After 2 testing, supply the -i switch to sed to make 1 the changes persistent-

More Related questions