如何使用单个sed命令移除Python元数据文件中的依赖版本信息
解决方案
可以使用这条单个sed命令完成需求:
sed -E 's/(Requires-Dist: [^ (]+)(.*)/\1/' your_metadata_file.txt
命令解释
-E:启用扩展正则表达式语法,让括号等元字符无需额外转义,简化正则写法- 正则匹配逻辑:
(Requires-Dist: [^ (]+):捕获Requires-Dist:开头,加上后续所有非空格、非左括号的字符(这部分就是依赖库的名称,比如hello、hello-world)(.*):捕获库名之后的所有内容(包括版本号、括号、分号条件等)\1:替换为第一个捕获组的内容,也就是只保留依赖库名称部分,去掉后面的版本和附加条件
输入示例
Requires-Python: >=3 Requires-Dist: hello (~=0.1.1) Requires-Dist: bar (~=1.14) Requires-Dist: hello-world (~=1.10) Requires-Dist: test (~=1.1) ; python_version < "3.4" Requires-Dist: test-bar ...
输出结果
Requires-Python: >=3 Requires-Dist: hello Requires-Dist: bar Requires-Dist: hello-world Requires-Dist: test Requires-Dist: test-bar ...
内容的提问来源于stack exchange,提问作者stackbiz
相关产品推荐
相关产品推荐

