Raja G
Raja G

Reputation: 6643

sed not removing all whitespaces from the beginning of each line in the file

I have few lines code as below:

bash-3.2$ cat remove_space.txt
    this is firs line with 2-3 spaces
                    2nd line with more space, this is where the issue is.
          3rd line

I am able to suppress the leading whitespaces from the beginning of each line but not from 2nd line. I am not getting why sed is failing there.

bash-3.2$ sed 's/^ *//g' remove_space.txt
this is firs line with 2-3 spaces
                2nd line with more space, this is where the issue is.
3rd line


bash-3.2$

Update

with `-vte` 

bash-3.2$ cat -vte remove_space.txt
    this is firs line with 2-3 spaces$
    ^I^I^I^I2nd line with more space, this is where the issue is.$
          3rd line $
$
$
bash-3.2$

Any help greatly appreciated.

Upvotes: 0

Views: 400

Answers (3)

Allan
Allan

Reputation: 12438

The problem here is because your file contains some \t in the beginning of the lines as shown by cat -vTE (as requested in my comment)

bash-3.2$ cat -vte remove_space.txt
    this is firs line with 2-3 spaces$
    ^I^I^I^I2nd line with more space, this is where the issue is.$
          3rd line $

You can change your command into:

sed -E 's/^[[:space:]]+//' remove_space.txt 

to take care of the spaces and the tabs. Also for portability reasons use POSIX regex as defined in the help https://www.freebsd.org/cgi/man.cgi?query=sed&sektion=&n=1

 -E        Interpret regular expressions as extended (modern) regular
   expressions rather than basic regular expressions (BRE's).  The
   re_format(7) manual page fully describes both formats.

Upvotes: 2

Aaron
Aaron

Reputation: 24812

The four ^I in the second line are tabulations, those are the whitespace characters that still appear in your output.

I suggest you use the following command to remove any kind of space from the beginning of the lines :

sed 's/^[[:space:]]*//' remove_space.txt

Upvotes: 2

Barmar
Barmar

Reputation: 781310

The second line has TAB characters after the first 4 spaces -- that's what ^I signifies. You're only removing spaces, not TAB.

sed $'s/^[ \t]*//' remove_space.txt

BTW, there's no need to use the g modifiers when a pattern is anchored with ^ or $. These patterns can only match once on a line.

Upvotes: 2

Related Questions