Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I search for two sequential newlines (\n) using perl?
    primarykey
    data
    text
    <p>How can I search for two sequential newlines (\n) using perl? Or more specifically, why is a search for \n\n not successful even when they exist? I have a file with sequential newlines (verfied with a hex editor, they are not returns, etc.) but perl doesn't seem accept this regex.</p> <p>perl -pi -e 's/\n\n/TEST/g' myfile.xml <strong>= no results</strong></p> <p>I'm actually trying to insert a bit of code into XML files but the linefeeds are in the middle, what's the most elegant way of doing so? I came up with a monstrous perl one liner but the double line feeds seem to cause the failure.</p> <p>I want to change a portion of a gtkrc file from:</p> <pre><code>GtkWidget::link-color = @link_color GtkWidget::visited-link-color = @text_color #################### # Color Definitions #################### </code></pre> <p>to:</p> <pre><code>GtkWidget::link-color = @link_color GtkWidget::visited-link-color = @text_color GtkWindow::resize-grip-height = 0 GtkWindow::resize-grip-width = 0 #################### # Color Definitions #################### </code></pre> <p>Using the a portion of the original code as my search term (there are tabs before each line on the original script, btw), my find and replace terms were:</p> <pre><code>color\n\n\t\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\n\t\# Color color\n\n\tGtkWindow::resize-grip-height = 0\n\tGtkWindow::resize-grip-width = 0\n\n\t\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\n\t\# Color </code></pre> <p>I came up with this big ugly perl command:</p> <pre><code>perl -pi -e 's/color\n\n\t\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\n\t\# Color/color\n\n\tGtkWindow::resize-grip-height = 0\n\tGtkWindow::resize-grip-width = 0\n\n\t\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\n\t\# Color/g' /usr/share/themes/Ambiance/gtk-2.0/gtkrc </code></pre> <p>EDIT: Corrected code from Zaid:</p> <pre><code>perl -0777 -pi -e 's/color\n\n\t\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\n\t\# Color/color\n\n\tGtkWindow::resize-grip-height = 0\n\tGtkWindow::resize-grip-width = 0\n\n\t\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\n\t\# Color/g' /usr/share/themes/Ambiance/gtk-2.0/gtkrc </code></pre> <p>As mentioned, it seems to be the \n\n that causes issues as anything else can be replaced. What is the better way to do this?</p> <p>I'm on Ubuntu 11.10, command will be run from shell script and not perl script.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload