sed to replace the matching pattern with equal number of spaces


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed to replace the matching pattern with equal number of spaces
# 1  
Old 05-16-2012
sed to replace the matching pattern with equal number of spaces

Hi

I have written a shell script which used sed code below

Code:
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
# 2  
Old 05-16-2012
Hi rakeshkumar,

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

# 3  
Old 05-16-2012
With awk:
Code:
awk -v p="$Pattern" 'BEGIN{l=length(p);s=sprintf("%"l"s","")} $0 ~ p{sub(p,s)}1' file > newfile

# 4  
Old 10-24-2012
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

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

input
Quote:
test_F1=N
test_F2=N
test1_F1=N
test2_F3=N
output
Quote:
test_F1=Y
test_F2=Y
test1_F1=N
test2_F3=N
Regards,
Haris
# 5  
Old 10-24-2012
I don't understand your question, not to mention how it might be related to this long-dormant thread.

Regards,
Alister
# 6  
Old 10-24-2012
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
# 7  
Old 10-24-2012
Code:
sed -n '/test_/ s/N$/Y/;p' input_file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace pattern matching

Can anyone help me with sed or awk to do a bulk replace of the below requirements. "REC_ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY ( START WITH +7486 INCREMENT BY +1 MINVALUE +7467 MAXVALUE... (6 Replies)
Discussion started by: ilugopal
6 Replies

2. Shell Programming and Scripting

sed - Exact pattern matching and replace

Hi Team, I am facing a problem as under, Suppose I have a file (test.txt) with the below content (all braces and slashes are included in the contents of the file) Now I want to append few words below matched line, I have written the below sed: sed '/option/a insert text here' test... (2 Replies)
Discussion started by: ankur328
2 Replies

3. Shell Programming and Scripting

Removing spaces from line matching a pattern

Hi, I want to remove the spaces from all the lines matching a particular pattern from my file. For instance in file abc.txt I have following data. Header,This is the header 111,this is 1st record 222, this is 2nd record 333, this is 3rd record Footer,3 records found Footer,111222333 ... (5 Replies)
Discussion started by: decci_7
5 Replies

4. Shell Programming and Scripting

Sed: printing lines AFTER pattern matching EXCLUDING the line containing the pattern

'Hi I'm using the following code to extract the lines(and redirect them to a txt file) after the pattern match. But the output is inclusive of the line with pattern match. Which option is to be used to exclude the line containing the pattern? sed -n '/Conn.*User/,$p' > consumers.txt (11 Replies)
Discussion started by: essem
11 Replies

5. Shell Programming and Scripting

sed - How to replace right part of equal sign (=) on a line

Hello. Using a bash script , I have a variable name for the file I want to modify FILE_TO_EDIT="/etc/my_config_file"And I have a variable name for the parameter to change PARAMETER="fallback_node" PARAMETER_NEW_VALUE="http://my_server_name.com/new_path" A config file may contain : 1°)... (2 Replies)
Discussion started by: jcdole
2 Replies

6. Shell Programming and Scripting

Replace a pattern in a file with a generated number using sed or awk

my file has thousands of line but let me show what i want to achieve... here is one line from that file cat fileName.txt (2,'','user3002,user3003','USER_DATA_SINGLE',1,0,0,'BACKUP',2,NULL,0,450,NULL,NULL,'','2011-05-10... (13 Replies)
Discussion started by: vivek d r
13 Replies

7. Shell Programming and Scripting

Creating number by pattern matching

I have a certain mnemonic string from which I want to calculate a number The pattern follows three letters s, v and d. If a letter is by its own, the number assigned to the letter is assumed to be one. Else it takes the value preceeding it. I then need to add the numbers together. Example ... (5 Replies)
Discussion started by: kristinu
5 Replies

8. Shell Programming and Scripting

Help with sed matching <tag1> newline spaces <tag2> and replace the value in the same string format

Hi, I'm very new to shell scripting and have searched google and this forum for quite some time now. I have the following in my xml file: <recipients> <member>value1</member> </recipients> I need to find a string <recipients> that follows with a new-line and bunch of spaces and... (5 Replies)
Discussion started by: mgharios
5 Replies

9. Shell Programming and Scripting

SED Question: Search and Replace start of line to matching pattern

Hi guys, got a problem here with sed on the command line. If i have a string as below: online xx:wer:xcv: sdf:/asdf/http:https-asdfd How can i match the pattern "http:" and replace the start of the string to the pattern with null? I tried the following but it doesn't work: ... (3 Replies)
Discussion started by: DrivesMeCrazy
3 Replies

10. Shell Programming and Scripting

Sed replace characters not equal to an expression

Hi all, Suppose I have a file with the contents below, and I only want to print words %S_ then | sort -u. ------------------------------ The %S_MSG that starts with '%.*s' is too long. Maximum length is %d. The %S_MSG name '%.*s' contains more than the maximum number of prefixes. The... (5 Replies)
Discussion started by: poldo
5 Replies
Login or Register to Ask a Question