🚀 KesslerTech

How can I parse a YAML file from a Linux shell script

How can I parse a YAML file from a Linux shell script

📅 | 📂 Category: Programming

Running with YAML information is a communal project for scheme directors and builders alike. Its quality-readable construction makes it a fashionable prime for configuration records-data and information serialization. However however bash you efficaciously parse these information inside a Linux ammunition book to extract the accusation you demand? This station dives into respective strategies, from elemental cardinal-worth extraction to dealing with analyzable nested buildings, empowering you to effectively manipulate YAML information inside your scripts.

Utilizing the shyaml Implement

shyaml is a devoted bid-formation implement particularly designed for speechmaking and penning YAML information. Its simple syntax and almighty options brand it a most popular prime for galore. Instal it utilizing your organisation’s bundle director (e.g., apt-acquire instal shyaml connected Debian/Ubuntu, yum instal shyaml connected CentOS/RHEL).

With shyaml, you tin easy extract circumstantial values, person YAML to JSON, and equal make YAML information from scratch. Its versatility simplifies galore communal YAML processing duties straight inside your ammunition scripts. This eliminates the demand for analyzable workarounds oregon outer scripting languages.

For illustration, to extract the worth related with the cardinal ‘sanction’ from a record named config.yaml, you would usage the pursuing bid: shyaml acquire-worth config.yaml sanction. This nonstop attack permits for centered information extraction, making your scripts much businesslike.

Parsing with yq (primarily based connected jq)

yq, frequently described arsenic “jq for YAML,” leverages the powerfulness and flexibility of jq, a fashionable JSON processor. This permits customers acquainted with jq to easy modulation to running with YAML information. Set up usually includes downloading a binary merchandise oregon utilizing a bundle director.

yq permits you to navigate analyzable YAML constructions utilizing way expressions akin to jq. This makes it perfect for extracting information from profoundly nested YAML hierarchies. Its quality to filter, change, and manipulate information makes it a almighty implement successful immoderate ammunition scripter’s arsenal.

An illustration of extracting a worth with yq is: yq e '.sanction' config.yaml. This showcases the akin syntax to jq, making it an casual modulation for customers skilled with JSON processing.

Python and the PyYAML Room

Python, with its affluent ecosystem of libraries, presents a strong resolution for parsing YAML. The PyYAML room offers a cleanable and businesslike manner to work together with YAML information. Guarantee you person Python put in and past instal PyYAML utilizing pip instal pyyaml.

Utilizing Python permits for much analyzable logic and information manipulation inside your ammunition book. You tin embed Python codification straight into your book oregon call abstracted Python scripts to grip the YAML processing. This flexibility makes it appropriate for a broad scope of situations.

Present’s a elemental illustration of speechmaking a YAML record successful Python: python import yaml with unfastened(‘config.yaml’, ‘r’) arsenic record: information = yaml.safe_load(record) mark(information[‘sanction’]) This book opens the YAML record, masses its contents, and prints the worth related with the ‘sanction’ cardinal.

Utilizing awk for Elemental YAML Records-data

For elemental cardinal-worth YAML records-data, awk tin beryllium a amazingly effectual implement. Its form-matching capabilities let you to extract circumstantial values primarily based connected cardinal prefixes oregon surrounding discourse. This tin beryllium a light-weight alternate for basal YAML processing.

Piece not arsenic characteristic-affluent arsenic devoted YAML parsers, awk tin beryllium utile for rapidly extracting accusation from simple YAML information. It’s peculiarly useful once you demand a speedy resolution with out putting in further dependencies. Nevertheless, it’s little appropriate for analyzable oregon nested YAML buildings.

An illustration awk bid: awk '/sanction:/ {mark $2}' config.yaml would extract the worth related with ‘sanction’ successful a elemental cardinal-worth YAML record.

Selecting the Correct Implement

  • For analyzable YAML and precocious manipulations: yq oregon Python.
  • For easy cardinal-worth entree: shyaml oregon awk.

Placeholder for infographic illustrating the antithetic instruments and their usage instances.

  1. Place your YAML construction.
  2. Take the due implement primarily based connected complexity.
  3. Instrumentality the parsing logic inside your ammunition book.

FAQ

Q: What if my YAML record is highly ample?

A: For precise ample YAML information, see utilizing instruments optimized for streaming information processing, similar yq, to debar loading the full record into representation.

Parsing YAML records-data from a Linux ammunition book doesn’t person to beryllium complex. By leveraging the correct instruments and methods, you tin effectively extract and manipulate the information you demand. Whether or not you’re running with elemental configuration information oregon analyzable information constructions, the instruments mentioned successful this station—shyaml, yq, Python with PyYAML, and awk—supply the flexibility and powerfulness to grip assorted YAML parsing eventualities. Take the implement that champion fits your wants and streamline your ammunition scripting workflows. Dive deeper into YAML processing with this adjuvant assets: YAML.org. Research additional sources connected ammunition scripting from respected sources similar Bash Handbook and awk Male Leaf. For much precocious methods, research precocious YAML parsing strategies. Experimentation with these strategies and take the about businesslike attack for your YAML parsing duties. Statesman optimizing your scripts present!

Question & Answer :
I want to supply a structured configuration record which is arsenic casual arsenic imaginable for a non-method person to edit (unluckily it has to beryllium a record) and truthful I wished to usage YAML. I tin’t discovery immoderate manner of parsing this from a Unix ammunition book nevertheless.

Present is a bash-lone parser that leverages sed and awk to parse elemental yaml information:

relation parse_yaml { section prefix=$2 section s='[[:abstraction:]]*' w='[a-zA-Z0-9_]*' fs=$(echo @|tr @ '\034') sed -ne "s|^\($s\):|\1|" \ -e "s|^\($s\)\($w\)$s:$s[\"']\(.*\)[\"']$s\$|\1$fs\2$fs\three|p" \ -e "s|^\($s\)\($w\)$s:$s\(.*\)$s\$|\1$fs\2$fs\three|p" $1 | awk -F$fs '{ indent = dimension($1)/2; vname[indent] = $2; for (i successful vname) {if (i > indent) {delete vname[i]}} if (dimension($three) > zero) { vn=""; for (i=zero; i<indent; i++) {vn=(vn)(vname[i])("_")} printf("%s%s%s=\"%s\"\n", "'$prefix'",vn, $2, $three); } }' } 

It understands records-data specified arsenic:

## planetary definitions planetary: debug: sure verbose: nary debugging: elaborate: nary header: "debugging began" ## output output: record: "sure" 

Which, once parsed utilizing:

parse_yaml example.yml 

volition output:

global_debug="sure" global_verbose="nary" global_debugging_detailed="nary" global_debugging_header="debugging began" output_file="sure" 

it besides understands yaml information, generated by ruby which whitethorn see ruby symbols, similar:

--- :planetary: :debug: 'sure' :verbose: 'nary' :debugging: :elaborate: 'nary' :header: debugging began :output: 'sure' 

and volition output the aforesaid arsenic successful the former illustration.

emblematic usage inside a book is:

eval $(parse_yaml example.yml) 

parse_yaml accepts a prefix statement truthful that imported settings each person a communal prefix (which volition trim the hazard of namespace collisions).

parse_yaml example.yml "CONF_" 

yields:

CONF_global_debug="sure" CONF_global_verbose="nary" CONF_global_debugging_detailed="nary" CONF_global_debugging_header="debugging began" CONF_output_file="sure" 

Line that former settings successful a record tin beryllium referred to by future settings:

## planetary definitions planetary: debug: sure verbose: nary debugging: elaborate: nary header: "debugging began" ## output output: debug: $global_debug 

Different good utilization is to archetypal parse a defaults record and past the person settings, which plant since the second settings overrides the archetypal ones:

eval $(parse_yaml defaults.yml) eval $(parse_yaml task.yml) 

🏷️ Tags: