使用sed将文件拆分为两个文件

I have a file of around 250M, which I want to split into two using the sed command in ubuntu.

The line from which I wan to split is like:

DROP TABLE IF EXISTS `captcha_log`CREATE TABLE IF NOT EXISTS `captcha_log`

The second file should include the above string.

You can use the csplit tool for this:

pattern='DROP TABLE IF EXISTS `captcha_log`CREATE TABLE IF NOT EXISTS `captcha_log`'
csplit infile /"$pattern"/

If you really want to use sed, you could do it in two steps:

pattern='DROP TABLE IF EXISTS `captcha_log`CREATE TABLE IF NOT EXISTS `captcha_log`'
sed -n "/$pattern/q;p" infile > outfile1
sed -n "/$pattern/,\$p" infile > outfile2

-n prevents printing as the default action; /$pattern/q exits as soon as the pattern line is reached, and p is executed for each line before that.

The second command just uses an address range, from the pattern line to the last one $. Because the command is double quoted to get $pattern in, the $ for the last line has to be escaped, \$.

Or you could do a single pass with awk:

pattern='DROP TABLE IF EXISTS `captcha_log`CREATE TABLE IF NOT EXISTS `captcha_log`'
awk -v pattern="$pattern" '$0 == pattern { ++flag }
                           flag { print > "outfile2"; next }
                           { print > "outfile1" }' infile

The first pattern checks if the line matches our split line and increments a flag if so; the second line checks if the flag if set, and if so, prints to the second output file and skips the last pattern/action; the third action is executed if the flag is not set and prints to the first output line.