awkを使用してCSVファイルから2番目と3番目の列を抽出するには?

awkを使用してCSVファイルから2番目と3番目の列を抽出するには?

バッシュを使っています。以下の項目を含むCSVファイルがあります。

102110089,54d8f511cc595d120048984b,57cc73366e58b7cc330083a7
102110091,54d8f511cc595d120048984d,57cc73366e58b7cc330083a8
102110093,54d8f511cc595d120048984e,57cc73366e58b7cc330083a9

2番目と3番目の列を抽出してSQL文に入れたいです。この道は正しいと思いました...

localhost:myproject davea$ awk '{printf "update my_table_user set thirdparty_user_id='%s' where thirdparty_user_id='%s';", $(NF-2),$(NF-1)}' /tmp/Region1\ users.csv
awk: trying to access out of range field -1
 input record number 1, file /tmp/Region1 users.csv
 source line number 1

ところで、「範囲外のフィールドにアクセスしようとすると、-1」エラーが発生します。 CSVファイルから2番目と3番目の列を抽出する正しい構文は何ですか?

編集する:これは与えられた答えで起こったことです...

localhost:myproject davea$ awk -F\, '{printf "update my_table_user set thirdparty_user_id=\'%s\' where thirdparty_user_id=\'%s\'\;", $(NF-2),$(NF-1)}'
>

編集2更新された回答に応じて、私の結果は次のとおりです。 「更新」という単語が切り捨てられていることに注意してください。

localhost:myproject davea$ awk -F, '{printf "update my_table_user set thirdparty_user_id='\''%s'\'' where thirdparty_user_id='\''%s'\'';\n", $1,$3}' /tmp/myfile.csv
';date my_table_user set thirdparty_user_id='102110089' where thirdparty_user_id='57cc73366e58b7cc330083a7
';date my_table_user set thirdparty_user_id='102110091' where thirdparty_user_id='57cc73366e58b7cc330083a8
';date my_table_user set thirdparty_user_id='102110093' where thirdparty_user_id='57cc73366e58b7cc330083a9
';date my_table_user set thirdparty_user_id='102110107' where thirdparty_user_id='57cc73366e58b7cc330083b3

ベストアンサー1

awk区切り文字が何であるかを知る必要があります,。したがって、次のようにコマンドを実行する必要があります。

awk -F\, '{printf "update my_table_user set thirdparty_user_id=\'%s\' where thirdparty_user_id=\'%s\'\;", $(NF-1),$(NF)}' /tmp/Region1\ users.csv

また、入力ファイルの形式が一貫している場合(3つのフィールド、最初と2番目のフィールドをインポート)、次のものを使用できます$1$2

おすすめ記事