模式和复制到新文件之间的Grep字符串 [英] Grep String between pattern and copy to new file
问题描述
我具有以下格式的数据,并且试图捕获两次出现的字符串之间的数据并将其保存在文件中.
I am having data in below format and I am trying to capture data between 2 occurrence of string and keep it in a file.
create statement
CREATE VIEW `T1` AS SELECT
aa
bb
cc
dd
create statement
CREATE VIEW `T2` AS SELECT
aa
ff
ee
create statement
CREATE VIEW `T3` AS SELECT
aa
bb
ff
..
...
..
我想要以以下格式输出:
I want output in below format :
FileName T1应该包含:-
FileName T1 should contain :--
create statement
CREATE VIEW `T1` AS SELECT
aa
bb
cc
dd
FileName T2应该包含:-
FileName T2 should contain :--
create statement
CREATE VIEW `T2` AS SELECT
aa
ff
ee
输出文件名来自反引号包围的值
Output filename comes from the value surrounded by backticks
我尝试过:
sed -n '/create statement/,/create statement/p'
推荐答案
能否请您尝试一下,并告诉我是否对您有帮助.
Could you please try following and let me know if this helps you.
awk '/create statement/{create=$0;next} /CREATE VIEW/{val=$3;gsub("`","",val);filename=val;if(create){print create ORS $0 > filename};next} {print > filename}' Input_file
它将创建3个名为 T1
, T2
和 T3
的输出文件,依此类推,直到所有T都出现为止.如果这不是您的问题,请在问题中明确说明并添加更多详细信息.
It will create 3 output files named T1
, T2
and T3
and so on till all the occurrences of T's. If this is not your question then please be clear in your question and add more details on it too.
现在也添加非单一班轮形式的解决方案:
Adding a non-one liner form of solution too now:
awk '
/create statement/{
create=$0;
next
}
/CREATE VIEW/{
val=$3;
gsub("`","",val);
filename=val;
if(create){
print create ORS $0 > filename};
next
}
{
print > filename
}
' Input_file
这篇关于模式和复制到新文件之间的Grep字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!