Tech Space

Experiencing Tech as a Professional Techie

sed/sh/awk removing a section in multiple files

Posted on | January 10, 2007 | No Comments

All of my scripts are in ~/bin, I’m replacing a marked section with my google adsense code, contained in a separate file.

remove_topstart.sh:
#!/bin/sh
find . -name ‘*.html’ | while read x
do
   cat $x | awk -f ~/bin/remove_topstart.awk >> $x.$$
   cp $x.$ $x
   rm $x.$
done


remove_topstart.awk:
BEGIN { outsideTop = 1 }
/<!–TOPSTART–>/ { outsideTop = 0 }
/<!–TOPEND–>/ { outsideTop = 1 }
outsideTop {
   if( $0 !~ “<!–TOPEND–>” )
   {
      print $0
   }
}

insert_adsense.sh:
#!/bin/sh
#my.adsense.txt contains as its first line and as
#its last line.


#insert after tag, regardless of case.
#change index.html in current directory
cat index.html | sed ‘/<[Bb][Oo][Dd][Yy]>/r /home/thomas/my.adsense.txt’ > index.html.$$
cp index.html.$ index.html
rm index.html.$

#change all *.html files in selected subdirectories
find . -name ‘*.html’ | egrep “SubDir1|SubDir2|SubDir3|SubDir4|SubDir5″ | while read x
do
   cat $x | sed ‘/<[Bb][Oo][Dd][Yy]>/r /home/thomas/my.adsense.txt’ > $x.$$
   cp $x.$$ $x
   rm $x.$$
done
Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • LinkedIn
  • Live
  • StumbleUpon
  • Tumblr
  • TwitThis
  • FriendFeed
  • Netvibes
  • Ping.fm
  • Posterous
  • Technorati
  • Tipd
  • Twitter
  • Yahoo! Buzz

Comments

Leave a Reply





CommentLuv Enabled