Sponsored Content
Top Forums Shell Programming and Scripting [TIP] Processing YAML files with yq Post 303040237 by stomp on Friday 25th of October 2019 08:37:48 AM
Old 10-25-2019
[TIP] Processing YAML files with yq

After the success of the jq - tool for parsing and manipulating JSON-Data someone wrote a tool called yq, which aims to be the same for YAML, what jq is for JSON. Seems to work fine. I'll definitely give it a chance in future.

Example YAML-File:

Code:
--- !ruby/object:Puppet::Node::Facts
  values:
    processors:
      models:
        - "Intel(R) Xeon(R) CPU           E5620  @ 2.40GHz"
      count: 1
      physicalcount: 1
    kernel: Linux
    architecture: x86_64
    hardwaremodel: x86_64
    operatingsystem: CentOS
    os:
      name: CentOS
      family: RedHat
      release:
        major: "6"
        minor: "10"
        full: "6.10"
    processor0: "Intel(R) Xeon(R) CPU           E5620  @ 2.40GHz"

yq - Usage:

Code:
# Command
yq -r .values.os.family data.yaml

# Output
RedHat

It's already available via repository in newer versions of major linux distributions

See also:
Yq

Last edited by stomp; 10-25-2019 at 12:00 PM..
These 6 Users Gave Thanks to stomp For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Processing files within a directory one by one

Hi How to create a shell script which takes in to account all the files present within a directory DIR one by one e.g. suppose i have a directory named DIR where there are files with the extension .ABC i want to create shell script which processes all these files one by one. ... (1 Reply)
Discussion started by: skyineyes
1 Replies

2. Shell Programming and Scripting

Two files processing

I have the following two files: 1st File: 1:1:100 2:101:400 3:401:450 4:451:600 5:601:980 6:981:1500 7:1501:1600 8:1601:1800 9:1801:2000 2nd File: 30 50 80 700 (2 Replies)
Discussion started by: moutaz1983
2 Replies

3. Shell Programming and Scripting

Processing files

Hi I have the folowing input file, the file looks like below sftp> . .. archive x001_ameint*.zip x001_ameint_1.zip x001_ameint_2.zip x001_REPORTS*.zip x001_REPORTS_1.zip sftp> I want my output to look like this x001_ameint*.zip x001_ameint_1.zip x001_ameint_2.zip (1 Reply)
Discussion started by: ramky79
1 Replies

4. Programming

problems iterating in RUBY while extracting info from YAML, Pls help!

Hi all, I am stuck with a ruby script that extracts detials from yaml file and processes accordingly. the yaml file confivnic: device: vnic1: policy: - L2 mode: active vnic2: policy: - L3 - L4 mode: active type: aggr ... (1 Reply)
Discussion started by: wrapster
1 Replies

5. Shell Programming and Scripting

get all files from a directory and pass the files for processing

Hi All, I have a directory in which there will be several files. i want to get all the files and pass it to a piece of code for processing on the files. This is the piece of code which does the processing. tr "\n" "|" < (log file name) | tr "$" "\n" > output echo ' ' >>output while... (1 Reply)
Discussion started by: suresh_kb211
1 Replies

6. Shell Programming and Scripting

Processing files using awk

Hi I have files in our UNIX directory like the below -rw-r--r-- 1 devinfo devsupp 872 Sep 14 02:09 IMGBTREE27309_12272_11_1_0_FK.idx0 -rw-r--r-- 1 devinfo devsupp 872 Sep 14 02:09 IMGBTREE27309_12272_11_0_0_PK.idx0 -rw-r--r-- 1 devinfo devsupp 432 Sep 14... (7 Replies)
Discussion started by: rbmuruga
7 Replies

7. UNIX for Dummies Questions & Answers

Cygwin Errors: Yaml Configuration issues

Hi everyone! Hope all of you will be fine. I am new to this forum. Well, I am trying to configure yaml-0.1.4 in cygwin on Windows 7. I have written the following command: *******/tmp/yaml-0.1.4 $ ./configure --prefix=/usr && make && make install and the following error appeared. ... (3 Replies)
Discussion started by: imdalucky1
3 Replies

8. Shell Programming and Scripting

Processing of log files

Hi, I have typical logs file something of these formats - fn2013.12.13.log fn2013.12.13_a.log fn2013.12.13_b.log suffix part is after the underscore ( ie a.log or b.log ) I need to process the files in ascending date order, but descending suffix order, and check if the file exist and... (12 Replies)
Discussion started by: scott_apc
12 Replies

9. Shell Programming and Scripting

YAML file update

removing post since it got fix (1 Reply)
Discussion started by: kishore150
1 Replies

10. UNIX for Beginners Questions & Answers

[Tip] Housekeeping Tasks Made Easy - User Home directories and Leftover Files

We have regularly questions about how to create users and user accounts. But regularly user accounts need to be deleted too. It is quite easy to delete the user account itself but usually the HOME directory of the user remains. It is good style to remove these directories but simply deleting... (3 Replies)
Discussion started by: bakunin
3 Replies
JSON_XS(1p)						User Contributed Perl Documentation					       JSON_XS(1p)

NAME
json_xs - JSON::XS commandline utility SYNOPSIS
json_xs [-v] [-f inputformat] [-t outputformat] DESCRIPTION
json_xs converts between some input and output formats (one of them is JSON). The default input format is "json" and the default output format is "json-pretty". OPTIONS
-v Be slightly more verbose. -f fromformat Read a file in the given format from STDIN. "fromformat" can be one of: json - a json text encoded, either utf-8, utf16-be/le, utf32-be/le storable - a Storable frozen value storable-file - a Storable file (Storable has two incompatible formats) clzf - Compress::LZF format (requires that module to be installed) yaml - YAML (avoid at all costs, requires the YAML module :) eval - evaluate the given code as (non-utf-8) Perl, basically the reverse of "-t dump" -t toformat Write the file in the given format to STDOUT. "toformat" can be one of: json, json-utf-8 - json, utf-8 encoded json-pretty - as above, but pretty-printed json-utf-16le, json-utf-16be - little endian/big endian utf-16 json-utf-32le, json-utf-32be - little endian/big endian utf-32 storable - a Storable frozen value in network format storable-file - a Storable file in network format (Storable has two incompatible formats) clzf - Compress::LZF format yaml - YAML dump - Data::Dump dumper - Data::Dumper Note that Data::Dumper doesn't handle self-referential data structures correctly - use "dump" instead. EXAMPLES
json_xs -t null <isitreally.json "JSON Lint" - tries to parse the file isitreally.json as JSON - if it is valid JSON, the command outputs nothing, otherwise it will print an error message and exit with non-zero exit status. <src.json json_xs >pretty.json Prettify the JSON file src.json to dst.json. json_xs -f storable-file <file Read the serialised Storable file file and print a human-readable JSON version of it to STDOUT. json_xs -f storable-file -t yaml <file Same as above, but write YAML instead (not using JSON at all :) lwp-request http://cpantesters.perl.org/show/JSON-XS.json | json_xs Fetch the cpan-testers result summary "JSON::XS" and pretty-print it. AUTHOR
Copyright (C) 2008 Marc Lehmann <json@schmorp.de> perl v5.14.2 2010-08-17 JSON_XS(1p)
All times are GMT -4. The time now is 03:50 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy