フィールドに基づいてファイルを複数のファイルに分割する

フィールドに基づいてファイルを複数のファイルに分割する

ファイルを複数のファイルに分割するにはどうすればよいですか$2

例えば、

123,hello,world
124,hello,planet
125,universe,hello
126,hello,universe

希望の出力、

hello.txt >

123,hello,world
124,hello,planet
126,hello,universe

universe.txt >

125,universe,hello

ベストアンサー1

GNU awkを使う:

awk '{name=$2 ".txt"; print >>name; close(name)}' FS=',' file

stackoverflow.comにもよく似た質問があります。ファイルを行ごとに分割し、最初の文字列を出力ファイルのタイトルとして保持します。

おすすめ記事