LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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


Reply
  Search this Thread
Old 08-05-2013, 08:59 AM   #1
andrew777
LQ Newbie
 
Registered: Feb 2009
Posts: 14

Rep: Reputation: 0
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.

Find:
<script type="text/javascript"><!--
google_ad_client

Replace with:
<br><script type="text/javascript"><!--
google_ad_client

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
 
Old 08-05-2013, 09:04 AM   #2
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
From my personal collection. Not sure what its going to do for you tho,....




nmap commands
nmap --reason 192.168.1.1
nmap --packet-trace 192.168.1.1
nmap --iflist

find name of glibc's dynamic linker
readelf -l /bin/bash | grep interpreter

get urls from website
lynx -dump -listonly http://www.linuxfromscratch.org/lfs/.../packages.html

download all urls from website
lynx -dump -listonly -nonumbers http://www.linuxfromscratch.org | xargs -n1 wget
or
lynx -dump -listonly -nonumbers http://www.linuxfromscratch.org | egrep '(\.xz|\.bz2|\.gz|\.tar)' | xargs -n1 wget

debug filesystem
debugfs

make multiple directories
mkdir somedirectory/{this that other}

dd with progress
dd if=/dev/zero | pv | dd of=/dev/somedrive

repeat last command but change word1 to word2
^word1^word2^

remove trailing date from sql files
for f in *.sql; do
mv -- "$f" "${f%%-*}.sql"
 
Old 08-05-2013, 09:13 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Quote:
Originally Posted by andrew777 View Post
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.

Find:
<script type="text/javascript"><!--
google_ad_client

Replace with:
<br><script type="text/javascript"><!--
google_ad_client

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"><!--/

@szboardstretcher: Wrong thread??
 
Old 08-05-2013, 01:28 PM   #4
andrew777
LQ Newbie
 
Registered: Feb 2009
Posts: 14

Original Poster
Rep: Reputation: 0
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

Replace with:
<br><script type="text/javascript"><!--
google_ad_client

Result:
bla bla bla <br><script type="text/javascript"><!--
google_ad_client
 
Old 08-05-2013, 01:40 PM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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
 
Old 08-05-2013, 01:41 PM   #6
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
Quote:
Originally Posted by andrew777 View Post
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

Replace with:
<br><script type="text/javascript"><!--
google_ad_client

Result:
bla bla bla <br><script type="text/javascript"><!--
google_ad_client
This is an adaptation of druuna's sed
Code:
sed '/<script type=/{N;/google_ad_client/s/<script type=/<br><script type=/}' infile
s/^/<br>/
substitute 'beginning of line' with <br>
changed to
s/<script/<br><script/

substitute <script with <br><script

as long as your bah bla bla does not contain <script it should do what you need

edit:
druuna's revised sed is better than mine, as it does not suffer the above 'restriction'

edit2:
as pointed out by druuna, both have same restriction ( both match first occurrence of <script )

Last edited by Firerat; 08-05-2013 at 01:57 PM.
 
1 members found this post helpful.
Old 08-05-2013, 01:43 PM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
@Firerat: Simpler and thus better then mine!
 
Old 08-05-2013, 01:45 PM   #8
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
Quote:
Originally Posted by druuna View Post
@Firerat: Simpler and thus better then mine!
doesn't yours handle the 'random' string before <script better than mine?
 
Old 08-05-2013, 01:47 PM   #9
sumit.inform
LQ Newbie
 
Registered: Mar 2013
Posts: 19
Blog Entries: 2

Rep: Reputation: Disabled
Hi andrew777,

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"><!--
 
Old 08-05-2013, 01:50 PM   #10
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Quote:
Originally Posted by Firerat View Post
doesn't yours handle the 'random' string before <script better than mine?
I don't think so. Even if <script type= appears multiple times on one line, you did not use the g flag, thus only the first one would be changed.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
find/replace by line nr or by pattern digerydoo Programming 7 10-08-2012 09:35 AM
search folders, find a line in a file, and replace it with a new line ithirdeye Programming 2 04-07-2012 04:27 PM
[SOLVED] How to find and replace a text spanning multiple lines with sed haveanother Linux - Newbie 7 02-26-2012 06:29 AM
[SOLVED] Perl Find and replace line in crontab Sir Todd Programming 2 02-14-2012 02:55 PM
find and replace in files from command line dexter_modem Linux - General 4 06-10-2003 11:27 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 03:15 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration