For example: I have a text as following:
Jul 11 xxxx xxxx start xxxxx
....
....
....
Jul 11 xxxx xxxx stop xxxxx
Jul 11 xxxx xxxx start xxxxx
....
....
....
Jul 11 xxxx xxxx stop xxxxx
....
now I want to split the above text file into different files based on "start" and "stop", like
/***text1.txt******/
Jul 11 xxxx xxxx start xxxxx
....
....
....
Jul 11 xxxx xxxx stop xxxxx
/***text2.txt******/
Jul 11 xxxx xxxx start xxxxx
....
....
....
Jul 11 xxxx xxxx stop xxxxx
How can I do that? Thanks.
This can make it:
Explanation
{if ($0 ~ /start/) a++}
looks for lines containing the wordstart
. If so, increments the variablea
, which is0
by default.{print >> "file"}'
prints$0
(that is, the whole line) to a file called "file" in the same directory.{print >> "file"a}
prints the line to a file called "file" + variablea
, which happens to be 0, 1, 2... So it prints tofile1
,file2
...Test
With awk, 'if ($0 ~ )' is implicit.