Note that there are some explanatory texts on larger screens.

plurals
  1. PODelete all lines between two patterns (exclusive of the pattern) using sed or awk
    primarykey
    data
    text
    <p>I have a somewhat large output text file where I need to delete all lines between two patterns but retain the pattern match.</p> <p>The files look vaguely like the following output. </p> <pre><code> TEST #1 coef1 | 48.36895 3.32013 14.57 0.000 41.86141 54.87649 coef2 | -50.08894 10.47335 -4.78 0.000 -70.61697 -29.56092 | indicator | 0 | .6647992 2.646627 0.25 0.802 -4.55925 5.888849 1 | 2.118701 5.225777 0.41 0.686 -8.19621 12.43361 | year | 2 | -.4324005 2.231387 -0.19 0.847 -4.836829 3.972028 3 | -.362762 1.97184 -0.18 0.854 -4.254882 3.529358 | _cons | 16.95753 6.342342 2.67 0.008 4.526383 29.38869 TEST #2 coef2 | 48.36895 3.32013 14.57 0.000 41.86141 54.87649 coef3 | -50.08894 10.47335 -4.78 0.000 -70.61697 -29.56092 | year | 4 | .6647992 2.646627 0.25 0.802 -4.55925 5.888849 5 | 2.118701 5.225777 0.41 0.686 -8.19621 12.43361 | idnumber | 6 | -.4324005 2.231387 -0.19 0.847 -4.836829 3.972028 7 | -.362762 1.97184 -0.18 0.854 -4.254882 3.529358 | _cons | 16.95753 6.342342 2.67 0.008 4.526383 29.38869 </code></pre> <p>I need to take the following output and delete all the lines between "year" and "_cons" but I need to retain the line starting with "_cons". The desired output is like so:</p> <pre><code> TEST #1 coef1 | 48.36895 3.32013 14.57 0.000 41.86141 54.87649 coef2 | -50.08894 10.47335 -4.78 0.000 -70.61697 -29.56092 | indicator | 0 | .6647992 2.646627 0.25 0.802 -4.55925 5.888849 1 | 2.118701 5.225777 0.41 0.686 -8.19621 12.43361 | year | _cons | 16.95753 6.342342 2.67 0.008 4.526383 29.38869 TEST #2 coef2 | 48.36895 3.32013 14.57 0.000 41.86141 54.87649 coef3 | -50.08894 10.47335 -4.78 0.000 -70.61697 -29.56092 | year | _cons | 16.95753 6.342342 2.67 0.008 4.526383 29.38869 </code></pre> <p>I wrote the following script (under OS X):</p> <pre><code>sed '/^ +year/,/^ +_cons/{/^ +year/!{/^ +_cons/!d}}' input.txt &gt;output.txt </code></pre> <p>but I got the following error:</p> <pre><code>sed: 1: "/^ +year/,/^ +_cons/{/^ ...": extra characters at the end of d command </code></pre> <p>I'm not sure if this approach is even correct because I can't seem to get sed to execute. Is sed even appropriate here or should I use awk?</p> <p>One last note, I need this script to work on a relatively generic Unix install. I have to send this to someone who must execute it under a very basic AIX (I think) install. No perl, no python, and I can't do much troubleshooting on their install over email.</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