The csplit command enables you to split the file wherever a pattern is found in a line:

[email protected]:~ > cat birds albatross blackbird chaffinch duck eagle fieldfare [email protected]ible:~ > csplit birds /finch/

-rw-i—i— 1 user users 51 2004-06-10 10:47 birds

albatross blackbird [email protected]:~ > cat xx01 chaffinch duck eagle fieldfare

In this example, you have split the file according to the pattern finch. This was found in the line containing chaffinch, so that line became the first line of the second file when the file was split.

The pattern on which we split the file could be a regular expression. One obvious use for this is to break up a structured file that has a standard separator for sections. In this example, you will also use {*} to indicate a split on the same pattern as many times as possible:

[email protected]:~ > cat file line 1 line 2

line 3 line 4 line 5

line 6

[email protected]:~ > ls file xx00 xx01 xx02 [email protected]:~ > cat xx00 line 1 line 2

line 3 line 4 line 5

Note that every time the string == was encountered, a new file was started.

Was this article helpful?

0 0

Post a comment