Skip to content

Instantly share code, notes, and snippets.

@kanjirz50
Last active Jul 28, 2017
Embed
What would you like to do?
NLP100本ノック2章のシェル
# -*- coding:utf-8 -*-
def main():
print(chap10())
print(chap11())
chap12()
chap13()
def chap10():
with open("./hightemp.txt", "r") as fin:
return len([line for line in fin])
def chap11():
with open("./hightemp.txt", "r") as fin:
replaced = (line.replace("\t", " ").rstrip() for line in fin)
return "\n".join(replaced)
def chap12():
with open("./hightemp.txt", "r") as fin, open("./col1.txt", "w") as fout1, open("./col2.txt", "\w") as fout2:
for line in fin:
elems = line.rstrip().split()
col1 = elems[0]
col2 = elems[1]
fout1.write(col1 + "\n")
fout2.write(col2 + "\n")
def chap13():
with open("./col1.txt", "r") as fin1, open("./col2.txt", "r") as fin2:
for col1, col2 in zip(fin1, fin2):
print(col1.rstrip(), col2.rstrip(), sep="\t")
if __name__ == "__main__":
main()
echo "10.行数のカウント"
wc -l hightemp.txt
echo "11.タブをスペース置換"
cat hightemp.txt | tr "\t" " "
echo "12.1列目をcol1.txtに,2列目をcol2.txtに保存"
cat hightemp.txt | cut -f1 > col1.txt && cat hightemp.txt | cut -f2 > col2.txt
echo "13.col1.txtとcol2.txtをマージ"
paste col1.txt col2.txt
echo "14. 先頭からN行を出力"
cat hightemp.txt|head -3
echo "15. 末尾のN行を出力"
cat hightemp.txt|tail -3
echo "16. ファイルをN分割する"
split hightemp.txt -n 3
echo "17. 1列目の文字列の異なり"
cat hightemp.txt | cut -f1 | sort -u
echo "18. 各行を3コラム目の数値の降順にソート"
cat hightemp.txt | sort -nrk3
echo "19. 各行の1コラム目の文字列の出現頻度を求め,出現頻度の高い順に並べる"
cat hightemp.txt|cut -f1|sort|uniq -c|sort -nrk1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment