0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

空白区切りのファイルをcsvに変換 (sed, awk)

Last updated at Posted at 2023-01-24

空白(複数個)区切りのテキストファイルをcsvに変換したときのメモ。

  • 目標
sample.txt
 -0.02223544   0.36483891 -1.16350713

  1.72316747  -1.4276687   0.91534189

  0.47763427  -0.52007756  0.91546411

sample.csv
0.36483891,-1.16350713
-1.4276687,0.91534189
-0.52007756,0.91546411

に変換する

  • コマンド
    複数個の空白を,に置換した後、先頭の,を消し、空行を消し、2列目と3列目を取り出す。

GNU版のsedの場合:

sed -e 's/ \+/,/g' -e 's/^,//g' -e '/^$/d' sample.txt | awk -F',' -v 'OFS=,' '{print $1,$2}' > sample.csv

BSD版のsed:

sed -e 's/ \{1,\}/,/g' -e 's/^,//g' -e '/^$/d' sample.txt | awk -F',' -v 'OFS=,' '{print $1,$2}' > sample.csv
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?