sed command to replace text with blank spaces

sed command to replace text with blank spaces

Post by titsata » Fri, 12 Sep 2003 07:12:23



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

Thanks

Nick

 
 
 

sed command to replace text with blank spaces

Post by laura fairhe » Fri, 12 Sep 2003 09:07:40



Quote:>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

Quote:}

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

seeyafornow
l

Quote:>Thanks

>Nick

--


 
 
 

sed command to replace text with blank spaces

Post by Alan Conno » Fri, 12 Sep 2003 09:21:37



> 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) ;

<comments and quoting removed in following script>

 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 h

 /customerName.**Length:136/!b

 s/.*\(customerName.**Length:136\).*/\1/

 s/./ /g                                    

 x

 s/\(customerName.**Length:136\)/\
 /

 G

 s/\n\(.*\)\n\(.*\)/\2\1/

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

Quote:

> Well, that's probably a bit horrific looking if you're a newbie
> in fact it's horrific looking anyway!!

> seeyafornow
> l

"horrible" ? It's BEAUTIFUL and going right in my sed docs.

Does the OP know to put that script in a file (doesn't need to be executable)
and call it with:

sed -f filename  inputfile

Alan C

--

   take control of your mailbox ----- elrav1 ----- http://tinyurl.com/l55a

 
 
 

sed command to replace text with blank spaces

Post by rakesh shar » Fri, 12 Sep 2003 11:13:19



> 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

> Thanks

> Nick

sed -e '
   /customerName.**Length:136/!b
   s//\
&\
/
   :loop
   s/\n./ \
/
   /\n\n/!bloop
   s///
' file_name.txt >> file_name_new.txt
 
 
 

sed command to replace text with blank spaces

Post by titsata » Fri, 12 Sep 2003 13:57:25




> > 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

> > Thanks

> > Nick

> sed -e '
>    /customerName.**Length:136/!b
>    s//\
> &\
> /
>    :loop
>    s/\n./ \
> /
>    /\n\n/!bloop
>    s///
> ' file_name.txt >> file_name_new.txt

you guys rock.

Thanks you so much for your help.

I will practice all those first thing tomorrow

Cheers

Nick

 
 
 

1. replace gnu sed command ~ with sed command

Hi,

I've written a little sed script which is working in my environment. But
  I tried it in another environment and there it didn't work because the
~ command was unknown there.

Here the script:
# beginning at line 3, remove every 8th line
# beginning at line 4, remove every 8th line
# beginning at line 5, remove every 8th line
# beginning at line 6, remove every 8th line
sed -e '3~8d
         4~8d
         5~8d
         6~8d' file1.txt > file2.txt

How could I replace this gnu sed command with a sed command which is
valid in all environments?

I tried different things, but they didn't work.

For example I tried to replace this command as follows:

sed -e '3,${d;n;n;n;n;n;n;n}
         4,${d;n;n;n;n;n;n;n}
         5,${d;n;n;n;n;n;n;n}
         6,${d;n;n;n;n;n;n;n}' file1.txt > file2.txt

What am I doing wrong?

I'm new to sed, but I really like it and want to learn more about it and
I'm very interested in your answers.

Thanks for your help

Dirk

2. need HELP fast! enabling sound with kde2.2 on redhat 7.1

3. Sed command to replace text in several files

4. a question about pipe comp.unix.programmer

5. Sed: Replace text in one file with SPECIFIC text from another

6. Linux for VAXstation 3100 ??

7. Can sed replace one or more spaces with one space?

8. Net BSD and VPN

9. How to replace *soft returns* with a blank with sed

10. question on sed: how to replace multiple blank lines by single ones?

11. Using sed to delete blank spaces

12. Replace text block with sed?

13. How to replace string with some text (with new lines) sed or something else ?