user5580578
user5580578

Reputation: 1334

How can I get the values from this file?

How can I grep the values from this file?

[myfile.file]
documentation            My Documentation
test                     testvalue
test for my var          123
test_var2                this is my second var
test_bool                false

For example I want to get the value "123" for the key "test for my var" or the value "this is my second var" for the key "test_var2. Between the keys and the values (in the file) are a lot of spaces. In my bash variable I just want to have the value without these spaces.

My current approach is not working.

#!/bin/bash
value=$(cat /tmp/myfile.file | grep '^test:' | awk '{print $2}')

Upvotes: 1

Views: 52

Answers (1)

anubhava
anubhava

Reputation: 784938

You may use this awk that splits fields on 2+ whitespaces:

awk -v s='test for my var' -F '[[:blank:]]{2,}' '$1 == s {print $2}' file
123

awk -v s='test_var2' -F '[[:blank:]]{2,}' '$1 == s {print $2}' file
this is my second var

Upvotes: 4

Related Questions