-2

I need to count no of services in docker-compose.yaml file, i.e with out apply it, like filtering yaml file using yq or any other method in Linux.

for example my file is

services:
  service-1:
    image: image2

  service-2:
    image: image

  service-3:
    image: image

  service-4:
    image: image
    ports:
      - "80:80"

I want output like services count is 4 and name of each service

Inian
  • 80,270
  • 14
  • 142
  • 161

1 Answers1

1

To get the number of keys inside .services you can use

yq e '.services | length' docker-compose.yaml
4

The keys can be shown with

yq e '.services | keys' docker-compose.yaml
- service-1
- service-2
- service-3
- service-4

Since you did not provide a desired output, I'll just assume you want all the info in 1 command and output as a regular string:

yq e '"Total keys: " + (.services | length), (.services | keys)[]' docker-compose.yaml
Total keys: 4
service-1
service-2
service-3
service-4
0stone0
  • 34,288
  • 4
  • 39
  • 64
  • Your welcome! Hope it helped, if so, please consider reading [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers). – 0stone0 Jan 26 '22 at 16:38
  • thanks for answer and three outputs are very helpfull – prudhvi reddy Feb 14 '22 at 13:25