0

I am trying to match a pattern and set that as a variable.

I have a file with many "value=key". I want to find the value for key "fizz". In the file I have this string

fizz="something_cool"

I try to parse it as:

cat file | grep fizz="(.*)"

I was thinking it would give me the group output, and then I would be able to use $1 to select it.

I also play with escaping characters and sed and awk. But I could not manage to get it working.

Developer
  • 917
  • 2
  • 9
  • 25

1 Answers1

1

You need to enable extended regex for using unescaped ( and ) and quote pattern properly to make it:

grep -E 'fizz="(.*)"' file

However awk might be better choice here since it will do both search and filter in same command.

You may just use:

awk -F= '$1 == "fizz" {gsub(/"/, "", $2); print $2}' file

something_cool
anubhava
  • 761,203
  • 64
  • 569
  • 643