Delete whitespace in each begin of line of file, using bash
Asked Answered
D

5

16

How i can delete whitespace in each line of file, using bash For instance, file1.txt. Before:

  gg g
 gg g
t  ttt

after:

gg g
gg g
t  ttt
Demasculinize answered 16/2, 2012 at 10:45 Comment(2)
All whitespace or just leading whitespace characters on a line. For instance, should " ggg gg" become "ggg gg" or "ggggg"Aluminium
2 khachik Yes, whitespace is deleted, but how i can change my file?Demasculinize
U
30

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

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

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

Unisexual answered 16/2, 2012 at 10:55 Comment(6)
this script delete all whitespacesDemasculinize
Yes, did your question somehow mean something else?? s/^ *// deletes leading whitespace if that's what you want ... but perhaps you should also edit your question then.Lillalillard
Edited to add @Lillalillard comment in the answer.Unisexual
The /g is redundant but harmless in that case, the regex already takes care of substituting all leading whitespace in one go.Lillalillard
Sure :-) Edited to have the simplest regexp.Unisexual
Shouldn't it be 's/^\s*//' so it removes tabs, too?Trucking
D
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
Disparate answered 16/2, 2012 at 10:49 Comment(4)
and how i can change my file? The resut is wrote in consoleDemasculinize
Thank you veru much and if i want to delete only whitespace in the begin of each line of file (excuse me i edit question :( )Demasculinize
What about trailing whitespace?Barmaid
How to keep only the leading white spaces?Discontinuance
B
4

use can use perl -i for in place replacement.

perl -p -e 's/^ *//' file 
Bunns answered 16/2, 2012 at 11:26 Comment(1)
Yes, yes! I was looking for a clean, quick way of stripping all beginning and trailing whitespace from lines in source code files. The above code strips the newline characters, not a desirable trait for my purposes, so perl -pi -e 's/^[\ \t]+|[\ \t]+$//g' [file] does exactly what I needed.Absorbefacient
P
1

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

      This is a test                                
                    Lolll
                       blaahhh
      This is a testtt

After issuing 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
Pines answered 14/8, 2015 at 18:34 Comment(0)
S
1

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

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

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

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

Sanitarian answered 7/6, 2020 at 9:36 Comment(1)
i was parsing XML file and only this helped.Defend

© 2022 - 2024 — McMap. All rights reserved.