Search and replace multi-line text in files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search and replace multi-line text in files
# 1  
Old 10-06-2005
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  
Old 10-06-2005
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  
Old 10-07-2005
Thank you very much for the solution.

Works great! Smilie
# 4  
Old 10-07-2005
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  
Old 10-07-2005
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  
Old 10-07-2005
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  
Old 10-07-2005
My guess is that he is not using RS="" and his file does not contain a line with more than 10,239 bytes.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Multi line regex for search and replace

I have text file like below: a.txt Server=abc Run=1 Time=120.123 Tables=10 Sessions=16 Time=380.123 Version=1.1 Jobs=5 Server=abc Run=2 Time=160.123 Tables=15 Sessions=16 Time=400.258 Version=2.0 (1 Reply)
Discussion started by: sol_nov
1 Replies

2. Shell Programming and Scripting

Search and Replace a text if the line contains a pattern

My text file looks like below . . . abcdefghi jklmnop $Bad_ptrq_GTS=rcrd_ip.txt $Bad_abcd_REJ=rcrd_op.txt ghijklm $Bad_abcd_TYHS=rcrd_op.txt abcgd abcdefghi jklmnop $Bad_ptrq_GTS=rcrd_ip.txt (2 Replies)
Discussion started by: machomaddy
2 Replies

3. Shell Programming and Scripting

Search text and replace line next to it

I have a file which requires modification via a shell script. Need to do the following: 0. take input from user for new text. 1. search for a keyword in the file. 2. replace the line next to this to this keyword with user supplied input. for e.g., my file has the following text: (some... (7 Replies)
Discussion started by: chingupt
7 Replies

4. Shell Programming and Scripting

Keyword search/replace for two text files?

What is the best way (bash/awk/sed?) to read in two text files and do a keyword search/replace? file1.txt: San Francisco Los Angeles Seattle Dallas file2.txt: I love Los Angeles. Coming to Dallas was the right choice. San Francisco is fun. Go to Seattle in the summer. ... (3 Replies)
Discussion started by: pxalpine
3 Replies

5. UNIX for Dummies Questions & Answers

VIM search and replace with line breaks in both the target and replacement text

Hi, Ive spent ages trying to find an explanation for how to do this on the web, but now feel like I'm :wall: I would like to change each occurence (there are many within my script) of the following: to in Vim. I know how to search and replace when it is just single lines... (2 Replies)
Discussion started by: blueade7
2 Replies

6. Shell Programming and Scripting

Global search and replace multi line file

Hello I need to search for a mult-line strngs(with spaces in between and qoted) in a file1 and replace that text with Fixed string globally in file1. The strng to search for is in file2. The file is big with some 20K records. so speed and effciency is required file1: (where srch & rplc... (0 Replies)
Discussion started by: Hiano
0 Replies

7. Shell Programming and Scripting

Search and replace text in many files

Hello, I am very new to Linux and am trying to find a way for following problem. I have a number of files in a folder as Export000.dat, Export001.dat ..and so on. Each file has a string field 'Absolute velocity'. I want it to be replaced by 'Pixel shift' in all the files. I tried something like... (4 Replies)
Discussion started by: chirag.joshi
4 Replies

8. Shell Programming and Scripting

Multi-Line Search and Replace

There appears to be several threads that touch on what I'm trying to do, but nothing quite generic enough. What I need to do is search through many (poorly coded) HTML files and make changes. The catch is that my search string may be on one line or may be on several lines. For example there... (5 Replies)
Discussion started by: bisbell
5 Replies

9. Shell Programming and Scripting

how to do search and replace on text files in directory

I was google searching and found Perl as a command line utility tool This almost solves my problem: find . | xargs perl -p -i.old -e 's/oldstring/newstring/g' I think this would create a new file for every file in my directory tree. Most of my files will not contain oldstring and I... (1 Reply)
Discussion started by: siegfried
1 Replies

10. Shell Programming and Scripting

Perl: Search for string on line then search and replace text

Hi All, I have a file that I need to be able to find a pattern match on a line, search that line for a text pattern, and replace that text. An example of 4 lines in my file is: 1. MatchText_randomNumberOfText moreData ReplaceMe moreData 2. MatchText_randomNumberOfText moreData moreData... (4 Replies)
Discussion started by: Crypto
4 Replies
Login or Register to Ask a Question