Re: sed command to replace text with blank spaces

From: laura fairhead (run_signature_script_for_my_email_at_INVALID.com)
Date: 09/11/03


Date: Thu, 11 Sep 2003 00:07:40 GMT

On 10 Sep 2003 15:12:23 -0700, titsataki1@yahoo.com (titsataki) wrote:

>hi everybody,
>Newbie question here. I was trying to replace some text of the file
>with equal amount of blank spaces via sed. I have been looking to all
>the groups but I have not seen this one done.
>
>I was able to do it but I did a dumb substitution with 55 actual
>spaces. Is there an actual command that would insert 55 spaces (or
>whatever other number)?
>
>
>Here is what I used:
>
>sed 's/customerName.**Length:136/
> /' file_name.txt >> file_name_new.txt
>

Hello Nick,

Probably the neatest way to do something like this is
to use perl;

perl -pe 's/(customerName.*\*Length:136)/" " x length($1)/e'

Using 'perl' also gives you the option to make the '.*'
non-greedy -Usually * eats up as many characters as it
can and then the matching will work backwards so in this
case if there were 2 "*Length:136" strings on the line
the expression would macth the last one. You can make it
match the last by using the non-greedy '?' modifier;

perl -pe 's/(customerName.*?\*Length:136)/" " x length($1)/e'

To do it with 'sed' the only way I can see at the moment
is somewhat complicated and would involve isolating the
regular expression match, seperating it from the entire
string itself and using an s/./ /g to translate every
character to a SPACE, then inserting it back into the string.

This is an example way you could do that although it's
not exactly functionally equivalent to the code you
posted (because the initial .* means the pattern will
match the last customerName on the line if there are
more than one) ;

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# hold space = original string
h

# if there is no match exit so the line gets printed as normal
/customerName.**Length:136/!b

# pattern space = matched text only
s/.*\(customerName.**Length:136\).*/\1/

# translate text into SPACEs
s/./ /g

# hold space = SPACEs, pattern space = original string
x

# delete text from string leaving a NEWLINE to mark its position
s/\(customerName.**Length:136\)/\
/

# append the SPACEs to insert to the end of the main string,
# so now the pattern space is: LEFT SIDE TEXT \n RIGHT SIDE TEXT \n SPACEs
G

# move the SPACEs into the marker position
s/\n\(.*\)\n\(.*\)/\2\1/

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Well, that's probably a bit horrific looking if you're a newbie
in fact it's horrific looking anyway!! - so it's best to use 'perl'
who loves these sorts of problems, or even 'awk' is better.

Here's a way to do it in 'awk';

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
{
if(match($0,"customerName.*[*]Length:136"))
  {
  t=substr($0,RSTART,RLENGTH)
  gsub("."," ",t)
  $0=substr($0,1,RSTART-1)""t""substr($0,RSTART+RLENGTH)
  }
print
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

seeyafornow
l

>Thanks
>
>Nick

-- 
echo alru_aafriehdab@ittnreen.tocm |sed 's/\(.\)\(.\)/\2\1/g'


Relevant Pages

  • Cant Compiling perl5.8.8 on FreeBSD6.2
    ... WITH_DEBUGGING=yes Build perl with debugging support. ... First let's make sure your kit is complete. ... What is the file extension used for shared libraries? ... I'll use sprintf to convert floats into a string. ...
    (comp.unix.bsd.freebsd.misc)
  • Re: use of DBI; I am getting multiple error messages mixed in with ?the correct output.
    ... But I'm not talking about C++ or Java or SQL. ... This is a perl newsgroup. ... to means by a "defined null string", as opposed to an undefined value. ... And where exactly are you getting the idea that the empty string is a ...
    (comp.lang.perl.misc)
  • Re: Perl Strings vs FileHandle
    ... Just wanted to run this through Perl gurus to see if fit is ... testing it every time through the loop. ... The comparison test to the string '1' is superfluous. ... Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers ...
    (comp.lang.perl.misc)
  • Re: Working with Source Code to Insert Copyright Statements as a Header
    ... Paul Lalli wrote: ... Where could I go to find some samples of PERL code that do the ... ascii english text, etc etc etc) at the top of the file, from a string: ... `perldoc Tie::File` at the command line to read its documentation. ...
    (comp.lang.perl.misc)
  • Re: Perl ActiveX: A VBA string passed to my control is treated as a doubel-quoted string
    ... I have written a Perl ActiveX control that's suppose to do some regular ... When I use this string in VBA, I get erroneous resutls from my control. ... Perl regex has its own set of escape characters like any other parser. ...
    (comp.lang.perl.misc)