シェル芸4回め 第4回春爛漫シェル芸芸人撩乱勉強会 自分用の履歴とメモ
第一問
[hirasawa@ubuntu1204-20 DATA]$ cat 2.html <a href="http://my.favorite.jk/index.cgi#jk">会長のブログ</a> <a href="http://index_cgi.com/index.cgi?p=index.cgi">aho</a> [hirasawa@ubuntu1204-20 DATA]$ history | grep 2.html 2030 cat 2.html 2031 cat 2.html | sed 's 2032 cat 2.html | sed '|||' 2033 cat 2.html | sed 's|index.cgi||g' 2034 cat 2.html | sed 's|/index.cgi||g' 2041 cat 2.html | awk '{sub("index","",$0); print}' 2045 cat 2.html | awk '{sub("index.cgi","",$0); print}' 2056 cat 2.html | awk '{gsub(/index.cgi/,"",$0); print}' [hirasawa@ubuntu1204-20 DATA]
こちらは、MLに流れてた別解。 grep -oってのがあるんでね
しりませんでした
[hirasawa@localhost ~]$ cat data.txt | grep -o "http://.*/index.cgi" http://my.favorite.jk/index.cgi http://index_cgi.com/index.cgi [hirasawa@localhost ~]$
第二問 rulを抜き出す
[hirasawa@ubuntu1204-20 DATA]$ cat 1.html <a href="http://www.aho.com"> アホどっと混む </a> <a href="#top">トップへ</a> <a href="http://www.jk.jp">"JK"</a> [hirasawa@ubuntu1204-20 DATA]$ cat 1.html | grep 'href=' <a href="http://www.aho.com"> <a href="#top">トップへ</a> <a href="http://www.jk.jp">"JK"</a> [hirasawa@ubuntu1204-20 DATA]$ cat 1.html | grep 'href=' | sed 's/^.*href="\(..*\)".*$/\1/' http://www.aho.com #top http://www.jk.jp">"JK [hirasawa@ubuntu1204-20 DATA]$ cat 1.html | grep 'href=' | sed 's/^.*href="\(..*\)".*$/\1/' |grep "^http" http://www.aho.com http://www.jk.jp">"JK [hirasawa@ubuntu1204-20 DATA]$
第3問 インデントに汚いhtmlを整形
[hirasawa@ubuntu1204-20 DATA]$ cat 3.html <table> <tr> <td>a</td><td>b</td> <td>c</td> </tr> </table> [hirasawa@ubuntu1204-20 DATA]$ cat 3.html | sed 's|</[^>][^>]*>|&\n|g' <table> <tr> <td>a</td> <td>b</td> <td>c</td> </tr> </table> [hirasawa@ubuntu1204-20 DATA]$ cat 3.html | sed 's|</[^>][^>]*>|&\n|g' | sed 's|\t\t*||g' <table> <tr> <td>a</td> <td>b</td> <td>c</td> </tr> </table> [hirasawa@ubuntu1204-20 DATA]$ cat 3.html | sed 's|</[^>][^>]*>|&\n|g' | sed 's|\t\t*||g' | grep -v "^$" <table> <tr> <td>a</td> <td>b</td> <td>c</td> </tr> </table> [hirasawa@ubuntu1204-20 DATA]$
第四問 webページの切り出し
[hirasawa@ubuntu1204-20 DATA]$ curl http://www.yahoo.html curl: (6) Couldn't resolve host 'www.yahoo.html' [hirasawa@ubuntu1204-20 DATA]$ [hirasawa@ubuntu1204-20 DATA]$ curl http://www.yahoo.co.jp > yahoo.txt % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 24951 0 24951 0 0 193k 0 --:--:-- --:--:-- --:--:-- 420k [hirasawa@ubuntu1204-20 DATA]$
ここまで前準備しておいたうえで
[hirasawa@ubuntu1204-20 DATA]$ cat yahoo.txt | sed 's|</[^/][^/]*>|&\n|g' | grep top_table <form name="sf1" action="http://search.yahoo.co.jp/search"><input type="hidden" name="fr" value="top_table"><input type="hidden" name="tid" value="top_table"><input type="hidden" name="ei" value="UTF-8"><table border="0" cellspacing="0" cellpadding="0"><tr><td><table cellpadding="0" cellspacing="0" border="0"><tr><td><input type="text" name="p" id="p" size="20"></td><td> ・<a href="f/topics/top_table/1/*-http://dailynews.yahoo.co.jp/fc/domestic/rikuzankai/?1366598817">元検事に不起訴不当 検察審</a> ・<a href="f/topics/top_table/2/*-http://dailynews.yahoo.co.jp/fc/science/medical_issues/?1366602325">小児難病、成人後も治療5万人</a> ・<a href="f/topics/top_table/3/*-http://dailynews.yahoo.co.jp/fc/world/japan_south_korea_relations/?1366597648">韓国外相が訪日取りやめ</a> ・<a href="f/topics/top_table/4/*-http://dailynews.yahoo.co.jp/fc/world/boston_marathon_explosions/?1366600084">容疑者兄弟 別のテロも計画か</a> ・<a href="f/topics/top_table/5/*-http://dailynews.yahoo.co.jp/fc/economy/company_affiliation/?1366591427">川崎重工と三井造船に統合案</a> ・<a href="f/topics/top_table/6/*-http://dailynews.yahoo.co.jp/fc/sports/uehara_koji/?1366602325">上原浩治 被弾で無失点止まる</a> ・<a href="f/topics/top_table/7/*-http://dailynews.yahoo.co.jp/fc/sports/volleyball_women_national_team/?1366602601">バレー女子 大友愛が現役引退</a> ・<a href="f/topics/top_table/8/*-http://dailynews.yahoo.co.jp/fc/entertainment/hollywood_stars/?1366600084">米オスカー女優 口論で逮捕</a> <img src="http://k.yimg.jp/images/clear.gif" alt="" width="10" height="20"><a href="f/topics/top_table/11/*-http://backnumber.dailynews.yahoo.co.jp/?t=d&d=20130422&c=top">今日の話題(22件)</a> <a href="f/topics/top_table/9/*-http://dailynews.yahoo.co.jp/photograph/pickup/?1366593983"><img src="http://news.c.yimg.jp/images/topics/20130422-00000013-jijp-000-view.jpg" alt="琢磨が日本人初V" width:120px;height:79px; border="0"></a> <a href="f/topics/top_table/10/*-http://dailynews.yahoo.co.jp/photograph/pickup/?1366593983">琢磨が日本人初V</a><br><font size="-3" color="#666666">4月22日9時34分配信<br> [hirasawa@ubuntu1204-20 DATA]$ [hirasawa@ubuntu1204-20 DATA]$ cat yahoo.txt | sed 's|</[^/][^/]*>|&\n|g' | grep top_table | sed 's|^.*<a href="\([^"][^"]*\)".*$|\1|' <form name="sf1" action="http://search.yahoo.co.jp/search"><input type="hidden" name="fr" value="top_table"><input type="hidden" name="tid" value="top_table"><input type="hidden" name="ei" value="UTF-8"><table border="0" cellspacing="0" cellpadding="0"><tr><td><table cellpadding="0" cellspacing="0" border="0"><tr><td><input type="text" name="p" id="p" size="20"></td><td> f/topics/top_table/1/*-http://dailynews.yahoo.co.jp/fc/domestic/rikuzankai/?1366598817 f/topics/top_table/2/*-http://dailynews.yahoo.co.jp/fc/science/medical_issues/?1366602325 f/topics/top_table/3/*-http://dailynews.yahoo.co.jp/fc/world/japan_south_korea_relations/?1366597648 f/topics/top_table/4/*-http://dailynews.yahoo.co.jp/fc/world/boston_marathon_explosions/?1366600084 f/topics/top_table/5/*-http://dailynews.yahoo.co.jp/fc/economy/company_affiliation/?1366591427 f/topics/top_table/6/*-http://dailynews.yahoo.co.jp/fc/sports/uehara_koji/?1366602325 f/topics/top_table/7/*-http://dailynews.yahoo.co.jp/fc/sports/volleyball_women_national_team/?1366602601 f/topics/top_table/8/*-http://dailynews.yahoo.co.jp/fc/entertainment/hollywood_stars/?1366600084 f/topics/top_table/11/*-http://backnumber.dailynews.yahoo.co.jp/?t=d&d=20130422&c=top f/topics/top_table/9/*-http://dailynews.yahoo.co.jp/photograph/pickup/?1366593983 f/topics/top_table/10/*-http://dailynews.yahoo.co.jp/photograph/pickup/?1366593983
第五問 数値参照の変換
へー、ほー、はー、
[hirasawa@ubuntu1204-20 DATA]$ cat numref 我々はシェル芸人だ。 [hirasawa@ubuntu1204-20 DATA]$ cat numref | nkf --numchar-input 我々はシェル芸人だ。 [hirasawa@ubuntu1204-20 DATA]$ cat numref | w3m -T text/html -dump 我々はシェル芸人だ。 [hirasawa@ubuntu1204-20 DATA]$
第六問
次のCGIが動いているサーバにポストする
[hirasawa@ubuntu1204-20 DATA]$ cat hoge.cgi #!/bin/bash -xv echo "Content-type: text/html" echo "" echo あなたのポストしたデータは、 dd bs=$CONTENT_LENGTH echo "" echo です。 [hirasawa@ubuntu1204-20 DATA]$
ローカルでとにかく実行してみる
[hirasawa@ubuntu1204-20 DATA]$ ./hoge.cgi #!/bin/bash -xv echo "Content-type: text/html" + echo 'Content-type: text/html' Content-type: text/html echo "" + echo '' echo あなたのポストしたデータは、 + echo $'\343\201\202\343\201\252\343\201\237\343\201\256\343\203\235\343\202\271\343\203\210\343\201\227\343\201\237\343\203\207\343\203\274\343\202\277\343\201\257\343\200\201' あなたのポストしたデータは、 dd bs=$CONTENT_LENGTH + dd bs= dd: `' : 無効な数字です echo "" + echo '' echo です。 + echo $'\343\201\247\343\201\231\343\200\202' です。 [hirasawa@ubuntu1204-20 DATA]$
またまた、へーっておもたよ
手元に実行する環境もないので、w
とりあえず答えだけ コピペ
wget --post-data a=aho <url> curl --data=urlencode a=aho <url> curl --data a=aho <url> seq 1 10000 | xargs -P 1000 -l@ curl --data-urlencode a=@ http:<rul>
忘れてなければ、ローカルで確かめるw
第七問 データの収集 本日の最高気温をゲットする
[hirasawa@ubuntu1204-20 DATA]$ curl http://weather.yahoo.co.jp/weather/jp/13/4410.html 2> /dev/null | grep "最高" <meta name="description" content="今日・明日の天気と風と波、明日までの6時間ごとの降水確率と最高・最低気温を見られます。"> <td><small><font color="#ff3300">最高気温(℃)[前日差]<br>17 [ +7 ]</font></small></td> <td><small><font color="#ff3300">最高気温(℃)[前日差]<br>18 [ +1 ]</font></small></td> [hirasawa@ubuntu1204-20 DATA]$ curl http://weather.yahoo.co.jp/weather/jp/13/4410.html 2> /dev/null | grep "最高" | tail -n 2 | head -n 1 <td><small><font color="#ff3300">最高気温(℃)[前日差]<br>17 [ +7 ]</font></small></td> [hirasawa@ubuntu1204-20 DATA]$
第8問と9問目
画像データの収集とクローリングw
[hirasawa@ubuntu1204-20 DATA]$ curl http://headlines.yahoo.co.jp/hl 2>/dev/null | nkf -w -Lux | sed 's/src="/\n&/g' | grep src= | sed 's/^src="\([^"]*\)".*$/\1/' | while read f ; do wget $f ; done [hirasawa@ubuntu1204-20 DATA]$ curl http://headlines.yahoo.co.jp/hl 2>/dev/null | nkf -w -Lux | sed 's/src="/\n&/g' | grep src= | sed 's/^src="\([^"]*\)".*$/\1/' | xargs wget [hirasawa@ubuntu1204-20 www.yahoo.co.jp]$ curl http://www.yahoo.co.jp | sed 's/href="/\n&/g' | grep ^href= | sed 's/[^"]*"//' | sed 's/".*$//' | awk '/^http/{print $0}!/http/{print "http://www.yahoo.co.jp/" $0}' | xargs wget あるいは wget -r http://www.yahoo.co.jp だそうです。こりゃすっげーやw