我有一个 SQL 文件,我需要删除所有评论
-- Sql comment line
如何使用 GREP 或其他工具在 Linux 中实现此目的?
最好的问候,
最佳答案
grep
工具有一个 -v
选项可以反转过滤器的意义。例如:
grep -v pax people
将为您提供people
文件中不包含pax
的所有行。
一个例子是:
grep -v '^ *-- ' oldfile >newfile
它去掉了注释标记前只有空白的行。它不会但是会像这样改变行:
select blah blah -- comment here.
如果你想这样做,你可以使用像 sed
这样的东西:
sed -e 's/ --.*$//' oldfile >newfile
它编辑每一行,删除从 "--"
到行尾的所有字符。
请记住,在真实 SQL 语句中查找字符串"--"
时要小心,例如(人为的):
select ' -- ' | colm from blah blah blah
如果你有这些,你最好创建/使用 SQL 解析器而不是简单的文本修改工具。
sed
运行的记录:
pax$ echo '
...> this is a line with -- on it.
...> this is not
...> and -- this is again' | sed -e 's/ --.*$//'
this is a line with
this is not
and
对于 grep
:
pax$ echo '
-- this line starts with it.
this line does not
and -- this one is not at the start' | grep -v '^ *-- '
this line does not
and -- this one is not at the start
关于sql - 如何使用 "grep"从文件中删除注释?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7804631/