我有一個輸入檔案
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
有什么幫助可以達到預期嗎?
這些裁判沒有幫助解決這個特殊問題
- 如何使用 sed 僅輸出捕獲的組?
- sed 與捕獲組
uj5u.com熱心網友回復:
使用sed:
sed -nE 's/^(pytest[^=]*=[^[:blank:]]*).*/\1/p' file
pytest==6.2.5
pytest-sugar==0.9.4
pytest-django==4.4.0
但是,grep -o解決方案會更簡單:
grep -o '^pytest[^=]*=[^[:blank:]]*' file
pytest==6.2.5
pytest-sugar==0.9.4
pytest-django==4.4.0
解釋:
^pytestpytest:一開始就匹配[^=]*: 匹配 0 個或多個任意字符,除了==: 匹配一個=[^[:blank:]]*: 匹配 0 個或多個非空白字符
uj5u.com熱心網友回復:
您在#. 這應該解決它:
$ sed -nE "s/(^pytest. )#.*/\1/p" ./requirements/local.txt
uj5u.com熱心網友回復:
第一個解決方案:awk你可以嘗試以下。使用match的功能,awk在 GNU 中撰寫和測驗awk應該可以在任何任何地方作業。簡單的解釋是,使用match函式 ofawk匹配正則運算式^pytest[^ ]*以匹配 pytest 的起始值,直到第一次出現空格,并使用substr函式 ofawk。
awk 'match($0,/^pytest[^ ]*/){print substr($0,RSTART,RLENGTH)}' Input_file
第二種解決方案:使用 GNUawk嘗試使用RS它的變數。
awk -v RS='(^|\n)pytest[^ ]*' 'RT{sub(/^\n*/,"",RT);print RT}' Input_file
uj5u.com熱心網友回復:
作為使用的替代方法awk,您還可以將欄位分隔符設定為#前面有可選空格,如果第一列以 pytest 開頭,則列印它
awk -F"[[:blank:]]*#" '/^pytest/ {print $1}' ./requirements/local.txt
輸出
pytest==6.2.5
pytest-sugar==0.9.4
pytest-django==4.4.0
如果#不總是存在,您還可以使匹配更具體以匹配數字,然后列印第一個欄位:
awk '/^pytest[^[:blank:]]*==[0-9] (\.[0-9] )*/ {print $1}' file
uj5u.com熱心網友回復:
使用 sed
$ sed -n '/^pytest/s/#.*//p' input_file
pytest==6.2.5
pytest-sugar==0.9.4
pytest-django==4.4.0
uj5u.com熱心網友回復:
sed單線將是:
sed -e '/^pytest/!d' -e 's/[[:blank:]]*#.*//' file
第一個運算式洗掉不以 開頭的行pytest。第二個洗掉評論部分(包括 之前的空白#),如果有的話。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/411022.html
標籤:
下一篇:停在一行的正則運算式
