LinuxQuestions.org
Support LQ: Use code LQCO20 and save 20% on CrossOver Office
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
 
LinkBack Search this Thread
Old 08-02-2006, 04:01 AM   #1
graziano1968
Member
 
Registered: Sep 2004
Posts: 212

Rep: Reputation: 30
another question about search and replace text


Hello

when I need to search and replace some code in several files I use this


Code:
find /your/home/dir -name "*.txt" | xargs perl -pi -e 's/stringtoreplace/replacementstring/g
Well,

when I have to search and replace something like this
Code:
<br>test<br>
on the same line I have no problem


when I have to search and replace something like this
Code:
<br>
test
<br>
with the code on separate lines I Can't . What to do to search and replace

Code:
<br>
test
<br>
with
Code:
OK
?




Thank you

Last edited by graziano1968; 08-02-2006 at 04:03 AM.
 
Old 08-02-2006, 09:08 AM   #2
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 31
This won't work with Perl's -p switch as it reads each line separately. If you want to use Perl, an alternative would be to write a script that opens each file and reads its content into a single variable.
But I think that using sed will be a little less cumbersome here:
Code:
sed -n -i.bak -e '1{$!N;$!N;};/^<br>\ntest\n<br>/{s/^<br>\ntest\n<br>/OK/;$!N;$!N};P;$!N;D' file1 file2
This will make a backup of each file first.
 
Old 08-02-2006, 09:14 AM   #3
berbae
Member
 
Registered: Jul 2005
Location: France
Distribution: Arch Linux
Posts: 530

Rep: Reputation: 50
This seems to do what you want:
Quote:
find /your/home/dir -name "*.txt" -exec sed -i '/<br>/,/<br>/ c OK' {} \;
I presumed that <br> is a sort of delimiter which goes always in pairs.
Can you confirm that <br> is a delimiter?

Last edited by berbae; 08-02-2006 at 02:30 PM.
 
Old 08-02-2006, 09:35 AM   #4
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 31
Quote:
Originally Posted by berbae
This seems to do what you want:[...]
Uh, that is, unless there are other occurrences of '<br>'.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl: Search and replace directories within text files Erhnam Programming 2 03-07-2006 04:07 AM
How to search and replace a text using grep DediPlace Linux - General 2 05-29-2005 06:47 PM
Script to search and replace in text file - kinda... jeffreybluml Programming 45 11-07-2004 05:37 PM
Search and replace text in file using shell script? matthurne Linux - Software 2 11-02-2004 10:11 AM
trying to search and replace text file for single & multiple line breaks separately brokenfeet Programming 7 08-29-2003 01:56 PM


All times are GMT -5. The time now is 09:48 PM.

Main Menu
 
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration