【发布时间】:2022-01-14 10:14:58
【问题描述】:
我有一个输入文件
Werkzeug==2.0.2 # https://github.com/pallets/werkzeug
ipdb==0.13.9 # https://github.com/gotcha/ipdb
psycopg2==2.9.1 # https://github.com/psycopg/psycopg2
watchgod==0.7 # https://github.com/samuelcolvin/watchgod
# Testing
# ------------------------------------------------------------------------------
mypy==0.910 # https://github.com/python/mypy
django-stubs==1.8.0 # https://github.com/typeddjango/django-stubs
pytest==6.2.5 # https://github.com/pytest-dev/pytest
pytest-sugar==0.9.4 # https://github.com/Frozenball/pytest-sugar
djangorestframework-stubs==1.4.0 # https://github.com/typeddjango/djangorestframework-stubs
# Documentation
# ------------------------------------------------------------------------------
sphinx==4.2.0 # https://github.com/sphinx-doc/sphinx
sphinx-autobuild==2021.3.14 # https://github.com/GaretJax/sphinx-autobuild
# Code quality
# ------------------------------------------------------------------------------
flake8==3.9.2 # https://github.com/PyCQA/flake8
flake8-isort==4.0.0 # https://github.com/gforcada/flake8-isort
coverage==6.0.2 # https://github.com/nedbat/coveragepy
black==21.9b0 # https://github.com/psf/black
pylint-django==2.4.4 # https://github.com/PyCQA/pylint-django
pylint-celery==0.3 # https://github.com/PyCQA/pylint-celery
pre-commit==2.15.0 # https://github.com/pre-commit/pre-commit
# Django
# ------------------------------------------------------------------------------
factory-boy==3.2.0 # https://github.com/FactoryBoy/factory_boy
django-debug-toolbar==3.2.2 # https://github.com/jazzband/django-debug-toolbar
django-extensions==3.1.3 # https://github.com/django-extensions/django-extensions
django-coverage-plugin==2.0.1 # https://github.com/nedbat/django_coverage_plugin
pytest-django==4.4.0 # https://github.com/pytest-dev/pytest-django
我正在尝试使用此命令为以pytest 开头的每一行提取# 之前的部分
sed -nE "s/(^pytest.+)#/\1/p" ./requirements/local.txt
预期输出
pytest==6.2.5
pytest-sugar==0.9.4
pytest-django==4.4.0
实际输出
pytest==6.2.5 https://github.com/pytest-dev/pytest
pytest-sugar==0.9.4 https://github.com/Frozenball/pytest-sugar
pytest-django==4.4.0 https://github.com/pytest-dev/pytest-django
对达到预期有什么帮助吗?
这些参考并没有帮助解决这个特殊问题
【问题讨论】:
-
您只匹配到#。在它之后没有任何内容是匹配文本的一部分,因此没有更改并因此打印出来......简单的解决方法是将 # 之后的所有内容也包含在您的 RE 中。
-
对!更改为
sed -nE "s/(^pytest.+)#.*/\1/p" ./requirements/local.txt解决了这个问题。谢谢 -
更改为
sed -nE "s/(^pytest.+)#.*/\1/p"可能已经解决了这个特定输入文件的问题,但是sed命令仍然会出现问题:当 1) 没有#字符时,2) 有一行中有多个#字符。