Linux 需要对SQL文件中的所有注释进行grep
我有一个包含SQLs的文件,它有几个CREATETABLE命令,后跟一些关于表/列的注释。文件文本如下所示Linux 需要对SQL文件中的所有注释进行grep,linux,awk,sed,grep,Linux,Awk,Sed,Grep,我有一个包含SQLs的文件,它有几个CREATETABLE命令,后跟一些关于表/列的注释。文件文本如下所示 create table abc (col1, ..,..); comment on table abc is 'This is a single line comment'; comment on column abc.col1 is 'This is a multi-line comment. One more line of description here'; comment o
create table abc
(col1, ..,..);
comment on table abc is 'This is a single line comment';
comment on column abc.col1 is 'This is a multi-line comment.
One more line of description here';
comment on column abc.col2 is 'This is a single line comment';
create table mno
(col1, ..,..);
comment on table mno is 'This is a multi-line comment.
One more line of description here';
comment on column mno.col1 is 'This is a single line comment';
collect statistics on mno column (column1);
简单grep命令无法捕获多行注释。我明白,我需要打印所有以“评论”开头的行,直到“;”第一次出现。。在多行注释的情况下,这也意味着在行间打印,而不是以“Commenton”语句开头
提前感谢您的建议。我会将sed用于此范围 此命令执行以下操作:
sed -e 's/comment\(.*\);/\1/' sql.txt
请用您的实际文件名替换sql.txt。我将使用sed来处理此范围 此命令执行以下操作:
sed -e 's/comment\(.*\);/\1/' sql.txt
请用实际文件名替换sql.txt。原始格式:
awk -v RS=';' '/comment on.*/' sqlfile
或在单行中:
awk -v RS=';' '/comment on.*/{$1=$1;print $0}' sqlfile
原始格式:
awk -v RS=';' '/comment on.*/' sqlfile
或在单行中:
awk -v RS=';' '/comment on.*/{$1=$1;print $0}' sqlfile
也许你可以试着删除换行,然后替换;使用换行符:
$ cat /tmp/test | tr -d '\n' | tr ';' '\n' | grep '^[[:space:]]*comment on'
comment on table abc is 'This is a single line comment'
comment on column abc.col1 is 'This is a multi-line comment.One more line of description here'
comment on column abc.col2 is 'This is a single line comment'
comment on table mno is 'This is a multi-line comment.One more line of description here'
comment on column mno.col1 is 'This is a single line comment'
也许你可以试着删除换行,然后替换;使用换行符:
$ cat /tmp/test | tr -d '\n' | tr ';' '\n' | grep '^[[:space:]]*comment on'
comment on table abc is 'This is a single line comment'
comment on column abc.col1 is 'This is a multi-line comment.One more line of description here'
comment on column abc.col2 is 'This is a single line comment'
comment on table mno is 'This is a multi-line comment.One more line of description here'
comment on column mno.col1 is 'This is a single line comment'
这可能适用于您(GNU-sed):
打印单行注释。删除不是多行注释的任何其他行
这可以更简洁地写为:
sed '/^comment on/{:a;/;\s*$/{p;d};n;ba};d' file
这可能适用于您(GNU-sed):
打印单行注释。删除不是多行注释的任何其他行
这可以更简洁地写为:
sed '/^comment on/{:a;/;\s*$/{p;d};n;ba};d' file
简单地说,使用:
合并行
如果希望所有注释都在线,则必须使用N
而不是N
合并保留缓冲区中的行,然后用空格替换换行:
sed -ne '/comment/{:;/\o47;\s*$/!{N;b};s/\n\s*/ /;p}' <file
sed-ne'/comment/{:;/\o47;\s*$/!{N;b};s/\N\s*/;p}只需使用:
合并行
如果希望所有注释都在线,则必须使用N
而不是N
合并保留缓冲区中的行,然后用空格替换换行:
sed -ne '/comment/{:;/\o47;\s*$/!{N;b};s/\n\s*/ /;p}' <file
sed-ne'/comment/{:;/\o47;\s*$/!{N;b};s/\N\s*/;p}您可以发布一些您已经做过的尝试吗?您可以发布一些您已经做过的尝试吗?这可以在没有分支命令(b
)的情况下完成。。。如果注释包含,则可能会中断代码>在一行的末尾。在之前添加\o47
代码>要正确!无论如何,你的剧本似乎比我的快。谢谢@poton,谢谢你宝贵的时间和帮助。我已经接受了PS的回答。因为我觉得这是最简单的。这可以在没有分支命令(b
)的情况下完成。。。如果注释包含,则可能会中断代码>在一行的末尾。在之前添加\o47
代码>要正确!无论如何,你的剧本似乎比我的快。谢谢@poton,谢谢你宝贵的时间和帮助。我已经接受了PS的回答,因为我觉得这是最简单的。谢谢@PS;它给我的o/p正是我想要的:)谢谢@PS;它给我的o/p正是我想要的:)正如建议的那样,您可以在之间添加\s*
和$
获取包含尾随空格的行:sed-n'/comment/{:;/\o47;\s*$/!{n;b};p}'
谢谢@F.Hauri,它给了我所需的输出。由于简单,我使用PS(使用awk)提供的答案。很高兴知道sed也可以做同样的事情:)@PankajK通过使用,您也可以合并行;-)一般来说,我更喜欢sed
而不是awk
,因为它更轻,而且安装频率更高,配置也不好。正如建议的那样,您可以在之间添加\s*
和$
获取包含尾随空格的行:sed-n'/comment/{:;/\o47;\s*$/!{n;b};p}'
谢谢@F.Hauri,它给了我所需的输出。由于简单,我使用PS(使用awk)提供的答案。很高兴知道sed也可以做同样的事情:)@PankajK通过使用,您也可以合并行;-)与awk相比,我更喜欢sed,因为它更轻,安装频率更高,配置也更差。谢谢你的回答。它没有给我正确的o/p,但感谢你及时的建议:)你得到了什么?知道这一点会帮助大家帮助你谢谢你的回答。它没有给我正确的o/p,但感谢你及时的建议:)你得到了什么?知道这一点会帮助每个人帮助你