sed to replace the matching pattern with equal number of spaces

Hi

I have written a shell script which used sed code below

sed -i 's/'"$Pattern"'/  /g' $FileName

I want to count the length of Pattern and replace it with equal number of spaces in the FileName.

I have used $(#pattern) to get the length but could not understand how to replace with equal spaces.

Any help is much appreciated

Hi rakeshkumar,

Using perl:

$ perl -i -pe 's/(pattern)/q[ ] x length $1/ge' infile

With awk:

awk -v p="$Pattern" 'BEGIN{l=length(p);s=sprintf("%"l"s","")} $0 ~ p{sub(p,s)}1' file > newfile

Hi ,
I have a doubt here
If my pattern is not fixed , like in below case then how can I do that using sed command .
I tried using awk command but it replaced N with Y in entire file.txt

 
awk -F '=' '{if ($1 == "test") {print $1"=Y"} else {print $1"=N"}}'  file.txt

input

output

Regards,
Haris

I don't understand your question, not to mention how it might be related to this long-dormant thread.

Regards,
Alister

How do i Replace if the pattern what I am trying to search is not fixed , like in the previous post as i have mentioned

I have to replace all flag values "N" to "Y" only for string starting with test and not for test1

Regards,
Harish

sed -n '/test_/ s/N$/Y/;p' input_file

Now that you have clarified your help request, it is clear that it has nothing whatsoever to do with this thread.

Resurrecting a dormant thread with an unrelated question is poor etiquette, a distraction, and a waste of time.

Most people will assume that a thread with a new post is an active conversation. They will read it top-down, most likely not immediately noticing the ages of the posts. And then, when they finally arrive at your new post, it has nothing to do with everything they've read up to that point.

Next time, please start your own thread.

Regards,
Alister

Small correction to your code..:slight_smile: