データのクリーンアップにシェルスクリプトを使用する

データのクリーンアップにシェルスクリプトを使用する

lang = enこのファイルをJSONからCSVに変換するときは、言語が英語の行を選択して他の行を削​​除するのに適した結合クエリが必要です。

json2csv -i Downloads/30.json -f id,text,lang -o Downloads/30.csv

30.json

{"created_at":"Sun Apr 01 09:00:00 +0000 2018","id":980369176291262464,"id_str":"980369176291262464","text":"RT @Q_cupid: \u0e01\u0e23\u0e30\u0e41\u0e2a\u0e43\u0e2a\u0e48\u0e0a\u0e38\u0e14\u0e44\u0e17\u0e22\u0e21\u0e32\u0e41\u0e23\u0e07\u0e2d\u0e31\u0e19\u0e19\u0e35\u0e49\u0e40\u0e02\u0e49\u0e32\u0e43\u0e08 \u0e41\u0e15\u0e48\u0e17\u0e35\u0e48\u0e44\u0e21\u0e48\u0e40\u0e02\u0e49\u0e32\u0e43\u0e08\u0e04\u0e37\u0e2d\u0e04\u0e19\u0e17\u0e35\u0e48\u0e01\u0e25\u0e49\u0e32\u0e44\u0e1b\u0e02\u0e42\u0e21\u0e22\u0e0a\u0e38\u0e14\u0e44\u0e17\u0e22\u0e17\u0e35\u0e48\u0e40\u0e04\u0e49\u0e32\u0e40\u0e2d\u0e32\u0e44\u0e27\u0e49\u0e44\u0e1b\u0e41\u0e01\u0e49\u0e1a\u0e19\u0e15\u0e32\u0e21\u0e28\u0e32\u0e25\u0e19\u0e35\u0e48\u0e41\u0e2b\u0e25\u0e30 \u0e2d\u0e35\u0e40\u0e27\u0e07\u0e07 \u0e02\u0e42\u0e21\u0e22\u0e44\u0e1b\u0e40\u0e01\u0e37\u0e2d\u0e1a100\u2026","source":"\u003ca href=\"http:\/\/twitter.com\/download\/android\" rel=\"nofollow\"\u003eTwitter for Android\u003c\/a\u003e","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":782859283733983233,"id_str":"782859283733983233","name":"mystar\ud83c\udf1fjaemin\ud83c\udf1f","screen_name":"mljm1920","location":null,"url":null,"description":"\u0e40\u0e1b\u0e47\u0e19\u0e21\u0e35\u0e4a\u0e17\u0e35\u0e48\u0e23\u0e31\u0e01\u0e19\u0e49\u0e2d\u0e07\u0e41\u0e08\u0e21\u0e21\u0e32\u0e01\u0e17\u0e35\u0e48\u0e2a\u0e38\u0e14\ud83d\ude18\ud83d\ude18 #JAEMIN  \ud83c\udf51 #EXO #NCTDREAM #NCT \u0e0a\u0e34\u0e1b\u0e40\u0e1b\u0e2d\u0e23\u0e4c #markmin \u0e40\u0e1b\u0e47\u0e19\u0e2b\u0e25\u0e31\u0e01 #HunHan #NoRen #ChanLe #SungLe","translator_type":"none","protected":false,"verified":false,"followers_count":205,"friends_count":2155,"listed_count":0,"favourites_count":17155,"statuses_count":9823,"created_at":"Mon Oct 03 08:26:12 +0000 2016","utc_offset":null,"time_zone":null,"geo_enabled":false,"lang":"th","contributors_enabled":false,"is_translator":false,"profile_background_color":"F5F8FA","profile_background_image_url":"","profile_background_image_url_https":"","profile_background_tile":false,"profile_link_color":"1DA1F2","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/980103235007492096\/Xx-SpFsJ_normal.jpg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/980103235007492096\/Xx-SpFsJ_normal.jpg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/782859283733983233\/1521042399","default_profile":true,"default_profile_image":false,"following":null,"follow_request_sent":null,"notifications":null},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweeted_status":

希望の出力:30.csv

出力には、lang列の値= enの行のみが必要です。

ベストアンサー1

そしてjqhttps://stedolan.github.io/jq/) 次のようにできます。

jq -r '{ id, text, lang: .user.lang } | select (.lang == "en") | [ .id, .text, .lang ] | @csv' 30.json

これにより、希望の出力が得られます。

980369176291262500,"RT @Q_cupid: กระแสใส่ชุดไทยมาแรงอันนี้เข้าใจ แต่ที่ไม่เข้าใจคือคนที่กล้าไปขโมยชุดไทยที่เค้าเอาไว้ไปแก้บนตามศาลนี่แหละ อีเวงง ขโมยไปเกือบ100…","en"

出力をファイルに書き込むには、リダイレクトするだけです。

jq -r '{ id, text, lang: .user.lang } | select (.lang == "en") | [ .id, .text, .lang ] | @csv' 30.json > output.csv

ご質問のサンプルJSONを有効なJSONに変更しました。ほとんどのデータを削除しているようなので、これは問題を引き起こしません。

おすすめ記事