Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I split a file up by matching lines context?
    text
    copied!<p>I have a file, <code>x</code>, with section delimiters:</p> <pre><code>The first section #! The second section #! The third section </code></pre> <p>And I want to split it up into a sequence of separate files, like:</p> <pre><code>The first section</code></pre> <pre><code>#! The second section</code></pre> <pre><code>#! The third section</code></pre> <p>I thought <code>csplit</code> would be the solution, with a command-line something like:</p> <pre><code>$ csplit -sk x '/#!/' {9999}</code></pre> <p>But the second file (<code>xx01</code>) ends up containing both delimiters:</p> <pre><code>#! The second section #! </code></pre> <p>Any ideas for how to accomplish what I want in a POSIX compliant way? (Yes, I could reach for Perl/Python/Ruby and friends; but, the point is to stretch my shell knowledge.)</p> <hr> <p>I worry that I've found a bug in OSX csplit. Can people give the following a go and let me know the results?</p> <pre><code>#!/bin/sh test -e work="$(basename $0).$RANDOM" mkdir $work csplit -sk -f "$work/" - '/#/' '{9999}' &lt;&lt;EOF First # Second # Third EOF if [ $(grep -c '#' $work/01) -eq 2 ]; then echo FAIL Repeat else echo PASS Repeat fi rm $work/* csplit -sk -f "$work/" - '/#/' '/#/' &lt;&lt;EOF First # Second # Third EOF if [ $(grep -c '#' $work/01) -eq 2 ]; then echo FAIL Exact else echo PASS Exact fi uname -a </code></pre> <p>When I run it on my Snow Leopard box, I get:</p> <pre><code>$ ./csplit-test csplit: #: no match FAIL Repeat PASS Exact Darwin lani.bigpond 11.2.0 Darwin Kernel Version 11.2.0: Tue Aug 9 20:54:00 PDT 2011; root:xnu-1699.24.8~1/RELEASE_X86_64 x86_64 </code></pre> <p>And on my Debian box, I get:</p> <pre><code>$ sh ./csplit-test csplit: `/#/': match not found on repetition 2 PASS Repeat PASS Exact </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