Search code examples
greptext-processing

Grep get value from key


I have a Linux command which outputs Category: value I want to extract just the value(using grep ig) I already tried |grep -vo "value" but it doesn't even print the line. An example would be: Given the command:

Category1: 33
Category2: 45
Category3: 234

I would like to only get 234 if I input "Category3: ", 33 if I input "Category1: " and so on


Solution

  • echo 'Category1: 33
    Category2: 45
    Category3: 234' | awk '/^Category/{print $2}'
    33
    45
    234
    

    If the first word on the line starts with Category (ignoring numbers and colons) print the second word.