|
テキストファイルを操作する 〜sort,dd,tr,cut,paste,sed〜
テキストファイルを操作する
〜sort,dd,tr,cut,paste,sed〜
0.改訂履歴
1.はじめに
このドキュメントでは,テキストファイルを操作するコマンドの使い方を説明する.
なお,使用している環境はMacOS Xだが,Linuxでも可能と思われる.(というかLinuxの本を見ながらテストしてみたし)
2.sortを使う
[help-mob:~] shinnai% cat test.csv
4,AA ,1000
3,BB ,2000
2,AA ,3000
1,BB ,4000
[help-mob:~] shinnai%
|
[help-mob:~] shinnai% sort test.csv
1,BB ,4000
2,AA ,3000
3,BB ,2000
4,AA ,1000
[help-mob:~] shinnai%
|
[help-mob:~] shinnai% sort -r test.csv
4,AA ,1000
3,BB ,2000
2,AA ,3000
1,BB ,4000
[help-mob:~] shinnai%
|
- カンマ区切りファイルだと指定し,2つ目の列でソートする.
[help-mob:~] shinnai% sort -t , -k 2 test.csv
4,AA ,1000
2,AA ,3000
3,BB ,2000
1,BB ,4000
[help-mob:~] shinnai%
|
3.dd(convert and copt a file)を使って大文字小文字変換してみる
[localhost:~] shinnai% cat test.csv
4,AA ,2000
3,BB ,1000
2,AA ,3000
1,BB ,1000
[localhost:~]
|
[localhost:~] shinnai% dd if=test.csv of=TEST1.csv conv=lcase
0+1 records in
0+1 records out
60 bytes transferred in 1 secs (60 bytes/sec)
[localhost:~] shinnai% cat TEST1.csv
4,aa ,2000
3,bb ,1000
2,aa ,3000
1,bb ,1000
[localhost:~] shinnai%
|
[localhost:~] shinnai% dd if=TEST1.csv of=TEST2.csv conv=ucase
0+1 records in
0+1 records out
60 bytes transferred in 1 secs (60 bytes/sec)
[localhost:~] shinnai% cat TEST2.csv
4,AA ,2000
3,BB ,1000
2,AA ,3000
1,BB ,1000
[localhost:~] shinnai%
|
4.tr(trancelate charactoers)でスペースを置き換える
[localhost:~] shinnai% cat test.csv
4,AA ,2000
3,BB ,1000
2,AA ,3000
1,BB ,1000
[localhost:~] shinnai%
|
[localhost:~] shinnai% cat test.csv | tr "[:blank:]" +
4,AA+++++,2000
3,BB+++++,1000
2,AA+++++,3000
1,BB+++++,1000
[localhost:~] shinnai%
|
- -d(elete)を使って,スペースを除去してみる.
[localhost:~] shinnai% cat test.csv | tr -d "[:blank:]"
4,AA,2000
3,BB,1000
2,AA,3000
1,BB,1000
[localhost:~] shinnai%
|
- Linuxの場合だと,"[:blank:]"じゃなくて[:blank:]だとおもう.
5.cutを使って特定の列を取り出す
[localhost:~] shinnai% cat test.csv
4,AA ,2000
3,BB ,1000
2,AA ,3000
1,BB ,1000
[localhost:~] shinnai%
|
- カンマ区切りのファイルから,2番目の列を取り出す.
[localhost:~] shinnai% cut -f 2 -d , test.csv
AA
BB
AA
BB
[localhost:~] shinnai%
|
[localhost:~] shinnai% cut -f 2 -d , test.csv | wc -c
32
[localhost:~] shinnai%
|
- 見えてないけど,ブランクも入っている.
- cutとtrを応用してみる.
[localhost:~] shinnai% cut -f 2 -d , test.csv | tr -d "[:blank:]"
AA
BB
AA
BB
[localhost:~] shinnai% cut -f 2 -d , test.csv | tr -d "[:blank:]" | wc -c
12
[localhost:~] shinnai%
|
6.pasteを使って列を組み替える
[localhost:~] shinnai% cat test.csv
4,AA ,2000
3,BB ,1000
2,AA ,3000
1,BB ,1000
[localhost:~] shinnai%
|
- カンマ区切りのこのファイルから,3つの列を,それぞれfile1.txt〜file3.txtとして取り出す.
- このとき,2つ目の列のスペースは除去する.
[localhost:~] shinnai% cut -f 1 -d , test.csv > file1.txt
[localhost:~] shinnai% cut -f 2 -d , test.csv | tr -d "[:blank:]" > file2.txt
[localhost:~] shinnai% cut -f 1 -d , test.csv > file3.txt
|
[localhost:~] shinnai% cat file1.txt
4
3
2
1
[localhost:~] shinnai% cat file2.txt
AA
BB
AA
BB
[localhost:~] shinnai% cat file3.txt
2000
1000
3000
1000
[localhost:~] shinnai%
|
- 2つめの列を取り出す.
- このとき,ブランクは消去する.
- 取り出した列を,カンマ区切りファイルとして,列2,列3,列1の順につなげる.
[localhost:~] shinnai% paste -d , file2.txt file3.txt file1.txt
AA,2000,4
BB,1000,3
AA,3000,2
BB,1000,1
[localhost:~] shinnai%
|
7.sedを使って行頭,行末にシングルクオートを入れる
- こいつは,Macだとうまくいかなかった.(行末処理が)
- 対象データを確認する.
[localhost:~] shinnai% cat file2.txt
AA
BB
AA
BB
[localhost:~] shinnai%
|
- 行頭(^)にシングルクォートを入れてfile2a.txtに書き出す.
[localhost:~] shinnai% sed -e "s/^/'/g" file2.txt > file2a.txt
[localhost:~] shinnai% cat file2a.txt
'AA
'BB
'CC
[localhost:~] shinnai%
|
- fil2a.txtを元に,行末にシングルクォートを入れる.
[localhost:~] shinnai% sed -e "s/$/'/g" file2a.txt > file2b.txt
[localhost:~] shinnai% cat file2b.txt
'AA'
'BB'
'CC'
[localhost:~] shinnai%
|
|
|