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
- Next message: Alan Connor: "Re: sed command to replace text with blank spaces"
- Previous message: Chris F.A. Johnson: "Re: Passing command-line arguments"
- In reply to: titsataki: "sed command to replace text with blank spaces"
- Next in thread: Alan Connor: "Re: sed command to replace text with blank spaces"
- Reply: Alan Connor: "Re: sed command to replace text with blank spaces"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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'
- Next message: Alan Connor: "Re: sed command to replace text with blank spaces"
- Previous message: Chris F.A. Johnson: "Re: Passing command-line arguments"
- In reply to: titsataki: "sed command to replace text with blank spaces"
- Next in thread: Alan Connor: "Re: sed command to replace text with blank spaces"
- Reply: Alan Connor: "Re: sed command to replace text with blank spaces"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|