Created
June 23, 2012 16:10
-
-
Save tdkn/2978835 to your computer and use it in GitHub Desktop.
kadai 20120621
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11月25日 | |
バス回数券; 2300; 交通費 | |
昼飯; 600; 食費 | |
珈琲豆; 1000; 嗜好品 | |
11月26日 | |
ガソリン代; 3210; 交通費 | |
夕飯; 800; 食費 | |
傘; 1050; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# 名前の姓と名の間は半角空白で分けられているが、 | |
# これをコンマに変更するプログラムを作成せよ。 | |
cat ./data.csv | awk ' | |
BEGIN { | |
FS = "," | |
} | |
{ | |
sub(/^[ ]/,"",$2) | |
sub(/[ ]+/,",",$2) | |
print $1","$2","$3 | |
}' > data1.csv | |
cat ./data1.csv | |
# TODO: | |
# [x] 2フィールド目の頭に空白があったとき消す | |
# [x] 2フィールド目の姓と名の間の空白を","で置換 | |
# [ ] 上記の段階を踏まずに一発で姓と名の間の空白のみを置換できないか? | |
# [ ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# id, 姓 名, 住所 | |
# 1, 田中 一郎,東京都港区赤坂七丁目10-17 | |
# 2, 田中 二郎, 神奈川県横浜市中央区港町1丁目 | |
# 3, 田中 三郎, 沖縄県那覇市上之屋1丁目2−1 | |
# : | |
# : | |
# 都道府県名と、それ以下の住所の間を | |
# コンマで区切るプログラムを作成せよ。 | |
cat ./data.csv | awk ' | |
BEGIN { | |
FS = "," | |
} | |
{ | |
sub(/^[ ]?/,"",$2) | |
sub(/[ ]+/,",",$2) | |
match($0,/都|道|府|県/) | |
pref=substr($0,RSTART,RLENGTH); | |
sub(/都|道|府|県/,pref",",$3) | |
print $1","$2","$3 | |
}' > data2.csv | |
cat ./data2.csv |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/awk -f | |
# ひらがなの文を入力したとき、それが回文であるかを | |
# チェックするプログラムを作成しなさい。 | |
BEGIN { | |
print "start." | |
} | |
{ | |
if($0 ~ /^[ 、。あ-ん\t]+$/) { | |
gsub(/[^あ-ん]/,"") | |
len = length($0) | |
# print "$0: "$0 | |
# printf("len: %d\n", len) | |
# printf("len%2: %d\n", len % 2) | |
print "元 逆 判" | |
for(i=len; i>0; i--) { | |
org=substr($0,(len+1)-i,1) # 元の文 | |
rev=substr($0,i,1) # 反転後の文 | |
if(org == rev) { | |
judge="o" | |
} else { | |
judge="x" | |
} | |
print org" "rev" "judge | |
} | |
} else { | |
print "ひらがな以外が入力されています." | |
} | |
} | |
END { | |
print "end." | |
} | |
# TODO: | |
# [x] そもそもひらがな文であるかどうかの判定 | |
# |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# ひらがなの文を入力したとき、それが回文であるかを | |
# チェックするプログラムを作成しなさい。 | |
echo -n "回文を入力: " | |
read judge_str | |
echo $judge_str | awk '{ | |
if($0 ~ /^[ 、。あ-ん\t]+$/) { | |
gsub(/[^あ-ん]/,"") | |
len = length($0) | |
# 判定用フラグに1を入れておく | |
flg = 0 | |
# 回分かどうかを1文字づつ判定 | |
for(i=len; i>0; i--) { | |
org=substr($0,(len+1)-i,1) # 元の文 | |
rev=substr($0,i,1) # 反転後の文 | |
# 文字列が一致しなかったら | |
if(org != rev) { | |
flg = 1 | |
break | |
} | |
} | |
if(flg == 1) { | |
print "回文ではないようです." | |
} else { | |
print "回文ですね!!!" | |
} | |
} else { | |
print "ひらがな以外が入力されています." | |
} | |
}' | |
# TODO: | |
# [x] そもそもひらがな文であるかどうかの判定 | |
# |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/awk -f | |
# 英語の文を入力したとき、それが回文であるかを | |
# チェックするプログラムを作成しなさい。 | |
# 回文の例: | |
# Now I see, referees, I won. | |
# Kodak ad? OK! | |
# Damn! I, Agassi, miss again! Mad! | |
# Madam, I'm Adam. | |
BEGIN { | |
print "start." | |
} | |
{ | |
if($0 ~ /^[ ,!'a-zA-z\.\?\t]+$/) { | |
str = tolower($0) # アルファベットを少文字に統一 | |
gsub(/[^a-z]/,"",str) # アルファベット以外を除去 | |
len = length(str) # 文字列の長さ | |
print "$0: "$0 | |
# printf("len: %d\n", len) | |
# printf("len%2: %d\n", len % 2) | |
print "元 逆 判" | |
for(i=len; i>0; i--) { | |
org=substr(str,(len+1)-i,1) # 元の文 | |
rev=substr(str,i,1) # 反転後の文 | |
if(org == rev) { | |
judge="o" | |
} else { | |
judge="x" | |
} | |
print org" "rev" "judge | |
} | |
} else { | |
print "英語以外が入力されています." | |
} | |
} | |
END { | |
print "end." | |
} | |
# TODO: | |
# [x] 記号等の除去 | |
# [x] 大文字、少文字の考慮 | |
# [x] そもそも英語であるかどうかの判定 | |
# |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/bash | |
# 英語の文を入力したとき、それが回文であるかを | |
# チェックするプログラムを作成しなさい。 | |
# 回文の例: | |
# Now I see, referees, I won. | |
# Kodak ad? OK! | |
# Damn! I, Agassi, miss again! Mad! | |
# Madam, I'm Adam. | |
echo -n "回文を入力: " | |
read judge_str | |
echo $judge_str | awk '{ | |
if($0 ~ /^[ ,!'\''a-zA-z\.\?\t]+$/) { | |
str = tolower($0) # アルファベットを少文字に統一 | |
gsub(/[^a-z]/,"",str) # アルファベット以外を除去 | |
len = length(str) # 文字列の長さ | |
# 判定用フラグに1を入れておく | |
flg = 0 | |
# 回分かどうかを1文字づつ判定 | |
for(i=len; i>0; i--) { | |
org=substr(str,(len+1)-i,1) # 元の文 | |
rev=substr(str,i,1) # 反転後の文 | |
# 文字列が一致しなかったら | |
if(org != rev) { | |
flg = 1 | |
break | |
} | |
} | |
if(flg == 1) { | |
print "回文ではないようです." | |
} else { | |
print "回文ですね!!!" | |
} | |
} else { | |
print "英語以外が入力されています." | |
} | |
}' | |
# TODO: | |
# [x] 記号等の除去 | |
# [x] 大文字、少文字の考慮 | |
# [x] そもそも英語であるかどうかの判定 | |
# |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/awk -f | |
#kakei5.awk: 家計簿プログラム 第5版: 最後の日の集計も表示。 | |
# | |
# 家計簿のファイルの書式 | |
# | |
# コメント ; 金額 ; 分類 ; さらにコメントを書ける | |
# | |
BEGIN { | |
FS = ";"; | |
} | |
/^\s*[0-9]+月[0-9]+日/ { # 行頭から日付が書かれていたら、 | |
dtotal = 0; # 日付単位の集計結果をクリア。 | |
for (i in dclass) | |
dclass[i] = 0; | |
} | |
NF >= 2 { | |
gsub(/[ \t]+/, ""); | |
total += $2; | |
dtotal += $2; | |
if (length($3) > 0) { | |
class[$3] += $2; | |
dclass[$3] += $2; | |
} else { | |
class["NONE"] += $2; # 分類の指定なし | |
dclass["NONE"] += $2; # 分類の指定なし | |
} | |
} | |
END { | |
print "出費の合計は " total " 円です"; | |
print "項目毎の出費は次の通り" | |
for (i in class) | |
if (i == "NONE") | |
print "\t分類のない項目が " class[i] " 円です"; | |
else | |
print "\t" i "に関しては " class[i] " 円です"; | |
print ""; | |
print "項目毎の出費割合は次の通り" | |
for (i in class) | |
if (i == "NONE") | |
print "\t分類のない項目が " (class[i]/total)*100 " %です"; | |
else | |
print "\t" i "に関しては " (class[i]/total)*100 " %です"; | |
print ""; | |
print "今日の出費は " dtotal " 円です"; | |
print "項目毎の出費は次の通り" | |
for (i in dclass) | |
if (i == "NONE") | |
print "\t分類のない項目が " dclass[i] " 円です"; | |
else | |
print "\t" i "に関しては " dclass[i] " 円です"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment