Monday, November 1, 2021

[SOLVED] Regex After Two Points

Issue

After outputting lsb_release -a in Linux, I just want to print the distributor ID on the screen. Example output:

No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 20.04.1 LTS
Release:    20.04
Codename:   focal

I just want to print the distributor id, that is, just the text "Ubuntu". I need to do this in bash, just with sed or grep. How can I do it ?


Solution

Pure bash, no external programs needed (Aside from lsb_release, of course):

#!/usr/bin/env bash
re="^Distributor ID:[[:space:]]+(.*)"
while IFS= read -r line; do
    if [[ $line =~ $re ]]; then
        echo "${BASH_REMATCH[1]}"
    fi
done < <(lsb_release -a 2>/dev/null)


Answered By - Shawn