Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Regex to extract multiple words from a paragraph

$ echo file.txt

NAME="Ubuntu"                           <--- some of this
VERSION="20.04.4 LTS (Focal Fossa)"     <--- and some of this
ID=ubuntu
ID_LIKE=debian
VERSION_ID="20.04"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=focal

I want this: Ubuntu 20.04.4 LTS.

I managed with two commands:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

echo "$(grep '^NAME="' ./file.txt | sed -E 's/NAME="(.*)"/\1/') $(grep '^VERSION="' ./file.txt | sed -E 's/VERSION="(.*) \(.*"/\1/')"

How could I simplify this to one command using grep/sed or perl?

>Solution :

With your shown samples try following awk code.

awk -F'"' '/^NAME="/{name=$2;next} /^VERSION="/{print name,$2}' Input_file

Explanation:

  • Setting field separator as " for all the lines here.
  • Checking condition if line starts with Name= then create variable name which has 2nd field. next will skip all further statements from there of awk program, they needed not to be executed.
  • Then checking if a line starts from VERSION= then print name and 2nd field here as per requirement.
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading