Operating System - HP-UX
1833866 Members
2445 Online
110063 Solutions
New Discussion

Re: delete leading blanks in a file

 
SOLVED
Go to solution

delete leading blanks in a file

How can I delete multiple leading blanks in a file?
This site ignores leading blanks so I substitute all blanks with "B" in my example:
#cat file
BBline1
line2
Bline3
BBBBBline4BwithBblank
#
should lead to
#cat file2
line1
line2
line3
line4BwithBblank
#
I found a solution with sed that deletes me only one leading blank :
sed -e 's/^ //g'
but there may be 20 leading blanks in a line...

TIA Clemens
always look on the bright side of life
14 REPLIES 14
James R. Ferguson
Acclaimed Contributor

Re: delete leading blanks in a file

Hi Clemens:

# sed -e 's/^[ \t]*//' filename

Note: Type a TAB character in lieu of \t. Some versions of 'sed' simply don't recognize the '\t'.

Regards!

...JRF...
Jochen Heuer
Respected Contributor

Re: delete leading blanks in a file

Hi Clemens,

you can also try this:

sed -e 's/^[: :]*//'

This should remove all white space (tab or spaces) from the beginning of the line.

Regards,

Jochen
Well, yeah ... I suppose there's no point in getting greedy, is there?
Jochen Heuer
Respected Contributor

Re: delete leading blanks in a file

Note: there is a blank between
[: and :] ...
Well, yeah ... I suppose there's no point in getting greedy, is there?

Re: delete leading blanks in a file

Hi James,
it's great! It works.
But can you explain why?

Clemens
always look on the bright side of life
Jochen Heuer
Respected Contributor

Re: delete leading blanks in a file

Hi Clemens,

it goes somehow like this:

^ --> beginning of line (as you probably already knew)

[\t]* --> followed by zero or more characters like \t (although I could not find an explanation for \t or tab in 'man 5 regexp' it probably means the same as [:SPACE:] whitespace -- space or tab)

Regards,

Jochen

Well, yeah ... I suppose there's no point in getting greedy, is there?
H.Merijn Brand (procura
Honored Contributor

Re: delete leading blanks in a file

Finally got through.

The perl version might be easier to read:

# perl -pe 's/^\s+//' infile >outfile

-p = print every line
-e = using the following expression

s/// = substitute in the current line
^ = start of line
\s = white space (blanks, tabs, spaces)
+ = one or more

Enjoy, have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Jose Mosquera
Honored Contributor

Re: delete leading blanks in a file

Hi,

To remove any space:
#cat |tr -d " " >

To remove tabs:
#cat |tr -d "\t" >

To remove both of them once:
#cat |tr -d " "|tr -d "\t" >

Rgds.
Justo Exposito
Esteemed Contributor

Re: delete leading blanks in a file

Hi,

You can do:

sed -e 's/\ \{1,\}/ /g' file

This change one or more blank by one.

Regards,

Justo.
Help is a Beatiful word

Re: delete leading blanks in a file

Thanks to all!
I'll try to resume:
Jochen, your first answer
sed -e 's/^[: :]*//' doesn't
work although I recognized the blank.
The explanation of \t is what I don't understand. I knew \t only for a tab. But here I works only for a blank -- that's what I want!
[:SPACE:] or [:BLANK:] doesn't work.
Procura, your perl is easy to understand but I never used perl before... Now I think about it!
Thanks to Jose Maria and Justo. You didn't recognize that I only want to process leading blanks.

always look on the bright side of life
Laurie Gellatly
Honored Contributor
Solution

Re: delete leading blanks in a file

True blanks?
how about sed -e 's/^ *//'

Cheers ...Laurie :{)
If you're not using OverTime, you're doing overtime!

Re: delete leading blanks in a file

Hi Laurie,
that's it! It works just like James' 's/^[ \t]*//' but it seems to be logical.
Thanks
Clemens
always look on the bright side of life
Jannik
Honored Contributor

Re: delete leading blanks in a file

sed - stream text editor
-e - script
s/^ *// - s/regular expression/replacement/flags

BR,
Jannik
jaton
Laurie Gellatly
Honored Contributor

Re: delete leading blanks in a file

Your welcome.

...Laurie :{)
If you're not using OverTime, you're doing overtime!
James R. Ferguson
Acclaimed Contributor

Re: delete leading blanks in a file

Hi (again) Clemens:

An easier read and understood variation of my original post is this:

# sed -e 's/^[[:space:]]*//' filename

The [:space:] character class or characters which produce white space (blanks, tabs) in displayed text.

I choose to match *either* spaces or tabs, or a mixture in any combination, in order to provide the most general match. Had you originally used only space characters between the fields of your file, but then used 'unexpand' to change spaces to tabs, a regular expression consisting of only the space character would not have achieved your goal.

Your original 'sed' substitution failed to consider anything other than blank (space) characters (i.e. not TABS), More importantly, your regular expression (RE) consisted only of *one* character. By adding the asterisk following the character class we constructed a RE that matched zero or more occurrences of the characters found in the character class that preceded the asterisk. Thus, the matching string was represented by the maximum number of characters which defined the RE.

Regards!

...JRF...