JSONからブロックを削除するawkスクリプト

JSONからブロックを削除するawkスクリプト

次のエントリを含む改行で区切られたJSONファイルがあります。

{"id":"eprints.ulster.ac.uk/view/year/2015.html","title":"Items where Year is 2015 - Ulster Institutional Repository","url":"eprints.ulster.ac.uk/view/year/2015.html"}
{"id":"eprints.ulster.ac.uk/view/year/2016.html","title":"Items where Year is 2016 - Ulster Institutional Repository","url":"eprints.ulster.ac.uk/view/year/2016.html"}
{"id":"eprints.ulster.ac.uk/view/year/2017.html","title":"Items where Year is 2017 - Ulster Institutional Repository","url":"eprints.ulster.ac.uk/view/year/2017.html"}
{"id":"eprints.ulster.ac.uk/10386/","title":"Structural performance of rotationally restrained steel columns in fire - Ulster Institutional Repos","url":"eprints.ulster.ac.uk/10386/"}
{"id":"eprints.ulster.ac.uk/10387/","title":"Determining the Effective Length of Fixed End Steel Columns in Fire - Ulster Institutional Repositor","url":"eprints.ulster.ac.uk/10387/"}

.id私は次から始まらないブロックだけが欲しいです。"eprints.ulster.ac.uk/view/"

したがって、上記のコードスニペットでスクリプトが実行されると、最初の3つのブロックが削除され、残りのブロックは次のようになります。

{"id":"eprints.ulster.ac.uk/10386/","title":"Structural performance of rotationally restrained steel columns in fire - Ulster Institutional Repos","url":"eprints.ulster.ac.uk/10386/"}
{"id":"eprints.ulster.ac.uk/10387/","title":"Determining the Effective Length of Fixed End Steel Columns in Fire - Ulster Institutional Repositor","url":"eprints.ulster.ac.uk/10387/"}

誰かがawkこれを行うスクリプトを書くのに役立ちますか?

答え1

Awkソリューションを具体的に要求した場合:

awk -F\" '$4 !~ /eprints.ulster.ac.uk\/view/' file > newfile

答え2

ソリューションjq

cat test.json| jq 'select(.id|startswith("eprints.ulster.ac.uk/view/")|not )'

パイプに慣れている場合、構文は非常に簡単です。

例えば

.id|startswith("eprints.ulster.ac.uk/view/")|not

各オブジェクトのフィールドを取得して.idパイプを通過することを意味しますstartswith。これはブール値を返し、ブール値は無効になります。

見て手動jqより多くの演算子とセレクタを表示するには、

答え3

興味のある方のために、次のコマンドを使用してRaphaelのソリューションを新しいJSONファイルに出力します。

cat uir-index.json| jq 'select(.id|startswith("eprints.ulster.ac.uk/view/")|not )' > cleaned-uir-index.json

出力形式は複数行のコードブロックに戻ります。次のように、「--compact-output / -c」オプションを使用して同じjqコマンドを実行しました。

cat uir-index.json| jq -c 'select(.id|startswith("eprints.ulster.ac.uk/view/")|not )' > cleaned-uir-index.json

これにより、クリーンアップされたファイルが改行形式で出力されます。

関連情報