特定のベンダーの Web ポータルからダウンロードした.CSVファイル (tab_delimited_file.csv としましょう) があります。ファイルを Linux ディレクトリの 1 つに移動したとき、この特定の.CSVファイルが実際には.CSVという名前のタブ区切りファイルであることに気付きました。ファイルのいくつかのサンプル レコードを以下に示します。
"""column1""" """column2""" """column3""" """column4""" """column5""" """column6""" """column7"""
12 455 string with quotes, and with a comma in between 4432 6787 890 88
4432 6787 another, string with quotes, and with two comma in between 890 88 12 455
11 22 simple string 77 777 333 22
上記のサンプル レコードは で区切られていtabs
ます。ファイルのヘッダーが非常に奇妙であることは知っていますが、これがファイル形式を受け取った方法です。
コマンドを使用tr
して を置き換えようtabs
としcommas
ましたが、レコード値に余分なコンマが含まれているため、ファイルが完全にめちゃくちゃになります。コンマを含むレコード値を二重引用符で囲む必要があります。私が使用したコマンドは以下の通りです。
tr '\t' ',' < tab_delimited_file.csv > comma_separated_file.csv
これにより、ファイルが次の形式に変換されます。
"""column1""","""column2""","""column3""","""column4""","""column5""","""column6""","""column7"""
12,455,string with quotes, and with a comma in between,4432,6787,890,88
4432,6787,another, string with quotes, and with two comma in between,890,88,12,455
11,22,simple string,77,777,333,22
サンプル ファイルを以下の形式に変換するのに助けが必要です。
column1,column2,column3,column4,column5,column6,column7
12,455,"string with quotes, and with a comma in between",4432,6787,890,88
4432,6787,"another, string with quotes, and with two comma in between",890,88,12,455
11,22,"simple string",77,777,333,22
sed
またはを使用するソリューションはawk
非常に便利です。