Exemplo n.º 1
0
#%% [markdown]
# ## 14. 先頭からN行を出力
# 自然数Nをコマンドライン引数などの手段で受け取り,入力のうち先頭のN行だけを表示せよ.確認にはheadコマンドを用いよ.
# ### 方針
# * `sys.argv`で引数を受け取る
# * `str.isdecimal()`で数字かどうか判定できる(jupyter notebook用)
# * それをhightempに適用

#%%
import common02

n = common02.get_argv_n()
hightemp = common02.get_hightemp()
print(''.join(hightemp[0:n]))

#%%
unix = common02.unix_run(f'head -n {n} hightemp.txt')
print(unix)
Exemplo n.º 2
0
#%% [markdown]
# # 第2章 UNIXコマンドの基礎
# hightemp.txtは,日本の最高気温の記録を「都道府県」「地点」「℃」「日」のタブ区切り形式で格納したファイルである.以下の処理を行うプログラムを作成し,hightemp.txtを入力ファイルとして実行せよ.さらに,同様の処理をUNIXコマンドでも実行し,プログラムの実行結果を確認せよ.
# ## 10. 行数のカウント
# 行数をカウントせよ.確認にはwcコマンドを用いよ.
# ### 方針
# * データをリストに格納してリストの要素数をカウントする

#%%
import common02
hightemp = common02.get_hightemp()

#%%
res = len(hightemp)
print(res)

#%%
unix = common02.unix_run('wc -l hightemp.txt')
print(unix)

#%%
Exemplo n.º 3
0
#%% [markdown]
# ## 19. 各行の1コラム目の文字列の出現頻度を求め,出現頻度の高い順に並べる
# 各行の1列目の文字列の出現頻度を求め,その高い順に並べて表示せよ.確認にはcut, uniq, sortコマンドを用いよ.
# ### 方針
# * `conut()`

#%%
import common02
hightemp = common02.get_hightemp()
col1 = [word.split("\t")[0] for word in hightemp]
word_list = set(col1)
res = [(word, col1.count(word)) for word in word_list]
res.sort(key=lambda tmp: tmp[1], reverse=True)
res
#%%
# cut -c 1-4 hightemp.txt | sort | uniq -c | sort -r
cut = common02.unix_run('cut -c 1-4 hightemp.txt')
with open('tmp.txt', mode='w') as f:
    f.write(cut)

sort = common02.unix_run(f'sort tmp.txt')
with open('tmp.txt', mode='w') as f:
    f.write(sort)

uniq = common02.unix_run('uniq -c tmp.txt')
with open('tmp.txt', mode='w') as f:
    f.write(uniq)

unix = common02.unix_run('sort -r tmp.txt')
print(unix)
Exemplo n.º 4
0
#%% [markdown]
# ## 18. 各行を3コラム目の数値の降順にソート
# 各行を3コラム目の数値の逆順で整列せよ(注意: 各行の内容は変更せずに並び替えよ).確認にはsortコマンドを用いよ(この問題はコマンドで実行した時の結果と合わなくてもよい).
# ### 方針
# * `list.sort()`
# * キーに`split("\t")[2]`(3カラム目)を指定

#%%
import common02

hightemp = common02.get_hightemp()
hightemp.sort(reverse=True, key=lambda temp: temp.split("\t")[2])
print(''.join(hightemp))

#%%
unix = common02.unix_run('sort -k 3 -r hightemp.txt')
print(unix)
Exemplo n.º 5
0
#%% [markdown]
# ## 13. col1.txtとcol2.txtをマージ
# 12で作ったcol1.txtとcol2.txtを結合し,元のファイルの1列目と2列目をタブ区切りで並べたテキストファイルを作成せよ.確認にはpasteコマンドを用いよ.
# ### 方針
# * `zip`でタプルを作って結合

#%%
with open('col1.txt') as f:
    col1 = f.read().split("\n")
with open('col2.txt') as f:
    col2 = f.read().split("\n")

res = ["\t".join(words) for words in list(zip(col1, col2))]
print("\n".join(res))

#%%
import common02
unix = common02.unix_run('paste col1.txt col2.txt')
print(unix)
Exemplo n.º 6
0
#%% [markdown]
# ## 12. 1列目をcol1.txtに,2列目をcol2.txtに保存
# 各行の1列目だけを抜き出したものをcol1.txtに,2列目だけを抜き出したものをcol2.txtとしてファイルに保存せよ.確認にはcutコマンドを用いよ.
# ### 方針
# * タブで`split`して1番目のデータをcol1に、2番目のデータをcol2に

#%%
import common02

hightemp = common02.get_hightemp()
col1 = [word.split("\t")[0] for word in hightemp]
col2 = [word.split("\t")[1] for word in hightemp]
with open('col1.txt', mode='w') as f:
    f.write('\n'.join(col1))
with open('col2.txt', mode='w') as f:
    f.write('\n'.join(col2))

#%%
unix1 = common02.unix_run('cat col1.txt')
unix2 = common02.unix_run('cat col2.txt')
print(unix1)
print(unix2)
Exemplo n.º 7
0
#%% [markdown]
# ## 16. ファイルをN分割する
# 自然数Nをコマンドライン引数などの手段で受け取り,入力のファイルを行単位でN分割せよ.同様の処理をsplitコマンドで実現せよ.
# ### 方針
# * 行数を取ってきてNで割り、結果の行数分ファイルを作成する

#%%
import common02
hightemp = common02.get_hightemp()
n = len(hightemp) // common02.get_argv_n() + 1
for i in range(n):
    res = hightemp[n * i:n * (i + 1)]
    with open(chr(97 + i), mode='w') as f:
        f.write(''.join(res))

#%%
common02.unix_run('split -l 5 hightemp.txt')
Exemplo n.º 8
0
#%% [markdown]
# ## 15. 末尾のN行を出力
# 自然数Nをコマンドライン引数などの手段で受け取り,入力のうち末尾のN行だけを表示せよ.確認にはtailコマンドを用いよ.
# ### 方針
# * 14の逆

#%%
import common02

n = common02.get_argv_n()
hightemp = common02.get_hightemp()
print(''.join(hightemp[len(hightemp) - n:]))

#%%
unix = common02.unix_run(f'tail -n {n} hightemp.txt')
print(unix)
Exemplo n.º 9
0
#%% [markdown]
# ## 11. タブをスペースに置換
# タブ1文字につきスペース1文字に置換せよ.確認にはsedコマンド,trコマンド,もしくはexpandコマンドを用いよ.
# ### 方針
# * `str.replace`

#%%
import common02
hightemp = common02.get_hightemp()
res = [word.replace("\t", " ") for word in hightemp]
print(''.join(res))

#%%
unix = common02.unix_run('sed s/\t/\x20/g hightemp.txt')
print(unix)
Exemplo n.º 10
0
#%% [markdown]
# ## 17. 1列目の文字列の異なり
# 1列目の文字列の種類(異なる文字列の集合)を求めよ.確認にはsort, uniqコマンドを用いよ.
# ### 方針
# * 1列目の`list`をそのまま`set`に変換する

#%%
import common02
hightemp = common02.get_hightemp()
col = [word.split("\t")[0] for word in hightemp]
print(set(col))

#%%
tmp = common02.unix_run('sort col1.txt')
with open('tmp.txt', mode='w') as f:
  f.write(tmp)
res = common02.unix_run('uniq tmp.txt')
print(res)