Replacing multiple lines using Sed

wami

New Member
Joined
Feb 13, 2018
Messages
4
Reaction score
0
Credits
0
Hi,

I'm following this tutorial: https://likegeeks.com/sed-linux/ and there is a section about replacing lines.
Now, it works great, but my question is how to replace multiple lines like this:

if (strlen($llcc) == 5) {
$lang = substr(strtolower($llcc),0,2);
$country = substr(strtoupper($llcc),3,2);
}

To be replaced as whole lines. This because there are too many lines similar to them and I want to be selective
 


Hi,

I'm following this tutorial: https://likegeeks.com/sed-linux/ and there is a section about replacing lines.
Now, it works great, but my question is how to replace multiple lines like this:

if (strlen($llcc) == 5) {
$lang = substr(strtolower($llcc),0,2);
$country = substr(strtoupper($llcc),3,2);
}

To be replaced as whole lines. This because there are too many lines similar to them and I want to be selective

I don't understand exactly what you are trying to achieve.
Is your code example above from a sed script? or is this from a shell-script?

I understand what the code snippet is doing. If the length of variable $llcc is 5, you are splitting its content into two different variables. One of which is made upper-case and the other is lower-case.

That part is quite clear to me. But I don't understand what you mean WRT using sed to replace whole lines....

Sed can be used to match patterns in text and then perform actions on sections of text which match those patterns. You pass it an entire file and specify the patterns and actions and it will process all of the text for you. So sed almost certainly can do what you want. And if it can't, then awk definitely will be able to. But I would need a little more information from you in order to be able to help any further.

For example, there isn't any information about the format of the file you are hoping to modify. How is the original data-file laid out? Are the strings that are read into $llcc in a particular field position on each line? Are they always in the same place?

If you can post a few example lines from your data-file and then show what you want it to look like after it has been passed through sed, it would help a lot. As things currently stand, I don't fully understand what you are asking, so I can't really answer.
 
Last edited:
I have PHP files contains a lot of lines like the above.
What I need to achieve is to replace whole 4 PHP lines with another 4 lines using Sed.
 
You're still being a bit vague.
OK, so you have a bunch of PHP files with a lot of lines like the above and you want to replace those four lines of code with four other lines of code.

But how alike are the other blocks of code?
Are they all exactly the same as your example?
Or are some of them slightly different?
How alike are they?

If any are slightly different it's going to complicate things rather a lot because you'd need to set up separate regex patterns to correctly identify ALL of the blocks of code you're interested in and specify the lines of code that will replace each pattern.... Doing that would require manually going through the code and finding all sections and then writing a regex for each unique block, writing a replacement pattern. etc. etc.

If you have to go that far, it will probably be quicker to just edit the files directly.

Another option might be to write a script that could parse the PHP files and identify four line sections of code that meet your criteria and then replace them with whatever you deem is necessary, but again - that will probably take more time to set up than simply editing the files yourself.

Another thing to take into consideration is:
If all of the sections of code you are interested in replacing are exactly the same - wouldn't it be better to put the four replacement lines of code into a function and then call the function at each place where the code has been duplicated?
There's no point replacing duplicate code with even more duplicate code. ;)

Is there any particular reason you feel you HAVE to use sed? This task might be something more suited to macros in a text editor like vim or emacs, or some other tool.
 

Members online


Latest posts

Top