Building Your Bash Toolkit: Simplifying Key-Value Extractions with `extract_value`

Sep 18, 2023 by Frederic Hallot | 486 views

CSC bash Tools Bash-Toolkit

https://cylab.be/blog/286/building-your-bash-toolkit-simplifying-key-value-extractions-with-extract-value

In our previous post of the Building Your Bash Toolkit series, we delved deep into the process_args function, a utility that enables our bash scripts to handle both piped and direct inputs effortlessly. With that power in hand, we are set to further simplify some of the common bash scripting tasks.

Recap of the Previous Post

Our previous endeavor was focused on creating a dynamic argument processor named process_args. This function enhances our Bash functions, enabling them to gracefully handle both direct arguments and piped data. By integrating process_args, we moved a step closer to creating a more streamlined Bash scripting experience.

The Problem at Hand

One recurring need in scripting and data handling is the extraction of values based on specific keys, especially in "key:value" type datasets. Manual extraction can be tedious and error-prone. Thus, a function to automate this process is not just helpful but almost essential for efficient scripting.

Introducing extract_value

The extract_value function simplifies the task of extracting values from "key:value" pairs.

  • cut: A Unix command-line utility that is used to extract sections from each line of input. The -d option specifies the delimiter, and -f specifies which field(s) to display. So, cut -d: -f2 will extract the value after the colon : from "key:value" pairs.

  • xargs: A command that reads items from standard input, delimited by blanks or newlines, and executes the command (default is /bin/echo) one or more times with any specified arguments followed by the items read from standard input. In our context, it's being used to trim any leading or trailing white spaces from the extracted value.

With these utilities in mind, let's proceed with the extract_value function:

extract_value() {
    process_args _inner_extract_value "$@"
}
_inner_extract_value() {
       echo "$@" | cut -d: -f2 | xargs
}

Appending to our toolkit:

echo 'extract_value() {
    process_args _inner_extract_value "$@"
}

# adding an alias 

alias ev=extract_value

_inner_extract_value() {
     echo "$@" | cut -d: -f2 | xargs
}' >> ~/bash-tools/01-basic-tools.sh

Demonstrating extract_value in Action

With extract_value ready, let's explore its utility:

# Direct command usage:
extract_value "username : john_doe"
ev "username : john_doe"

# Using piped input:
echo "username : john_doe" | extract_value
echo "username : john_doe" | ev

These examples clearly showcase the versatility and efficiency extract_value brings to our Bash toolkit.

Recap

extract_value is the next building block in our Bash toolkit, streamlining the process of extracting values from "key:value" pairs. With the foundation of process_args and the utility of extract_value, our toolkit grows both in strength and versatility.

Conclusion

Every addition to our Bash toolkit pushes us further towards scripting excellence. The convenience that tools like process_args and extract_value offer demonstrates the potential of efficient and modular scripting. The beauty of Bash lies in its flexibility and power, and with each tool we craft, we tap deeper into that potential.

What's Next?

In our next post, we will dive into a fascinating crossover between Bash and Python, exploring how to invoke Python functions seamlessly from our Bash scripts. With process_args to manage our inputs, this integration promises to be both intuitive and powerful. Stay tuned!

This blog post is licensed under CC BY-SA 4.0