別の文字が一致するまで、1文字の行を削除したいと思います。

別の文字が一致するまで、1文字の行を削除したいと思います。

次の内容を含むファイルがあります。

some other content in this file
    demo({
     'item1': { 'buy': 'bananna', 'drink': x'cooldrink',
                'work': 'hard', 
                'study': 'science' },
     'item2': { 'buy': 'apple', 'drink': x'cola',
                'work': 'slow', 
                'eat': 'sweet',
                'travel': 'world' },
     'item3': { 'buy': 'bananna', 
                'drink': x'cooldrink',
                'work': 'hard', 
                'study': 'science'}})
Some other content in this file.

今、item2を検索して次を削除したいと思います。

'item2': { 'buy': 'apple', 'drink': x'cola',
                'work': 'slow', 
                'eat': 'sweet',
                'travel': 'world' },

現在私はawk '!/'item2'/' filename。ただし、これにより、角かっこがコンマで閉じられるまで、特定の行のみが削除されます。を使ってみましたが、 までpcregrep -Mo 'item2' filename設定できません。{},

また、削除する必要がある場合は、末尾item3にカンマがない特別な場合があります,

どうすべきかを提案してください。

ベストアンサー1

$ perl -0777 -pe '$s="item1"; s/ *\x27$s\x27:\s*\{.*?\},\n*//s ; s/,\s*\x27$s\x27:\s*\{.*?\}(?!,)//s' ip.txt 
some other content in this file
    demo({
     'item2': { 'buy': 'apple', 'drink': x'cola',
                'work': 'slow', 
                'eat': 'sweet',
                'travel': 'world' },
     'item3': { 'buy': 'bananna', 
                'drink': x'cooldrink',
                'work': 'hard', 
                'study': 'science'}})
Some other content in this file.

$ perl -0777 -pe '$s="item3"; s/ *\x27$s\x27:\s*\{.*?\},\n*//s ; s/,\s*\x27$s\x27:\s*\{.*?\}(?!,)//s' ip.txt 
some other content in this file
    demo({
     'item1': { 'buy': 'bananna', 'drink': x'cooldrink',
                'work': 'hard', 
                'study': 'science' },
     'item2': { 'buy': 'apple', 'drink': x'cola',
                'work': 'slow', 
                'eat': 'sweet',
                'travel': 'world' }})
Some other content in this file.

$ perl -0777 -pe '$s="item2"; s/ *\x27$s\x27:\s*\{.*?\},\n*//s ; s/,\s*\x27$s\x27:\s*\{.*?\}(?!,)//s' ip.txt 
some other content in this file
    demo({
     'item1': { 'buy': 'bananna', 'drink': x'cooldrink',
                'work': 'hard', 
                'study': 'science' },
     'item3': { 'buy': 'bananna', 
                'drink': x'cooldrink',
                'work': 'hard', 
                'study': 'science'}})
Some other content in this file.
  • -0777ファイル全体を読み取り、複数行にわたって一致するようにマークするsために使用されます。.*
  • s/ *\x27$s\x27:\s*\{.*?\},\n*//s次のようなitem1要素の場合item2,}
  • s/,\s*\x27$s\x27:\s*\{.*?\}(?!,)//s私の考えでは、同じ要素の場合、前の行から要素をitem3削除する必要があるようです。,
  • \x27一重引用符の16進コード
  • $s="item2";削除したい要素に変更してください。


編集する:

シェルから変数を渡す(参照SOに関する質問と回答詳細)

$ export var1='item3'
$ perl -0777 -pe '$s=$ENV{var1}; s/ *\x27$s\x27:\s*\{.*?\},\n*//s ; s/,\s*\x27$s\x27:\s*\{.*?\}(?!,)//s' ip.txt 
some other content in this file
    demo({
     'item1': { 'buy': 'bananna', 'drink': x'cooldrink',
                'work': 'hard', 
                'study': 'science' },
     'item2': { 'buy': 'apple', 'drink': x'cola',
                'work': 'slow', 
                'eat': 'sweet',
                'travel': 'world' }})
Some other content in this file.

複数のアイテムを削除することもできます

$ export var1='(item3|item2)'
$ perl -0777 -pe '$s=$ENV{var1}; s/ *\x27$s\x27:\s*\{.*?\},\n*//s ; s/,\s*\x27$s\x27:\s*\{.*?\}(?!,)//s' ip.txt 
some other content in this file
    demo({
     'item1': { 'buy': 'bananna', 'drink': x'cooldrink',
                'work': 'hard', 
                'study': 'science' }})
Some other content in this file.

おすすめ記事