Ubuntu grep和awk只复制insert语句

Ubuntu grep和awk只复制insert语句,ubuntu,awk,grep,Ubuntu,Awk,Grep,我有一个包含多行Create、Insert、Alter语句的.sql文件,现在我只想获取Insert语句并将其复制到另一个.sql文件中 Create和Insert语句的示例如下 CREATE CACHED TABLE "PUBLIC"."xyz"( "ID" BIGINT NOT NULL, "GID" BIGINT NOT NULL, "GDATA" BINARY(65

我有一个包含多行Create、Insert、Alter语句的.sql文件,现在我只想获取Insert语句并将其复制到另一个.sql文件中

Create和Insert语句的示例如下

CREATE CACHED TABLE "PUBLIC"."xyz"(
    "ID" BIGINT NOT NULL,
    "GID" BIGINT NOT NULL,
    "GDATA" BINARY(65536)
);


INSERT INTO "PUBLIC"."INFORMIX_SERVERS" VALUES
(4521215141212121, 8763121524544545454
4545, X'3a000000127265706f7369746f7279536572');
我尝试了以下命令,但没有成功 cat dump.sql | grep-i'插入*;'


cat dump.sql | grep-E'insert*;'|awk-F\>'{print$2}'| awk-F\<{print$1}'

如果输入文件的INSERT语句在其所有行之前和之后都是空的,那么这可能会有所帮助

awk -v RS= '/^INSERT/' Input_file
或者您可以尝试以下更通用的解决方案

awk '!NF||/^CREATE/{foundInsert=""} /^INSERT/{foundInsert=1} foundInsert' Input_file
说明:增加对以上内容的详细说明

awk '               ##Starting awk program from here.
!NF || /^CREATE/{   ##Checking condition if NF is NULL OR line starts from CREATE then do following.
  foundInsert=""    ##Nullifying foundInsert here.
}
/^INSERT/{          ##Checking condition if line starts from INSERT then do following.
  foundInsert=1     ##Setting foundInsert to 1 here.
}
foundInsert         ##Checking if foundInsert is SET then print that line.
' Input_file        ##Mentioning Input_file name here.

如果输入文件的INSERT语句在其所有行之前和之后都是空的,那么这可能会有所帮助

awk -v RS= '/^INSERT/' Input_file
或者您可以尝试以下更通用的解决方案

awk '!NF||/^CREATE/{foundInsert=""} /^INSERT/{foundInsert=1} foundInsert' Input_file
说明:增加对以上内容的详细说明

awk '               ##Starting awk program from here.
!NF || /^CREATE/{   ##Checking condition if NF is NULL OR line starts from CREATE then do following.
  foundInsert=""    ##Nullifying foundInsert here.
}
/^INSERT/{          ##Checking condition if line starts from INSERT then do following.
  foundInsert=1     ##Setting foundInsert to 1 here.
}
foundInsert         ##Checking if foundInsert is SET then print that line.
' Input_file        ##Mentioning Input_file name here.
将记录分隔符设置为;然后使用INSERT搜索记录。用gsub去除前导空格并打印


将记录分隔符设置为;然后使用INSERT搜索记录。用gsub去除前导空格并打印。

请在您的问题中以代码的形式添加您的努力,这在SOnot my downvote上得到了高度鼓励。顺便说一句,您的insert语句是否总是用段落或换行符分隔?当然感谢您的指导,不,它没有分隔。我尝试了一些命令,如下面的链接,但仍然无法获得输出。当然,请在您的问题中添加这些命令,以避免对您的问题投反对票或接近票,评论不是为了努力或代码添加:请在您的问题中以代码的形式添加努力,顺便说一句,SOnot my downvote非常鼓励您使用哪种语言。您的插入语句是否总是与段落或换行符分隔?当然感谢您的指导,不,它没有分隔。我已经尝试了一些命令,如下面的链接,但仍然无法获得输出。当然,请在您的问题中添加这些命令,以避免对您的问题进行否决票或接近票,评论不是为了努力或代码添加:感谢or命令。这是有效的。@Shubhamkapoor,您的欢迎Shubham,也请您再次添加您在您的问题评论中发布/显示的内容,因为这是一个鼓励,谢谢。感谢您的指导,我已经添加了我的工作,我将确保从下一次开始添加相同的内容。感谢OR命令。这是有效的。@Shubhamkapoor,您的欢迎Shubham,也请您再次添加您在您的问题评论中发布/显示的内容,因为这是一个鼓励,谢谢。感谢您的指导,我已经添加了我的内容,我将确保从下一次开始添加相同的内容。