3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

複数条件で除外 rubyワンライナー

Posted at

テキストファイルから,○○○を含み×××を含む行を除外したかった.

できるだけ後から見てもすっきりわかる方法をと思ってたどり着いた方法.

AAAを含む行 が BBBを含んでいなければ除外したい
このファイルから特定の行を除外したい

$ cat oneline.txt 
AAA BBB
AAA
BBB
CCC
AAA CCC
BBB CCC

AAA があって BBBを含まなければ除外

$ ruby -ne 'print $_ unless  (/AAA/ =~ $_) &&  not(/BBB/ =~ $_) ' oneline.txt 
AAA BBB
BBB
CCC
BBB CCC

if にすれば,除外した行が表示される

$ ruby -ne 'print $_ if  (/AAA/ =~ $_) &&  not(/BBB/ =~ $_) ' oneline.txt 
AAA
AAA CCC
3
4
9

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?