使用正则表达式有选择地搜索和替换某些行

发布于 2024-09-03 04:02:00 字数 1233 浏览 5 评论 0原文

我有一个包含很多 SQL 语句的文件,例如:

CREATE TABLE "USER" (
    "ID" INTEGER PRIMARY KEY,
    "NAME" CHARACTER VARYING(50) NOT NULL,
    "AGE" INTEGER NOT NULL
);

COPY "USER" (id, name, age) FROM stdin;
1   Skywalker   19
2   Kenobi      57

我希望 COPY 语句中的列名大写并加引号:

COPY "USER" ("ID", "NAME", "AGE") FROM stdin;

使用 sed,我发现了以下正则表达式:

sed -r 's/([( ])(\w+)([,)])/\1"\U\2\E"\3/g'

确实 替换列名,但选择性不够,并替换文件中的其他单词:

~/test]$sed -r 's/([( ])(\w+)([,)])/\1"\U\2\E"\3/g' star_wars_example
CREATE TABLE "USER" (
  "ID" INTEGER PRIMARY "KEY",
  "NAME" CHARACTER VARYING("50")NOT "NULL",
  "AGE" INTEGER NOT NULL
);

COPY "USER" ("ID", "NAME", "AGE") FROM stdin;
1   Skywalker   19
2   Kenobi      57

为了避免此问题,我希望 sed 仅将我的正则表达式应用于以 < 开头的行code>COPY 并以 FROM stdin; 结尾。

我研究过lookahead/lookbehind,但是sed 不支持它们。它们似乎在 super-sed 中受支持,但我目前正在使用 Cygwin(这里强制使用 Windows...)并且它在软件包列表中似乎不可用。

有没有办法强制 sed 仅考虑特定行?

我考虑过在应用 sed 之前通过 grep 管道传输我的文件,但其他行将从输出中消失。

我错过了一些明显的东西吗?

如果答案很容易适用于默认的 Cygwin 安装,那就太好了。我想我可以尝试在 cygwin 上安装 super-sed,但我想知道是否有更明显的想法

I have a file containing a lot of SQL statements, such as:

CREATE TABLE "USER" (
    "ID" INTEGER PRIMARY KEY,
    "NAME" CHARACTER VARYING(50) NOT NULL,
    "AGE" INTEGER NOT NULL
);

COPY "USER" (id, name, age) FROM stdin;
1   Skywalker   19
2   Kenobi      57

I want the column names in the COPY statements to be uppercased and quoted:

COPY "USER" ("ID", "NAME", "AGE") FROM stdin;

Using sed, I found the following regexp:

sed -r 's/([( ])(\w+)([,)])/\1"\U\2\E"\3/g'

It does replace the column names, but it is not selective enough, and replaces other words in the file:

~/test]$sed -r 's/([( ])(\w+)([,)])/\1"\U\2\E"\3/g' star_wars_example
CREATE TABLE "USER" (
  "ID" INTEGER PRIMARY "KEY",
  "NAME" CHARACTER VARYING("50")NOT "NULL",
  "AGE" INTEGER NOT NULL
);

COPY "USER" ("ID", "NAME", "AGE") FROM stdin;
1   Skywalker   19
2   Kenobi      57

To avoid this problem, I want sed to only apply my regexp to the lines starting with COPY and ending with FROM stdin;.

I have looked into lookahead / lookbehind, but they are not supported in sed. They seem to be supported in super-sed, but I am currently using Cygwin (Windows is mandatory here...) and it does not seem available in the package list.

Is there a way to force sed to only consider specific line?

I've considered piping my file through grep before applying sed, but other lines will then disappear from the output.

Am I missing something obvious?

It would be great if the answer was easily applicable on a default Cygwin install. I guess I could try installing super-sed on cygwin, but I'd like to know if there are more obvious ideas

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七秒鱼° 2024-09-10 04:02:00

由于我目前没有可用的 sed,并且从未实际使用过分组,因此该命令可能会也可能不会(完全或按预期)工作=)

尝试

sed -r '/^COPY /{ s/([( ])(\w+)([,)])/\1"\U\2\E"\3/g }'

如果我正确理解了手册,这将执行替换以 COPY 开头的任何行。

另一种方法是使用分支。这看起来会复杂得多,但更灵活。

Since I have no sed available to me at the moment, and have never actually used grouping, this command may or may not work (at all, or as intended) =)

Try

sed -r '/^COPY /{ s/([( ])(\w+)([,)])/\1"\U\2\E"\3/g }'

If I understand the manual correctly, this will execute the substitution on any line starting with COPY.

Another approach would be to use branching. This would look a lot more complicated, but is more flexible.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文