sách gpt4 ai đã đi

用于匹配相对路径文件名的正则表达式和 sed 命令

In lại 作者:行者123 更新时间:2023-11-29 09:44:41 25 4
mua khóa gpt4 Nike

我正在努力创建一个 sed 正则表达式命令来更改这样的行:

In file included from dira/file_a.h:8, dire/file_e.h:9, and dirf/file_f.h:10,
from dirb/file_b.h:6,
from /existing/abs/path/dirb/file_b.cc:6:
dirc/file_c.h:88: error: 'eqn_count_t' does not name a type
dirc/file_c.h:95: error: 'wave_count_t' does not name a type
dirc/file_c.h:104: error: ISO C++ forbids declaration of 'WmHyperbolicEqnSet' with no type

为此期望的输出:

In file included from /abspaths/dira/file_a.h:8, /abspaths/dire/file_e.h:9, and /abspaths/dirf/file_f.h:10,
from /abspaths/dirb/file_b.h:6,
from /existing/abs/path/dirb/file_b.cc:6:
/abspaths/dirc/file_c.h:88: error: 'eqn_count_t' does not name a type
/abspaths/dirc/file_c.h:95: error: 'wave_count_t' does not name a type
/abspaths/dirc/file_c.h:104: error: ISO C++ forbids declaration of 'WmHyperbolicEqnSet' with no type

所以,

  • 只匹配以.h结尾的相对路径+文件名
  • 不匹配以正斜杠开头的行(因此已经是绝对路径)
  • 每行匹配多次出现
  • 很明显,我需要一个与 Mac OS X 的 BSD sed 命令一起工作的命令。

我想要的正则表达式和 sed 命令是什么?

我正在尝试修改 gcc 输出,因为包含的带有错误/警告的头文件会生成错误流输出,其中引用的是相对路径,而不是绝对路径。在我的 XCode IDE 调用外部构建系统时,.h 文件中发生的错误不可“点击”。

1 Câu trả lời

上次编辑

我终于成功地创建了一个适用于两者的命令,只有一个命令:

sed 's/^\(.* \)\{0,1\}\([^/ ][^ ]\{1,99\}\.h\)/\1\/abspath\/\2/;' testfile.txt

并且为了支持多重匹配...甚至在 Mac 上也是如此(来自@sudo_O 的一些简化想法):

sed -E -e :a -e 's/^(.* )?([^/][^ ]+\.h)/\1\/abspath\/\2/' -e ta testfile.txt 

但是 Mac 的 sed 实现不支持 ; 作为命令分隔符。所以我们需要使用多个-e命令字段。

为了测试它们,我使用以下命令修改了 textfile.txt:

sed -e '4s/^.*$/& &/' -i.bak testfile.txt

(也适用于 Mac)

有两部分,首先可以计算任何字符串,但以空格结尾... 0 次或 1 次。第二部分不得以空格或斜杠开头,可以包含任何内容但不能包含空格,并且必须以 .h 结尾。如果匹配,第一部分(包含前导空格,但如果出现 0 次则可能为空)必须后跟 /abspath/,而不是第二部分。

年长

这行不通吗?

biên tập 针对 Mac 修改:

sed 's/ \([^/ ][^ ]\{1,99\}\.h\)/ \/abspath\/\1/;' testfile.txt

在 Mac 和 Linux 上工作相同。我已将 + 替换为 {1,99}.

抱歉,我没有正确阅读问题。这对两者都很好:

sed 's/^\([^/ ][^ ]\{1,99\}\.h\)/\/abspath\/\1/;
s/ \([^/ ][^ ]\{1,99\}\.h\)/ \/abspath\/\1/;' testfile.txt

关于用于匹配相对路径文件名的正则表达式和 sed 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14095269/

25 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com