dpqg86714
2017-02-12 19:49
浏览 110
已采纳

使用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.

图片转代码服务由CSDN问答提供 功能建议

我有一个大约250M的文件,我想用 sed <分成两个文件 ubuntu中的/ code>命令。

我想要分割的行如下:

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

第二个文件应该包含上面的字符串。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • dswm97353 2017-02-12 20:11
    最佳回答

    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.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题