在不编写正则表达式的情况下,用bash替换XML中的字符串

时间:2017-07-11 04:52:53

标签: xml string bash replace xml-parsing

我有一个大的平面文件,其中包含我想删除的重复字符串的许多实例:

<content type="html">
  &lt;p&gt; &lt;/p&gt;
  &lt;p&gt;Jump around on couch, meow constantly until given food.&lt;/p&gt;
  &lt;p&gt; &lt;/p&gt;
</summary>

因为you can't parse [X]HTML with regex我正在寻找一个解决方案,我不必编写自己的正则表达式。我尝试使用tr没有任何运气。这是我想要的输出:

<content type="xhtml">

  &lt;p&gt;Jump around on couch, meow constantly until given food.&lt;/p&gt;

</summary>

如何在不编写正则表达式的情况下从bash中删除重复字符串?

2 个答案:

答案 0 :(得分:-1)

我使用了一个名为rpl的工具,它不需要我编写任何正则表达式:

$ rpl '&lt;p&gt; &lt;/p&gt;' '' /tmp/file

Really DELETE all occurences of &lt;p&gt; &lt;/p&gt; (case sensitive)? (Y/[N]) Y
Replacing "&lt;p&gt; &lt;/p&gt;" with "" (case sensitive) (partial words matched)
A Total of 55 matches replaced in 1 file searched.

使用brew install rpl通过Homebrew安装。在2分钟内完成。

答案 1 :(得分:-1)

有了正则表达式的知识,它将是:

/**
 * Main class of the application.
 */
public class Main{

  // Define the variable for the window of the game.
  public static JFrame window;

  // Define the variable for the introductory video.
  public static MediaPlayer video;

  // Define the variable for the key listener.
  public static KeyListener keyListener;

  // Define the variable for the Runnable interface.
  public static Runnable runnable;

  // Define the variable for a thread.
  public static Thread thread;

  /**
   * Main function of the application.
   */
  public static void main(String[] args){

    // Prevent the JavaFX toolkit from closing.
    Platform.setImplicitExit(false);

    // Create the window of the game.
    window = new JFrame();

    // Set the title.
    window.setTitle("Chip");

    // Set the resolution as 1920 x 1280.
    window.setSize(1926,1343);

    // Set the location as in the middle of the screen.
    window.setLocationRelativeTo(null);

    // Set the operation when the window closes.
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // Disable the maximization and resizable mode.
    window.setResizable(false);

    // Enable the listening of keys.
    enableKeyListener();

    // Show the window.
    window.setVisible(true);

    // Create the Runnable interface.
    runnable = new Runnable(){

      /**
       * Main function of the Runnable interface.
       */
      public void run(){

        try{

          // Show the introductory video.
          showVideo();

          // Pause the execution of the application for 30 seconds (duration of the introductory video).
          Thread.sleep(30000);

        }catch (InterruptedException interruptedException){

          // Stop the video if an interruption has been occurred.
          video.stop();

        }finally{

          // Set the background image.
          String filename = "./media/image/background.jpg";
          window.setContentPane(new JLabel(new ImageIcon(filename)));

          // Show the window.
          window.setVisible(true);

        }

      }

    };

    // Create a new thread.
    thread = new Thread(runnable);

    // Start the execution of the thread.
    thread.start();

  }

  /**
   * Enables the listening of keys.
   */
  public static void enableKeyListener(){

    // Create the key listener.
    keyListener = new KeyListener(){

      // Set the behavior whenever a key is pressed.
      public void keyPressed(KeyEvent keyEvent){

        // Check if the "Escape" key is pressed.
        if (keyEvent.getKeyCode() == KeyEvent.VK_ESCAPE){

          // Check if the introductory video it is being played.
          if (video.getStatus().equals(Status.PLAYING)){

            // Make an interruption in the thread that is being executed.
            thread.interrupt();

          }

        }

      }

      // Set the behavior whenever a key is released.
      public void keyReleased(KeyEvent keyEvent){}

      // Set the behavior whenever a key is typed.
      public void keyTyped(KeyEvent keyEvent){}

    };

    // Add the key listener to the window of the game.
    window.addKeyListener(keyListener);

  }

  /**
   * Shows the introductory video.
   */
  public static void showVideo(){

    // Create the video panel and the JavaFX panel.
    JPanel panelVideo = new JPanel();
    JFXPanel panelJavaFX = new JFXPanel();

    // Set the size of the video panel as the resolution of the introductory video (1920 x 1080).
    panelVideo.setSize(1920,1080);

    // Set the location of the video panel as in the middle of the window of the game.
    int coordinateX = (window.getWidth() - panelVideo.getWidth() - window.getInsets().left - window.getInsets().right) / 2;
    int coordinateY = (window.getHeight() - panelVideo.getHeight() - window.getInsets().top - window.getInsets().bottom) / 2;
    panelVideo.setLocation(coordinateX,coordinateY);

    // Define the video file.
    String filename = "./media/video/introduction.mp4";
    video = new MediaPlayer(new Media(new File(filename).toURI().toString()));

    // Add the video to the JavaFX panel.
    panelJavaFX.setScene(new Scene(new Group(new MediaView(video))));

    // Add the JavaFX panel to the video panel.
    panelVideo.add(panelJavaFX);

    // Add the video panel to the window of the game.
    window.getContentPane().setLayout(null);
    window.add(panelVideo);

    // Play the video.
    video.play();

  }

}