37

I have a database.yml file like

development:
  adapter: mysql2
  encoding: utf8
  database: d360
  host: localhost
  username: root
  password: password

test:
  adapter: mysql2
  encoding: utf8
  database: sample
  host: localhost
  username: root
  password: password

Now, I want value of database of test environment (that is sample for the YAML shown). How can we do that using sed?

5 Answers 5

28

There are better tools than sed. You might be stuck on a minimal system, but other people looking for an answer probably won't be.

The python version of yq acts like jq:

$ pip3 install yq
$ yq -r .test.database database.yml
sample

shyaml works too, but has an odd restriction that you can only redirect into it:

$ pip3 install shyaml
$ shyaml get-value test.database < database.yml
sample

If perl is around and you have YAML installed:

$ cpan YAML
$ perl -MYAML -le 'print YAML::LoadFile(shift)->{test}{database}' database.yml
sample

Or, if you want ruby:

$ ruby -r yaml -e 'puts YAML.load_file(ARGV[0])["test"]["database"]' database.yml
sample
0
26

That is fairly easy, not using sed, but with appropriate shell tools. First, if you need to preserve sample in a variable for later use, then something like the following will work using bash substring replacement to isolate sample on the Test:/database: line:

$ db=$(grep -A3 'test:' database.yml | tail -n1); db=${db//*database: /}; echo "$db"
sample

or for a shorter solution that you can dump to the command line, remove the variable and command substitution and use a tool like cut:

$ grep -A3 'test:' database.yml | tail -n1 | cut -c 13-
sample

or, with awk, simply:

$ grep -A3 'test:' database.yml | tail -n1 | awk '{ print $2}'
sample

All of the different ways can be used inside command substitution (i.e. var=$(stuff) ) to store sample in var, it is just a matter of which you would rather use. I think you get the idea.

3
  • I've seen this before db=${db//*database: /}, but what/how do we call this?
    – bretonics
    Jan 16, 2019 at 18:24
  • 1
    @bretonics - that is a parameter expansion with substring replacement. It takes the existing variable db in =${db then will replace every occurrence // of *database: with /... (nothing). (note the closing '/' is implied). So you are simply removing every occurrence of *database: from db and assigning the modified result back to db. Jan 16, 2019 at 23:00
  • 2
    This type of code assumes you know the format of the input file... if that's the case why not grep -m2 database | tail -1 | awk '{print $2}'?
    – qneill
    Jul 3, 2019 at 15:29
13

On Ubuntu, install yq:

$ sudo apt update
$ sudo apt install snapd
$ sudo snap install yq

Use yq:

$ value=$(yq r database.yml test.database)`
1
  • There is a difference in usage between the snap and the Python package. snap version(4.6.0): yq .test.database database.yml -j python version(2.12.0): yq eval .test.database database.yml Mar 5, 2021 at 16:02
8

YAML obviously isn't natively supported in shell. But sed also isn't a YAML parser. Neither are grep, tail, cut, etc. I'm pretty sure you'd need an actual language in which to write a proper parser. Awk, perl, python, erlang, etc.

You could use sed or shell or somesuch if you know that your YAML data will always be formatted in a certain way or hold certain values in a particular order, but to make a parser that will work generally, and on any ol' YAML file you point it at, you'll need to emulate the parsers that already exist in other languages.

If you don't want to write your own (perhaps asking the StackOverflow community for help once you have some code to consider), then one option might be to install a tool like shyaml which can accurately parse files and give you authoritative content. A quick solution easily implemented into your scripts, though it has some dependencies (python).

1
  • 2
    This is a better answer that will lead to better solutions (in that they will break less when the yaml file changes... and believe me the yaml file will change).
    – qneill
    Jul 3, 2019 at 15:32
3

For macOS / Linux users using Homebrew, yq is available, and provides a jq-like expeience:

$ brew install yq
$ yq eval .test.database database.yml
sample

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.