tengo un archivo de entrada
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
y estoy tratando de extraer las partes antes del #
para cada línea que comienza con pytest
usando este comando
sed -nE "s/(^pytest.+)#/\1/p" ./requirements/local.txt
Rendimiento esperado
pytest==6.2.5 pytest-sugar==0.9.4 pytest-django==4.4.0
Salida real
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
¿Alguna ayuda para conseguir lo esperado?
Estos árbitros no han ayudado a resolver este problema en particular.
Usando sed
:
sed -nE 's/^(pytest[^=]*=[^[:blank:]]*).*/\1/p' file pytest==6.2.5 pytest-sugar==0.9.4 pytest-django==4.4.0
Sin embargo, una solución grep -o
sería aún más simple:
grep -o '^pytest[^=]*=[^[:blank:]]*' file pytest==6.2.5 pytest-sugar==0.9.4 pytest-django==4.4.0
Explicación:
^pytest
: coincide con pytest
al principio[^=]*
: coincide con 0 o más de cualquier carácter excepto =
=
: Coincide con un =
[^[:blank:]]*
: coincide con 0 o más caracteres que no sean espacios en blancoTe falta la expresión regular después de #
. Esto debería resolverlo:
$ sed -nE "s/(^pytest.+)#.*/\1/p" ./requirements/local.txt
Primera solución: con awk
podría intentar seguir. Usando la función de match
de awk
aquí, escrita y probada en GNU, awk
debería funcionar en cualquiera. La explicación simple sería usar la función de match
de awk
para hacer coincidir la expresión regular ^pytest[^ ]*
para hacer coincidir el valor inicial de pytest hasta la primera aparición de espacio e imprimir el valor coincidente usando la función substr
de awk
.
awk 'match($0,/^pytest[^ ]*/){print substr($0,RSTART,RLENGTH)}' Input_file
Segunda solución: usando GNU awk
, intente seguir donde hacer uso de la variable RS
.
awk -v RS='(^|\n)pytest[^ ]*' 'RT{sub(/^\n*/,"",RT);print RT}' Input_file