Sunday, October 22, 2006

sed sample: to check duplicated packages

If there are duplicated packages installed in the FreeBSD system, you can use such script to check them out by the command sed:

pkg_info | sort | sed -e 's/-[0-9].*$//' | \
uniq -c | grep -v '^[[:space:]]*1'

Another easy sample, to replace pattern1 with pattern2 in a file:

  1. generate a backup file, foo.bak
    sed -i.bak s/pattern1/pattern2/g foo
  2. no backup files generated, notice there is no space after -i:
    sed -i'' s/pattern1/pattern2/g foo

Once, I use wget to download a whole website. However, the charset of these web pages I downloaded are no set correctly, which should orignally be utf-8. Thus, I resolved this problem with the method below.

  • Firstly, download the pages into a directory named myfavor:
    wget -m -E -Pmyfavor -k http://junist.googlepages.com
  • Then, use this command to set the correct charset for all html files:
    find . -name "*html" -type f -print | xargs sed -i'' '/<\/title>/a\\<meta\ http-equiv="Content-Type"\ content="text\/html;\ charset=utf-8">'

No comments: