Monday, April 4, 2022

[SOLVED] Extract string following pattern (grep)

Issue

I am trying to learn how to use grep. I have a file that list my python packages as follow:

channels:
  - conda-forge
  - defaults
dependencies:
  - numpy=1.21.1=py39h6635163_0
  - pyinstaller=4.2=py39h4dafc3f_1
  - ...

I am only interested what comes after "dependancies". I am trying, using bash grep/sed/awk whatever basic linux tool, to iterate in all those lines, to save in one variable the python package and in another variable the version (I dont care about what is after the last =) and to call a function.

Example for the first line:

$ > echo $1 $2
numpy 1.21.1

Thans you for your help


Solution

using awk:

awk '$1 == "-"{ if (key == "dependencies:") print $NF; next } {key=$1}' file
numpy=1.21.1=py39h6635163_0
pyinstaller=4.2=py39h4dafc3f_1
...

Ed Morton's code: https://stackoverflow.com/a/66188243/14259465



Answered By - Carlos Pascual
Answer Checked By - Cary Denson (WPSolving Admin)