1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
|
__zplug::utils::awk::path()
{
local awk_path
local -a awk_paths
local awk variant
# Look up all awk from PATH
for awk_path in ${^path[@]}/{g,n,m,}awk
do
if [[ -x $awk_path ]]; then
awk_paths+=( "$awk_path" )
fi
done
# There is no awk execute file in this PATH
if (( $#awk_paths == 0 )); then
__zplug::log::write::error \
"gawk or nawk is not found"
return 1
fi
# Detect awk variant from available awk list
for awk_path in "${awk_paths[@]}"
do
if ${=awk_path} --version 2>&1 | grep -q "GNU Awk"; then
# GNU Awk
variant="gawk"
awk="$awk_path"
# Use gawk if it's already installed
break
elif ${=awk_path} -Wv 2>&1 | grep -q "mawk"; then
# mawk
variant=${variant:-"mawk"}
echo $awk:$variant
else
# nawk
variant="nawk"
awk="$awk_path"
# Search another variant if awk is nawk
continue
fi
done
if [[ $awk == "" || $variant == "mawk" ]]; then
__zplug::log::write::error \
"gawk or nawk is not found"
return 1
fi
echo "$awk"
}
__zplug::utils::awk::available()
{
local awk_path
__zplug::utils::awk::path \
| read awk_path
# AWK is available
if [[ -n $awk_path ]]; then
return 0
else
return 1
fi
}
__zplug::utils::awk::ltsv()
{
local \
user_awk_script="$1" \
ltsv_awk_script
ltsv_awk_script=$(cat <<-'EOS'
function key(name) {
for (i = 1; i <= NF; i++) {
match($i, ":");
xs[substr($i, 0, RSTART)] = substr($i, RSTART+1);
};
return xs[name":"];
}
EOS
)
awk -F'\t' \
"${ltsv_awk_script} ${user_awk_script}"
}
|