Neo
August 7, 2007, 4:39pm
1
Well, I'm losing my regex ability in sed! Please help.
I need to search for this text in multiple html files in a directory:
</body>
and add the following lines in front of the text above:
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-1234567-00";
urchinTracker();
</script>
to finally read:
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-1234567-00";
urchinTracker();
</script>
</body>
... for each file in the directory.
... and everything I try with sed keeps giving me errors.
Thanks for your help!
reborg
August 7, 2007, 5:06pm
2
The simplest way I can think of, given the embedded slashes is a substitution:
sed 's*</body>*<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">\
</script>\
\
<script type="text/javascript">\
_uacct = "UA-1234567-00";\
urchinTracker();\
</script>\
</body>*' filename
Neo
August 7, 2007, 5:37pm
3
Hi reborg!
Thanks. That works for a single file to standard out very well.
How about this operation for many files in a single directory? Do I need to wrap this in a shell script: read each file in the directory, write to a tmp file and then mv the tmp file to replace the original file?
Or can we do this with one line in sed?
mStr1='<script src="http://www.google-analytics.com/urchin.js"'
mStr2=' type="text/javascript"> </script> <script type="text/javascript"'
mStr3='> _uacct = "UA-1234567-00"; urchinTracker(); </script>'
for mFName in `find . -type f`
do
while read mLine
do
if [ "${mLine}" = "</body>" ]; then
echo ${Str1}
echo ${Str2}
echo ${Str3}
fi
echo ${mLine}
done < ${mFName} > $$Temp
mv $$Temp ${mFName}
done
Neo
August 7, 2007, 5:45pm
5
Thanks shell_life.
I also did it this way:
!/bin/sh
for file in $(ls *.html)
do
sed 's*</body>*<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">\
</script>\
\
<script type="text/javascript">\
_uacct = "UA-1234567-00";\
urchinTracker();\
</script>\
</body>*' $file > /tmp/tmpfile.tmp
mv /tmp/tmpfile.tmp $file
done
I was hoping to find a simple "one liner" vs. a shell script wrapper around sed. It seems there are no "one liners".
Thanks for the suggestions!
This seems to be the best solution for a "one liner".
Good luck.
reborg
August 7, 2007, 5:54pm
7
Unless using gsed...
in which case:
gsed -i 's*</body>*<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">\
</script>\
\
<script type="text/javascript">\
_uacct = "UA-1234567-00";\
urchinTracker();\
</script>\
</body>*' *.html
gets rid of the script The alternative would be to replace the sed in the above expression with a 'perl -pi -e '.
for i in *.txt
do
ex - ${i} <<EOF
%s#foo#bar#g
wq!
EOF
# or with printf
# printf '%s#foo#bar#g\n.\nwq!\n' | ex "${i}"
done
Neo
August 7, 2007, 5:58pm
9
reborg:
Unless using gsed...
in which case:
gsed -i 's*</body>*<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">\
</script>\
\
<script type="text/javascript">\
_uacct = "UA-1234567-00";\
urchinTracker();\
</script>\
</body>*' *.html
gets rid of the script
Hi reborg! Thanks! I am on Mac OSX, which does not seem to have gsed. So, I'll stick with the wrapper for now, since it works fine.
Hi shell_life, I'll edit my post to fix my typo, LOL Thanks again!
reborg
August 7, 2007, 6:07pm
10
I edited while you were replying, maybe you have perl, I don't have a Mac handy (unless I'm in the office) to check.
Neo
August 7, 2007, 6:11pm
11
Hi reborg!
Yes, that is the way I used to do years ago it before I forgot (with perl, not sed), thanks; and yes, OSX has perl out-of-the-box.
Thanks again! Mission accomplished!
find ./ -type f -exec sed 's*</body>*<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">\
</script>\
\
<script type="text/javascript">\
_uacct = "UA-1234567-00";\
urchinTracker();\
</script>\
</body>*' {} \;