sed

sed

私は持っていますfilename.json。端末で解析すると

file filename.json

出力は次のとおりです

filename.json: UTF-8 Unicode text, with very long lines  

wc -l filename.json    
1 filename.json

これをjson使用して解析する場合は、jqID、要約、作成者など、印刷したいデータの部分に言及する必要があります。同様の構造を持つ何千ものjsonがありますが、データを「要約」、「説明」、「レビュー」などとして保存したいと思います。私は何千ものJSONファイルを持っているので、一つ一つ確認したくありません。しかし、私が望むデータが2つのスキーマの間にあることを知っています。

「タイトル」:そして 「URL」:

$ cat filename.json

以下を提供します。

{"source":"PhoneArena","author":"","title":"Apple's US Black Friday shopping event has gift cards galore for select iPhones, iPads, and more","description":"As confirmed earlier this week, a four-day Black Friday and Cyber Monday shopping event is underway, offering Apple Store gift cards with purchases of select iPhone models, three iPad variants, an assortment of Macs, the entire Apple Watch Series 3 family, as well as the HomePod, both Apple TV versions, and select Beats headphones.That ...","url":"https://www.phonearena.com/news/Apples-US-Black-Friday-shopping-event-has-gift-cards-galore-for-select-iPhones-iPads-and-more_id111287","urlToImage":"https://i-cdn.phonearena.com/images/article/111287-two_lead/Apples-US-Black-Friday-shopping-event-has-gift-cards-galore-for-select-iPhones-iPads-and-more.jpg","publishedAt":"2018-11-23T09:05:00Z","dataRefreshedTime":"2018-11-23T09:43:09Z","category":"phone_news_reviews","resource":"PhoneArena"},{"source":"PhoneArena","author":"","title":"Verizon's top Black Friday bargain is a free Moto G6, no trade-in required","description":"That made it virtually impossible for retailers like Best Buy and B&H Photo Video to outdo themselves come the actual Black Friday frenzy, but luckily, that’s what carriers are (sometimes) good for.Enter Verizon, which revealed a wide range of killer deals on popular high-end ...","url":"https://www.phonearena.com/news/Verizons-top-Black-Friday-bargain-is-a-free-Moto-G6-no-trade-in-required_id111285","urlToImage":"https://i-cdn.phonearena.com/images/article/111285-two_lead/Verizons-top-Black-Friday-bargain-is-a-free-Moto-G6-no-trade-in-required.jpg","publishedAt":"2018-11-23T07:54:00Z","dataRefreshedTime":"2018-11-23T09:43:09Z","category":"phone_news_reviews","resource":"PhoneArena"},

したがって、パターン間のすべてを印刷したいのですが、端末にはファイルが1行だけあり、パターンが複数回表示されます。私が考える唯一の方法は、2つのパターンの間でファイルの終わりまで印刷することです。

私はsedを試してみます:

sed -n '^/title/,/^url/p' filename.json

しかし、空白で印刷されます。

機械学習技術を使用して言語分析のためのデータをさらに入力したいと思います。

パターン間を印刷し、パターンを複数回繰り返す他の方法に関する提案です。したがって、各反復の間にデータを印刷したいと思います。

予想される結果は、CSVまたはTSVで印刷することです。

1 "As confirmed earlier this week, a four-day Black Friday and Cyber Monday shopping event is underway, offering Apple Store gift cards with purchases of select iPhone models, three iPad variants, an assortment of Macs, the entire Apple Watch Series 3 family, as well as the HomePod, both Apple TV versions, and select Beats headphones.That ..."

2 "That made it virtually impossible for retailers like Best Buy and B&H Photo Video to outdo themselves come the actual Black Friday frenzy, but luckily, that’s what carriers are (sometimes) good for.Enter Verizon, which revealed a wide range of killer deals on popular high-end ..."

etc,.

ファイルの最後まで。

ベストアンサー1

長い話を短く

ksh、bash、zshから:

sed -e $'s,"title":,\1,g' -e $'s,"url":,\2,g' -e $'s,^[^\1]*,,' -e $'
         s,\1\\([^\2]*\\)\2[^\1]*,\\1\\\n,g' infile

sed

文字区切り記号。

標準溶液キャラクター@区切り文字は次のとおりです#

sed 's,^[^@]*,,;s,@\([^#]*\)#[^@]*,\1 ,g' infile

これにより、先頭から以外のすべての文字が削除されます。@ - 間にある文字が抽出されます。最初 @ 次に最初 #次へ。

それぞれワイヤー入力ファイルinfile

汎用区切り記号。

各区切り文字列を上記の答えに簡単に変換すると、別の区切り文字を変換できます。一つ特徴。

sed -e 's,"title":,@,g' -e 's,"url":,#,g' -e 's/^[^@]*//;s/@\([^#]*\)#[^@]*/\1 /g' infile

あなたの場合は、スペース()の代わりに改行文字を使用できます。これはGNU sed()\1用に書くときに簡単です。\1\n

sed -e 's,"title":,@,g' -e 's,"url":,#,g' -e 's/^[^@]*//;s/@\([^#]*\)#[^@]*/\1\n/g' infile

他の(以前の)sedの場合は、明示的な改行を追加します。

sed -e 's,"title":,@,g' -e 's,"url":,#,g' -e 's/^[^@]*//;s/@\([^#]*\)#[^@]*/\1\
/g' infile

上記で使用した区切り文字がファイル内に存在する危険がある場合は、ファイル内に存在しない別の区切り文字を選択してください。これが問題になる場合、始まりと終わりの区切り文字はCtrl- A(またはエンコーディング:^A、 hex:、Ox01または8進数\001)などの制御文字にすることができます。Ctrl- - と入力してV Ctrlシェルコンソールに入力できますA。コマンドラインに^ Aが表示されます。

sed -e 's,"title":,^A,g' -e 's,"url":,^B,g' -e 's,^[^^A]*,,;s,^A\([^^B]*\)^B[^^A]*,\1\n,g' infile

あるいは、入力が難しい場合(ksh、bash、zsh)を使用できます。

sed -e $'s,"title":,\1,g' -e $'s,"url":,\2,g' -e $'s,^[^\1]*,,' -e $'s,\1\\([^\2]*\\)\2[^\1]*,\\1\\\n,g' infile

またはあなたのsedがそれをサポートしている場合:

sed -e 's,"title":,\o001,g' -e 's,"url":,\o002,g' -e 's,^[^\o001]*,,' -e 's,\o001\([^\o002]*\)\o002[^\o001]*,\1\o012,g' infile

区切り文字が「説明」の場合:

"description":開始タグが実際に(出力例で)ある場合は、代わりに使用してください。"title":

上記の出力(質問で以前に接続したファイルから):

"Black Friday deal: Palm companion phone is $150 off at Verizon, but there's a catch","description":"",
"LG trademarks potential names for its foldable phone, one fits a crazy concept found in patents","description":"",
"Blackview's Black Friday promo discounts the BV9500 Pro and other rugged phones on Amazon","description":"Advertorial by Blackview: the opinions expressed in this story may not reflect the positions of PhoneArena! disclaimer   amzn_assoc_tracking_id = 'phone0e0d-20';amzn_assoc_ad_mode = 'manual';amzn_assoc_ad_type ...",

行番号を付ける必要がある場合は、sedを再度使用してくださいsed -n '=;p;g;p'

| sed -n '=;p;g;p'
1
"Black Friday deal: Palm companion phone is $150 off at Verizon, but there's a catch","description":"",

2
"LG trademarks potential names for its foldable phone, one fits a crazy concept found in patents","description":"",

3
"Blackview's Black Friday promo discounts the BV9500 Pro and other rugged phones on Amazon","description":"Advertorial by Blackview: the opinions expressed in this story may not reflect the positions of PhoneArena! disclaimer   amzn_assoc_tracking_id = 'phone0e0d-20';amzn_assoc_ad_mode = 'manual';amzn_assoc_ad_type ...",

AWK

awkに実装された同様のロジック:

awk -vone=$'\1' -vtwo=$'\2' '{
            gsub(/"title":/,one);
            gsub(/"url":/,two);
            sub("^[^"one"]*"one,"")
            gsub(two"[^"one"]*"one,ORS)
            sub(two"[^"two"]*$","")
           } 1' infile

おすすめ記事