![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#8
|
|||
|
|||
|
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. |
| Forum Sponsor | ||
|
|
|
#9
|
||||
|
||||
|
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
|
||||
|
||||
|
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
|
|||
|
|||
|
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:
|
|||
| Google The UNIX and Linux Forums |