Home > database >  Delete lines contening string except other string is in this line in bash
Delete lines contening string except other string is in this line in bash

Time:11-23

I want to generate a sub-schema (even if it is imperfect) by deleting all the lines that only concern the batch

I tried to make a replacement in the file with

sed -i -e '/batch/!b' -e '{/web/!d;}' $target_file

the output file should no longer have a line with 'batch' unless in addition to batch it has 'web' on the same line.

the problem is that the lines containing just web have also disappeared!

starting file:

  graph TD;
  
  classDef batch          fill:#050
  classDef web             fill:#005
  classDef batch_web       fill:#055

  classDef batch_livré    fill:#050,color:#0a0
  classDef web_livré       fill:#005,color:#0a0
  classDef batch_web_livré fill:#055,color:#0a0
  
  classDef conf_batch    stroke:#090,stroke-width:2px;
  classDef conf_web       stroke:#00f,stroke-width:2px;
  classDef conf_batch_web stroke:#099,stroke-width:2px;

  classDef batch_conf             fill:#050,stroke:#090,stroke-width:2px;
  classDef web_conf                fill:#005,stroke:#00f,stroke-width:2px;

  classDef batch_web_conf_batch    fill:#055,stroke:#090,stroke-width:2px;
  classDef batch_web_conf_web       fill:#055,stroke:#00f,stroke-width:2px;
  classDef batch_web_conf_batch_web fill:#055,stroke:#099,stroke-width:2px;
  classDef batch_web_conf          fill:#055,stroke:#099,stroke-width:2px;
  
  classDef batch_conf_livré    fill:#050,color:#0a0,stroke:#090,stroke-width:2px;
  classDef web_conf_livré       fill:#005,color:#0a0,stroke:#00f,stroke-width:2px;
  
  classDef batch_web_conf_batch_livré    fill:#055,color:#0a0,stroke:#090,stroke-width:2px;
  classDef batch_web_conf_web_livré       fill:#055,color:#0a0,stroke:#00f,stroke-width:2px;
  classDef batch_web_conf_batch_web_livré fill:#055,color:#0a0,stroke:#099,stroke-width:2px;
  classDef batch_web_conf_livré          fill:#055,color:#0a0,stroke:#099,stroke-width:2px;

  classDef livré color:#0a0;
  classDef mep color:#0a0,stroke:#090;

  subgraph Légende
    batch:::batch
    web:::web
    batch_web[batch et web]:::batch_web
    conf_batch[conf batch]:::conf_batch
    conf_web[conf web]:::conf_web
    conf_batch_web[conf batch et web]:::conf_batch_web
    livré:::livré
  end

  subgraph Jasslounge
    22.6.90.01.0:::web_conf_livré
    22.6.90.01.0===22.6.90.02.0:::batch_conf_livré
    22.6.90.02.0===22.6.90.03.0:::batch_livré
    22.6.90.03.0===22.6.90.05.0:::web_livré
    22.6.90.05.0===22.6.90.06.0:::batch_livré
    22.6.90.06.0===22.6.90.07.0:::batch_web_livré
    22.6.90.07.0===22.6.90.08.0:::web_conf_livré
    22.6.90.08.0===22.6.90.09.0:::web_conf_livré
    22.6.90.09.0===22.6.90.10.0:::batch_web_conf_web_livré
  end
  class Jasslounge mep

Desired output file:

graph TD;
  
  classDef web             fill:#005
  classDef batch_web       fill:#055

  classDef web_livré       fill:#005,color:#0a0
  classDef batch_web_livré fill:#055,color:#0a0
  
  classDef conf_web       stroke:#00f,stroke-width:2px;
  classDef conf_batch_web stroke:#099,stroke-width:2px;

  classDef web_conf                fill:#005,stroke:#00f,stroke-width:2px;

  classDef batch_web_conf_batch    fill:#055,stroke:#090,stroke-width:2px;
  classDef batch_web_conf_web       fill:#055,stroke:#00f,stroke-width:2px;
  classDef batch_web_conf_batch_web fill:#055,stroke:#099,stroke-width:2px;
  classDef batch_web_conf          fill:#055,stroke:#099,stroke-width:2px;
  
  classDef web_conf_livré       fill:#005,color:#0a0,stroke:#00f,stroke-width:2px;
  
  classDef batch_web_conf_batch_livré    fill:#055,color:#0a0,stroke:#090,stroke-width:2px;
  classDef batch_web_conf_web_livré       fill:#055,color:#0a0,stroke:#00f,stroke-width:2px;
  classDef batch_web_conf_batch_web_livré fill:#055,color:#0a0,stroke:#099,stroke-width:2px;
  classDef batch_web_conf_livré          fill:#055,color:#0a0,stroke:#099,stroke-width:2px;

  classDef livré color:#0a0;
  classDef mep color:#0a0,stroke:#090;

  subgraph Légende
    web:::web
    batch_web[batch et web]:::batch_web
    conf_web[conf web]:::conf_web
    conf_batch_web[conf batch et web]:::conf_batch_web
    livré:::livré
  end

  subgraph Jasslounge
    22.6.90.01.0:::web_conf_livré
    22.6.90.03.0===22.6.90.05.0:::web_livré
    22.6.90.06.0===22.6.90.07.0:::batch_web_livré
    22.6.90.07.0===22.6.90.08.0:::web_conf_livré
    22.6.90.08.0===22.6.90.09.0:::web_conf_livré
    22.6.90.09.0===22.6.90.10.0:::batch_web_conf_web_livré
  end
  class Jasslounge mep

thank you in advance

CodePudding user response:

If I understand what you're trying to do correctly, then

sed '/web/! { /batch/d; }' "$target_file"

deletes all lines that contain batch but don't contain web.

The way this works is

   /web/! {      # in all lines that don't contain web
     /batch/ {   # but do contain batch
       d;        # delete
     }
   }

This would be more straightfoward with awk, by the way, where you could write

awk '!/batch/ || /web/' "$target_file"
  • Related