Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think <code>awk</code> is probably the better tool for this job, but it can be done with <code>sed</code>:</p> <pre><code>sed '/^[^ ]* *00 *|/{ :a s/\(|.*[^(]\)\([0-9a-f][1-9a-f]\)/\1(\2)/ t a :b s/\(|.*[^(]\)\([1-9a-f][0-9a-f]\)/\1(\2)/ t b }' data </code></pre> <p>The script looks for lines containing <code>00</code> before the pipe, and only applies the operations to those lines. There are two substitute operations, each wrapped in a loop. The <code>:a</code> and <code>:b</code> lines are labels. The <code>t a</code> and <code>t b</code> commands are a conditional jump to the named label if there was a substitution performed since the last jump. The two substitutions are almost symmetric; the first deals with any number not ending in 0; the second deals with any number not starting with 0; between them, they ignore <code>00</code>. The patterns look for a pipe, any sequence of characters not ending with an open parenthesis <code>(</code>, and the appropriate pair of digits; it replaces that so that the number ends up inside parentheses. The loops are necessary because a <code>g</code> modifier doesn't start from the beginning again, and the patterns work backwards through the numbers.</p> <p>Given this data file (a slightly extended version of yours):</p> <pre><code>foobar 42 | ff 00 00 00 00 foobaz 00 | 0a 00 0b 00 00 foobie 00 | 00 00 00 00 00 bar 00 | ab ba 00 cd 00 fizbie 00 | ab ba 00 cd 90 fizzbuzz 00 | ab ba 00 cd 09 </code></pre> <p>the output from the script is:</p> <pre><code>foobar 42 | ff 00 00 00 00 foobaz 00 | (0a) 00 (0b) 00 00 foobie 00 | 00 00 00 00 00 bar 00 | (ab) (ba) 00 (cd) 00 fizbie 00 | (ab) (ba) 00 (cd) (90) fizzbuzz 00 | (ab) (ba) 00 (cd) (09) </code></pre> <p>It is moderately educational to add a <code>p</code> after each of the substitute commands, so you can see how the substitutions work:</p> <pre><code>foobar 42 | ff 00 00 00 00 foobaz 00 | 0a 00 (0b) 00 00 foobaz 00 | (0a) 00 (0b) 00 00 foobaz 00 | (0a) 00 (0b) 00 00 foobie 00 | 00 00 00 00 00 bar 00 | ab ba 00 (cd) 00 bar 00 | ab (ba) 00 (cd) 00 bar 00 | (ab) (ba) 00 (cd) 00 bar 00 | (ab) (ba) 00 (cd) 00 fizbie 00 | ab ba 00 (cd) 90 fizbie 00 | ab (ba) 00 (cd) 90 fizbie 00 | (ab) (ba) 00 (cd) 90 fizbie 00 | (ab) (ba) 00 (cd) (90) fizbie 00 | (ab) (ba) 00 (cd) (90) fizzbuzz 00 | ab ba 00 cd (09) fizzbuzz 00 | ab ba 00 (cd) (09) fizzbuzz 00 | ab (ba) 00 (cd) (09) fizzbuzz 00 | (ab) (ba) 00 (cd) (09) fizzbuzz 00 | (ab) (ba) 00 (cd) (09) </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
    1. 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