Reputation: 41
I have a simple question (I think), I need to replace a specific string which occurs in multiple lines in a file (verilog). These lines are themselves between specific patterns
ex:
lk
lk
lk
lk
lk
//comment1
input [5:0]a,
input [3:0]b,
input c,
input [4:0]d,
input f,
//comment2
lm
lm
lm
lm
I need to replace "input" with "logic" between "comment1" and "comment2" keeping them as it is in the final result (quotes not included, only for understanding)
Right now what I have is
sed '/\/comment1/,/\/comment2/s/input/logic/g' file1.sv > file2.sv
the result I get is
lk
lk
lk
lk
lk
//comment1
logic
logic
logic
logic
logic
//comment2
lm
lm
lm
lm
The rest of the text after logic is gone, I need to preserve that
Can someone please help, I will be grateful...
Upvotes: 3
Views: 1844
Reputation: 13
I have just tested your sed command, and to me it worked out just perfect. The only thing I can see that is wrong is that you have forgotten the last slash in front of the comment-lines. It worked for me either way, but the correct line would be
sed '/\/\/comment1/,/\/\/comment2/s/input/logic/g' file1.sv > file2.sv
As you can see I have one more "/" in each of the comment-lines. Try it out and see if it helps. I am running GNU sed version 4.2.1 here.
To answer your question in the comment about reading the filename from the command line inside a script this would be the most simple way
#!/bin/bash
sed '/\/\/comment1/,/\/\/comment2/s/input/logic/g' "$1" > file2.sv
exit 0
$1 means the first argument. If would like to be able to specifiy the output file as well you do as this
#!/bin/bash
sed '/\/\/comment1/,/\/\/comment2/s/input/logic/g' "$1" > "$2"
exit 0
Whereas this means the first argument is the input file, the second argument is the output file. The quotation might not be necessary, but it's a good idea to quote strings (and the filenames are strings basically).
Upvotes: 0
Reputation: 784898
You can use this sed command:
sed -i.bak '/\/\/comment1/,/\/\/comment2/s/\<input\>/logic/g' file
/\/\/comment1/,/\/\/comment2/
is to search text between 2 comment blocks\<input\>
is for matching input with word boundaries s/\<input\>/logic/g
is for replacing "input" with "logic"Upvotes: 1