Created
November 25, 2015 22:18
-
-
Save idStar/8423444fd36dced71959 to your computer and use it in GitHub Desktop.
How to find the value for a given key, in a particular file. The notion of "key" and "value" are a bit loose. See the documentation.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# In this example, we demonstrate how to find the value for a given key, in a particular file, | |
# using the Bash Shell Scripting language. We've setup some primitive functions to provide | |
# some modularity. See near the end for example usage on how you might call this. | |
# | |
# Assumptions: | |
# 1. Stripping leading and trailing whitespace in the value string is desired. | |
# 2. The last occurrence of the key (if there are multiple) is all we care about. | |
# This function strips out leading and trailing whitespace. | |
# It is adapted from the excellent examples found at: http://stackoverflow.com/a/3232433/535054 | |
# Make sure you encase the value to be stripped in double quotes at the call site, incase | |
# you have a phrase whose intermediate spaces you do not wish to be stripped. | |
# | |
# @param $1 The string whose leading and trailing spaces you wish to be stripped. | |
# | |
# @return The stripped version of the passed in string. | |
strip_external_whitespace() { | |
# Retrieve parameters: | |
local string_to_strip=$1 | |
stripped_version="$(echo -e "${string_to_strip}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')" | |
echo ${stripped_version} | |
} | |
# Finds a value associated with a specified key, in the specified file. This function | |
# assumes that there is only once instance of the string to be found. | |
# | |
# @param $1 The path and name to the file we'll search within. | |
# @param $2 The string representing the "key" we are searching for. | |
# @param $3 The string representing the delimiter between the key and the value. | |
# | |
# @return The value associated with the specified key, stripped of leading and trailing whitespace. | |
find_value_for_key_in_file() { | |
# Retrieve parameters: | |
local file_path=$1 | |
local provided_key=$2 | |
local provided_delimiter=$3 | |
retrieved_value_raw=`egrep "${provided_key}" ${file_path} | tail -1 | cut -f 2 -d ${provided_delimiter}` | |
retrieved_value=`strip_external_whitespace "${retrieved_value_raw}"` | |
echo ${retrieved_value} | |
} | |
# --- Example Usage --- | |
# | |
# Let's say you have a text file on disk with the line: | |
# | |
# Build Configuration: Debug Level 1 | |
# | |
# You want to treat the word 'Build Configuration' as a key, the colon as a delimeter, | |
# and you thus, want to retrieve the value string 'Debug Level 1'. | |
file_path=~/MyTestFile.txt | |
provided_key="Build Configuration" | |
provided_delimiter=":" | |
echo `find_value_for_key_in_file "${file_path}" "${provided_key}" "${provided_delimiter}"` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment