如何使用bash“sed”从需求文件中删除所有版本

问题描述 投票:0回答:1

下面的python需求文件定义了超过500个包,每个包都有一个“

==
”,后面跟着
version
字符串。

# comment string
python_package_a == 1.2.3

# some text
python_package_b == 2.3.4

# test bar
hello == 0.1.2

# many other packages (more than 500 packages)
other_500_packages == x.y.z

我想使用

bash sed
从需求文件中删除所有版本。

这是预期的输出:

# comment string
python_package_a == 

# some text
python_package_b == 

# test bar
hello == 

# many other packages (more than 500 packages)
other_500_packages == 

如何编写“

sed
”命令以仅从所有包行中删除“
version
”?

regex bash sed pypi
1个回答
1
投票

我知道如何使用 sed 搜索替换整行,但我不知道如何保留“==”之前的所有字符串

你不必“保留”,只需匹配该行的一部分即可。只需匹配

==
,然后匹配该行的其余部分。然后将其替换为
==

sed 's/==.*/==/'
© www.soinside.com 2019 - 2024. All rights reserved.