Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>Yes</h2> <hr> <pre><code>:g/^func/.;/^[^!]/-1 print </code></pre> <p><br /> <em>Update</em></p> <hr> <p>An explanation was suggested...so here goes... vi(1) is powerful in part because it is a cursor-addressing extension to Ken Thompson's original line-oriented ed(1) editor. (ed(1) and its spinoff <code>ex</code> is still available on Linux after all these years, albeit in clone form like vi itself.) ed and its early-unix siblings were the first programs anywhere to use regular expressions.</p> <p>Ok, create a file with 26 or so lines, one for each letter of your alphabet and start vi, ed, or ex. (For ed or ex, leave out the : characters.) Try:</p> <pre><code>:1;/m/p </code></pre> <p>The general form of a vi command is: <em>addr, addr2 commmand</em></p> <p>In my example the command is just <code>p</code> for print. addr1 and addr2 are usually a line number or a regular expression using /re/ to search downward or ?re? to search upward. Try <code>/c/;/g/p</code> which prompts me to explain: the <code>;</code> causes the editor to switch to the line found by the first address before it evaluates the second address. It doesn't always matter in the default wrapscan mode but if you type <code>:set nows</code> (not in ed) then search patterns won't wrap and the difference between <code>,</code> and <code>;</code> becomes bigger.</p> <p>The most important line mode command wasn't used in my example but it should be mentioned here: <code>:s/pattern/replacement/</code> or <code>:s/pattern/replacement/g</code>. This command can of course take addresses so a typical command is <code>1,$s/old/new/g</code> The <code>$</code> identifies the last line. The default address for most commands is the current line but for the <code>global</code> or <code>g</code> command it defaults to <code>1,$</code> and has the general form</p> <pre><code>addr1, addr2 g /pattern/ any_linemode_command </code></pre> <p>For example, say I'm <a href="https://stackoverflow.com/users/1/jeff-atwood"><strong>Jeff</strong></a> but I want to blame <a href="https://stackoverflow.com/users/4/joel-spolsky"><strong>Joel</strong></a> for different types of critical errors in the logs. I need to be stealthy and not change the Jeff's on mere warning lines, so I need:</p> <pre><code>g/critical.*error/s/Jeff/Joel/ </code></pre> <p>That will run the substitute command on every line of the file that has the pattern "critical <em>anything</em> error" and then just change Jeff to Joel.</p> <p>So now the answer should be fairly clear. The command works as follows: on every line of the file, check to see if the line begins with /^func/ (func at the beginning of the line) and if it does, start with <code>.</code> (the current line) then, resetting the current address to that line (<code>;</code>) search for a line that does NOT begin with <code>!</code>, and if it's found, subtract one from the line number found (back up slightly to the last actual comment) and then just run the <code>print</code> command.</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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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