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 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 03:20 AM
automating file search and replace text ommatidia Shell Programming and Scripting 3 02-28-2008 01:40 PM
Perl: Search for string on line then search and replace text Crypto Shell Programming and Scripting 4 01-04-2008 07:24 AM
search and replace the whole line Jartan Shell Programming and Scripting 17 09-25-2007 10:58 AM
How do you search and replace a text with markerA that end with markerB drone Shell Programming and Scripting 1 06-19-2003 06:12 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #8  
Old 10-09-2005
Registered User
 

Join Date: Oct 2005
Posts: 5
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.
Reply With Quote
Forum Sponsor
  #9  
Old 10-09-2005
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,667
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
Reply With Quote
  #10  
Old 10-09-2005
Ygor's Avatar
Moderator
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,249
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;
'
Reply With Quote
  #11  
Old 10-10-2005
Registered User
 

Join Date: Oct 2005
Posts: 5
It almost did. Postscript files contain \n \ and / etc. With some modifications this code works!

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...
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 06:10 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0