Skip to content

Instantly share code, notes, and snippets.

@jazzsequence
Last active Dec 8, 2022
Embed
What would you like to do?
ChatGPT get_field bash function
------------------ ----------------------------------------------
ID 1234d12a-1d12-12b1-1234-123e1234a123
Name some-misc-slug
Label Some Misc Name
Created 2022-11-29 21:45:09
Framework wordpress
Region United States
Repository 12b123ab-0e12-34dc-a56d-fd7890bb1234:
https://github.com/jazzsequence/jazzsequence.github.io.git
------------------ ----------------------------------------------
get_field() {
# $1: field name
# $2: input string
echo "$2" | awk -v field="$1" '$1 == field { print $2 }'
}
get_field() {
local input="$1"
# Remove leading and trailing whitespace from each line
input="$(echo "$input" | sed -e 's/^[ \t]*//')"
# Remove the first and last lines that are entirely dashes
input="$(echo "$input" | sed -e '1d' -e '$d')"
# $1: field name
# $2: input string
echo "$2" | awk -v field="$1" '$1 == field { print $2 }'
}
# Extract the ID and Name fields
id=$(get_field "ID" "$input")
name=$(get_field "Name" "$input")
# Print the extracted values
echo "ID: $id"
echo "Name: $name"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment