Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multiple substitution 5

Status
Not open for further replies.

dickiebird

Programmer
Feb 14, 2002
758
GB
I've wrestled with this for long enough !
I have a text file with variable number of 0 (zero) on the beginning of most lines. I need to replace these leading zeros with same no. of spaces.
So far, I've got this :
sed 's/^0+/ /g' infile > outfile

which nearly does it ( It replaces 00000 or 000 etc with one space only.)
How would I replace all leading strings of zeros with a string of same number of spaces ?
TIA
;-)
Dickie Bird
db@dickiebird.freeserve.co.uk
 
Two ways:

sed -e :a -e 's/^\(0*\)0/\1 /;ta' infile > outfile

or

awk 'BEGIN{
ZEROS="^0+"
}
{
if( match( $0, ZEROS )){
sub( ZERO, "", $0 )
for( i = 0; i < RLENGTH; i++ )
printf( &quot; &quot; )
}
print
}' infile > outfile

The awk choice could be re-written many ways - maybe someone can find a good one-liner on it.
Cheers,
ND [smile]

bigoldbulldog@hotmail.com
 
Why not use awk?
awk ' {
if ($0 ~ /^0/) {
gsub(/0/,&quot; &quot;,$0)
}
print
} infile > outfile
 
OOps...sorry bout that,that kills all zeroes..
Big old bulldogs looks like it works..
 
Cheers BigOldBulldog - the sed version is just perfect !
I'll now spend an hour or two looking through sed&awk
to figure out why it works !
TAL ;-)
Dickie Bird
db@dickiebird.freeserve.co.uk
 
bigoldbulldog BRILLANT a star vox clamantis in deserto.
 
Or how about the perl one-liner:

perl -p -e &quot;s/^(0+)/' ' x length(\$1)/e&quot; input > output

Cheers, Neil :)
 
Perl would be nice - except this particular server is on AIX 3.2.5 - pre-perl !
;-) Dickie Bird
db@dickiebird.freeserve.co.uk
 

dickiebird:
sed -e :a -e 's/^\(0*\)0/\1-/;ta'
he recursively replace
(000)0 to 000-
(00)0- to 00--
... and so on

:a mark a label
^\(0*\) look for string of zeros at line begin
0 followed by a zero
\1 print the founded string
- followed by a - (in the example was a space)
; concatenate commands (no spaces!!)
ta if substitution was made restart at label, on the same input line

simply performant sed, once again: compliment bigoldbugdog !

toolkit:
i cannot get your version working,
my perl5 complains about \$1
if i un-mask it, he miscompute
the length($1) == length(whole-line)
what's wrong ?
PS: i hope you agree, bigoldbugdog's one-liner is more elegant. :)

marsd:
how can you propose awk, after reading bigoldbugdog's one-liner ?
vox clamantis in deserto.
 
F O R M I D A B L E bigoldbulldog !!
jamais je n'oublirai ce 'statement', merci, mon etoile.
 
Thanks for the explanation, jamisar.
Perhaps marsd was formulating his solution at the same time as BigOldBulldog, and hadn't seen the one-line sed solution ?
Anyway - I've been spoilt today by everyone - Thanks again all!
;-)
Dickie Bird
db@dickiebird.freeserve.co.uk
 
sure dickiebird you are rigth,
sorry marsd :) vox clamantis in deserto.
 
Didn't see it till too late...I've been hiding ever
since ;)
That is pretty to look at isn't it.
 
jamisar,
the \$1 escapes it for the shell. sed is for evil geniuses only ;-)
 
from the shell, not for the shell (-: And bigoldbulldog's solutions is by far the most elegant :)I
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top