8
7

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 5 years have passed since last update.

フィールド区切り文字の変換:可変個空白→カンマ

Last updated at Posted at 2016-01-27

可変個の空白文字でフィールドを区切る形式のテキストデータを、カンマ区切り(CSV)データに変換する。

$ awk -v IFS=' ' -v OFS=',' '{$1=$1;print $0}' src.txt > dest.csv

awkコマンドの組み込み変数IFS(入力フィールド区切り文字)とOFS(出力フィールド区切り文字)を利用。一見すると無駄なフィールド自己代入($1=$1;)により、レコード全体($0)の再構築が行われるらしい。あふれだすバッドノウハウ感。

例)vmstatコマンドの出力など

$ cat src.txt
 2  0 216376 4947412      0 25388376    0    0     0     0    0    0  0  0 100  0  0
 0  0 216376 4954320      0 25388432    0    0     0     2  353  781  0  0 100  0  0
...
$ cat dest.csv
2,0,216376,4947412,0,25388376,0,0,0,0,0,0,0,0,100,0,0
0,0,216376,4954320,0,25388432,0,0,0,2,353,781,0,0,100,0,0
...
8
7
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
8
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?