sql - 如何使用 "grep"从文件中删除注释?

标签 sql grep

我有一个 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/

相关文章:

sql - 在oracle中使整个列名小写

mysql - 这个 coldfusion9 SQL 搜索查询可以更快吗?

sql - 从另一个表的多行中生成 1 行

awk - grep 匹配某个模式的行,以及匹配前后的行,直到不同的模式

unix - 如何使用 egrep 查找具有 2 个连续相同字符的单词?

regex - sed通配符替换

php - 为 INSERT 语句 PHP 设置默认值

sql - 为什么在解释查询中看不到枚举类型的索引?

bash - 递归 Grep 仅显示总匹配数

linux - grep 包含两个或多个单词,一行一行包含多个文件