Skip to content

Instantly share code, notes, and snippets.

@mcsf
Last active September 7, 2020 10:47
Show Gist options
  • Save mcsf/706f979f29aa29f7a9f38eac90fc6938 to your computer and use it in GitHub Desktop.
Save mcsf/706f979f29aa29f7a9f38eac90fc6938 to your computer and use it in GitHub Desktop.
#!/bin/bash
# search term> i
# (partial-name-match) image
# (partial-name-match) instagram
# (partial-keyword-match) gallery
# search term> ig
# (exact-keyword-match) instagram
# search term> im
# (partial-name-match) image
# (partial-keyword-match) gallery
# search term> in
# (partial-name-match) instagram
# search term> li
# (partial-name-match) list
# (partial-keyword-match) revue
# search term> lista
# (exact-name-match) list
# (exact-keyword-match) revue
# search term> foto
# (partial-keyword-match) image
# search term> graph
# search term> paragr
# (partial-name-match) paragraph
# search term>
block_types_path=$(mktemp)
awk_script_path=$(mktemp)
cat > "$block_types_path" <<'EOD'
core/list List Lista ul,ol
core/paragraph Paragraph Parágrafo text,texto
core/image Image Imagem image,picture,fotografia
core/gallery Gallery Galeria image,images
core/instagram Instagram Instagram ig
foo/revue Revue Revista list,lista
EOD
cat > "$awk_script_path" <<'EOD'
# Different AWK implementations provide different regular expressions
# for word boundaries ("\<"), so do this manually.
function open_match(str, pat) {
return tolower(str) ~ "(^|[^[[:alnum:]]])"pat
}
BEGIN {
term = tolower(term)
}
($1 == term || tolower($2) == term || tolower($3) == term) {
exact_name[NR] = $1
}
($4 ~ term) {
split($4, words, ",")
for (i in words) {
if (words[i] == term) {
exact_keyw[NR] = $1
break
}
}
}
(open_match($1, term) || open_match($2, term) || open_match($3, term)) {
partial_name[NR] = $1
}
open_match($4, term) {
partial_keyw[NR] = $1
}
END {
for (i in exact_name) {
print "(exact-name-match) ", exact_name[i]
}
for (i in exact_keyw) {
if (i in exact_name) {
continue
}
print "(exact-keyword-match) ", exact_keyw[i]
}
for (i in partial_name) {
if (i in exact_name || i in exact_keyw) {
continue
}
print "(partial-name-match) ", partial_name[i]
}
for (i in partial_keyw) {
if (i in exact_name || i in exact_keyw || i in partial_name) {
continue
}
print "(partial-keyword-match) ", partial_keyw[i]
}
}
EOD
while read -p "search term> " -r term
do
awk -f "$awk_script_path" -v term="$term" "$block_types_path"
done
rm -f "$block_types_path" "$awk_script_path"
# vim: ts=8 tw=78
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment