yysaki
4/4/2013 - 1:22 PM

## シェル操作課題 (cut, sort, uniq などで集計を行う)解答

シェル操作課題 (cut, sort, uniq などで集計を行う)解答

``````#!/bin/bash
# [Questions](http://d.hatena.ne.jp/Yamashiro0217/20120727/1343371036)

echo "make data"
cat << EOS > data
server1,1343363124,30,/video.php
server2,1343363110,20,/profile.php
server1,1343363105,8,/profile.php
server2,1343363205,35,/profile.php
server2,1343363110,20,/profile.php
server4,1343363225,12,/video.php
server1,1343363265,7,/video.php
EOS

echo "Q.1"
cat data

echo "Q.2"
awk -F, '{print \$1,\$4}' data

echo "Q.3"
grep server4 data

echo "Q.4"
cat data | wc -l

echo "Q.5"
sort -t, -k1,1 -k3,3n data | head -5

echo "Q.6"
sort data | uniq | wc -l

echo "Q.7"
awk -F, '{print \$3}' data | sort | uniq | wc -l

echo "Q.8"
awk -F, '{print \$4}' data | sort | uniq -c | sort -k1,1rn | head -1 | sed "s/ //g"

echo "Q.9"
awk -F, '{print \$1}' data | sort | uniq -c | sort -k1,1rn | sed "s/ //g" | sed "s/server/ xxx/"

echo "Q.10"
awk -F, '{print \$3}' data | sort -n | uniq | grep [0-9][0-9] | sort

echo "remove data"
rm data
``````