How to find and replace where the find is on two lines and replace on first line
Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How to find and replace where the find is on two lines and replace on first line
Hi everyone,
I have thousands of html files that I need to modify and do not feel like doing it manually. So I want to use a linux script to make the change, but I can't figure out how to do it with sed.
I need to add a <br> into a line where the line to match spans over two lines.
I have thousands of html files that I need to modify and do not feel like doing it manually. So I want to use a linux script to make the change, but I can't figure out how to do it with sed.
I need to add a <br> into a line where the line to match spans over two lines.
How can I find a match on two lines and then insert the <br> at the beginning of the first line?
Any help would be greatly appreciated.
Thanks
Andy
Have a look at this:
Code:
sed '/<script type=/{N;/google_ad_client/s/^/<br>/}' infile
If that does what you want/need at sed's -i flag to make the canges in-place.
BTW: I've shortened the first search string to <script type=. If you do need the whole string, don'ty forget to escape the / . I.E.: /<script type="text\/javascript"><!--/
druuna, your command adds the <br> at the beginning of the line, but I need it to be in front of the search string, because the string sometimes appears within a line. How can I get it to be put right in front of the search string...
Find:
<script type="text/javascript"><!--
google_ad_client
in:
bla bla bla <script type="text/javascript"><!--
google_ad_client
You should have included this in your original post
Give this a try:
Code:
sed '/<script type=/{N;/google_ad_client/s/^\(.*\)<script/\1<br><script/}' infile
Test run:
Code:
$ cat infile
bla bla bla <script type="text/javascript"><!--
google_ad_client
bla bla bla <script type="text/javascript"><!--
xxxxx_ad_client
<script type="text/javascript"><!--
google_ad_client
<script type="text/javascript"><!--
vvvvve_ad_client
$ sed '/<script type=/{N;/google_ad_client/s/^\(.*\)<script/\1<br><script/}' infile
bla bla bla <br><script type="text/javascript"><!--
google_ad_client
bla bla bla <script type="text/javascript"><!--
xxxxx_ad_client
<br><script type="text/javascript"><!--
google_ad_client
<script type="text/javascript"><!--
vvvvve_ad_client
druuna, your command adds the <br> at the beginning of the line, but I need it to be in front of the search string, because the string sometimes appears within a line. How can I get it to be put right in front of the search string...
Find:
<script type="text/javascript"><!--
google_ad_client
in:
bla bla bla <script type="text/javascript"><!--
google_ad_client
Although this is my first attempt for answer in this forum but if I am catching your problem correct then file should be like this as below :
<script type="text/javascript"><!--
google_ad_client
bla bla bla <script type="text/javascript"><!--
google_ad_client
<script type="text/javascript"><!--
google_ad_client
bla bla bla <script type="text/javascript"><!--
So if you write below line I hope that can give you Desired pattern :
************************************
sed 's/<script/<br><script/g'
************************************
eg:
cat > p1.txt
<script type="text/javascript"><!--
google_ad_client
bla bla bla <script type="text/javascript"><!--
google_ad_client
<script type="text/javascript"><!--
google_ad_client
bla bla bla <script type="text/javascript"><!--
crtl+c
cat p1.txt | sed 's/<script/<br><script/g' -----> Command
Output :
<br><script type="text/javascript"><!--
google_ad_client
bla bla bla <br><script type="text/javascript"><!--
google_ad_client
<br><script type="text/javascript"><!--
google_ad_client
bla bla bla <br><script type="text/javascript"><!--
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.