The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to search and replace text in same file Vrgurav Shell Programming and Scripting 1 04-25-2008 07:20 AM
automating file search and replace text ommatidia Shell Programming and Scripting 3 02-28-2008 04:40 PM
Perl: Search for string on line then search and replace text Crypto Shell Programming and Scripting 4 01-04-2008 10:24 AM
search and replace the whole line Jartan Shell Programming and Scripting 17 09-25-2007 02:58 PM
How do you search and replace a text with markerA that end with markerB drone Shell Programming and Scripting 1 06-19-2003 10:12 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-06-2005
marz marz is offline
Registered User
  
 

Join Date: Oct 2005
Posts: 5
Search and replace multi-line text in files

Hello
I need to search for a mult-line text in a file exfile1 and replace that text with another text. The text to search for is in exfile2 and the replacement text is in exfile3.

I work with kornshell under AIX and need to do this with a lot of files. (the file type is postscript and they need to be edited before printing with our old card plotter that cannot manage bitmaps)

exfile1:
asdasdasdasd
asdasdasdasd
abc
def
ghi
sdasdasdasda
asdasdasdada

exfile2:
abc
def
ghi

exfile3:
jkl
mno
pqr

I have tried with sed with little sucess.
Any ideas?
  #2 (permalink)  
Old 10-06-2005
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,411
You can't use sed because its processing is line-based. You can use awk if you unset the record separator, like this...
Code:
awk ' BEGIN { RS="" }
      FILENAME==ARGV[1] { s=$0 }
      FILENAME==ARGV[2] { r=$0 }
      FILENAME==ARGV[3] { sub(s,r) ; print }
    ' exfile2 exfile3 exfile1 > exfile4

..which gives...
Code:
asdasdasdasd
asdasdasdasd
jkl
mno
pqr
sdasdasdasda
asdasdasdada

  #3 (permalink)  
Old 10-07-2005
marz marz is offline
Registered User
  
 

Join Date: Oct 2005
Posts: 5
Thank you very much for the solution.

Works great!
  #4 (permalink)  
Old 10-07-2005
marz marz is offline
Registered User
  
 

Join Date: Oct 2005
Posts: 5
I have tested a little more and I have a problem.
All the files are bigger than 10,239 bytes and cannot be processed by the awk function.

Error:
"awk: 0602-534 Input line xxxxxxxx cannot be longer than 10,239 bytes."

Any idea to solve this problem?

Best regards
marz
  #5 (permalink)  
Old 10-07-2005
akrathi akrathi is offline
Registered User
  
 

Join Date: Sep 2005
Posts: 41
I am doing awk on file size of 84461608 bytes without
any trouble . Can you please expalin what all you are doing on that ?
  #6 (permalink)  
Old 10-07-2005
reborg's Avatar
reborg reborg is offline Forum Staff  
Administrator
  
 

Join Date: Mar 2005
Location: Ireland
Posts: 4,246
Quote:
Originally Posted by akrathi
I am doing awk on file size of 84461608 bytes without
any trouble
My guess is that you are:
1. Working on a different platform
2. You are woking with gnu awk, the OP is not.
  #7 (permalink)  
Old 10-09-2005
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,411
This is the same idea as before but using perl instead of awk...
Code:
perl -e '
   undef $/;
   open(SEAR, "< exfile2");
   open(REPL, "< exfile3");
   open(INFI, "< exfile1");
   open(OUTF, "> exfile4");
   $sear = <SEAR>;
   $repl = <REPL>;
   $data = <INFI>;
   $data =~ s/$sear/$repl/g;
   print OUTF $data;
'

Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:53 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0