Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Awk is appropriate for this problem domain because the text file is already (more or less) organized into columns. Here's what I would use:</p> <pre><code>awk 'NF == 3 &amp;&amp; index($2, "=") { filename = substr($2, index($2, "=") + 1) } NF == 2 &amp;&amp; filename { print $0 &gt; (filename ".txt") }' &lt; input.txt </code></pre> <p>Explanation:</p> <p>Think of the lines starting with <em>variableStep</em> as "three columns" and the other lines as "two columns". The above script says, "Parse the text file line-by-line; if a line has three columns and the second column contains an '=' character, assign 'all of the characters in the second column that occur after the '=' character' to a variable called <code>filename</code>. If a line has two columns and the <code>filename</code> variable's been assigned, write the entire line to the file that's constructed by concatenating the string in the filename variable with '.txt'".</p> <p>Notes:</p> <ul> <li><strong>NF</strong> is a built-in variable in Awk that represents the "number of fields", where a "field" (in this case) can be thought of as a column of data.</li> <li><strong>$0</strong> and <strong>$2</strong> are built-in variables that represent the entire line and the second column of data, respectively. (<strong>$1</strong> represents the first column, <strong>$3</strong> represents the third column, etc...)</li> <li><strong>substr</strong> and <strong>index</strong> are built-in functions described here: <a href="http://www.gnu.org/software/gawk/manual/gawk.html#String-Functions" rel="nofollow">http://www.gnu.org/software/gawk/manual/gawk.html#String-Functions</a> The <strong>redirection operator (>)</strong> acts differently in Awk than it does in a shell script; subsequent writes to the same file are appended.</li> <li>String concatenation is performed simply by writing expressions next to each other. The parenthesis ensure the concatenation happens before the file gets written to.</li> </ul> <p>More details can be found here: <a href="http://www.gnu.org/software/gawk/manual/gawk.html#Two-Rules" rel="nofollow">http://www.gnu.org/software/gawk/manual/gawk.html#Two-Rules</a></p>
 

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