Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I assume that the only lines starting with 04, 05 etc. are the lines separating the different blocks:</p> <pre><code>awk ' BEGIN { mode="unknown" } /^04/ { mode="04" } /^05/ { mode="05" } /^06/ { mode="06" } { if (mode != "unknown") print $0 &gt;&gt; sprintf("%s_file.txt", mode)}' &lt; $file </code></pre> <p><code>awk</code> is basically an environment to execute actions depending on patterns. Every pattern-action statement has the form</p> <pre><code>pattern { action } </code></pre> <p>In its most simple form a pattern is a regular expression matching the current input line. BEGIN is a special case which "matches" before the input is read and there is also an END "pattern" which gets executed after the input file is consumed.</p> <p>On execution <code>awk</code> reads the input file line by line and executes all actions where the pattern matches the line. In the code above the <code>mode</code> variable is set if the input line begins (<code>^</code>) with 04, 05 etc. The last line (without the pattern) matches to all lines and just writes the whole line to the corresponding file.</p> <p>I'll try to summarize this in some pseudo code:</p> <pre><code>mode := "unknown" for each line of input do if line starts with 04 then set mode to 04 endif if line starts with 05 then set mode to 05 endif if line starts with 06 then set mode to 06 endif # mode is now either "unknown" if no 04/05/06 pattern has been read # from the file yet or holds the most recently read block start (04/05/06) if mode is not "unknown" then append whole line to file named 'mode'_file.txt endif endfor </code></pre> <p>If you want to go further have a look at the man page or browse the web for <code>awk</code> introduction. There you also can learn more about the <em>pattern</em> part which can do much more than just match the current input line as in the example above.</p> <hr> <p>In case all blocks start with two digits (any digits) the code above can be shortened to</p> <pre><code>awk ' BEGIN { mode="unknown" } /^[[:digit:]][[:digit:]]/ { mode=substr($0, 1, 2) } { if (mode != "unknown") print $0 &gt;&gt; sprintf("%s_file.txt", mode)}' &lt; $file </code></pre>
 

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