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
# 8  
Old 10-09-2005
I'am running AIX 5.2 on IBM RS/6000.
I ran this script with RS="" and it worked great on small files. But when i started testing with bigger files this error occured.
the search, replace and input files are alla bigger than 10k.
I will test more on monday.
# 9  
Old 10-09-2005
Maybe this will do it...
Code:
#! /usr/bin/ksh

DATA=data
OLD=old
NEW=new
TMP=repfiletmp$$


target1=$(sed 1q < $OLD)
targetnum=$(wc -l < $OLD)
echo target: $targetnum $target1
echo tmpfile = $TMP


exec < $DATA
IFS=""
while read line ; do
        if [[ $line = $target1 ]] ; then
                tmpct=1
                echo $line > $TMP
                while ((tmpct<targetnum)) ; do
                        read line && echo $line >> $TMP
                        ((tmpct=tmpct+1))
                done
                if cmp -s $OLD $TMP ; then
                        cat $NEW
                else
                        cat $TMP
                fi
                rm $TMP

        else
                echo $line
        fi
done

exit 0

# 10  
Old 10-09-2005
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;
'

# 11  
Old 10-10-2005
It almost did. Postscript files contain \n \ and / etc. With some modifications this code works! Smilie

I think the perl variant that Ygor posted is faster. But i have not been able to get it to work with \n \ and / and such.


Code:
DATA=ain.ps
OLD=aold.ps
NEW=anew.ps
OUT=aout.ps
rm -f $OUT

TMP=repfiletmp$$
target1=$(sed 1q < $OLD)
targetnum=$(wc -l < $OLD)
echo target: $targetnum $target1
echo tmpfile = $TMP

exec < $DATA
IFS=""
while read -r line ; do
        if [[ $line = $target1 ]] ; then
                tmpct=1
                print -r -- "$line" > $TMP
                while ((tmpct<targetnum)) ; do
                        read -r line && print -r -- "$line" >> $TMP
                        ((tmpct=tmpct+1))
                done
                if cmp -s $OLD $TMP ; then
                        cat $NEW >> $OUT
                else
                        cat $TMP >> $OUT
                fi
                rm $TMP
        else
                print -r -- "$line" >> $OUT
        fi
done
exit 0

Quote:
Originally Posted by Perderabo
Maybe this will do it...
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