Parsing Columns From Files WITHOUT awk
# awk is cool - but sometimes jumping from bash to awk to bash gets clunky.
# We don't actually have use awk - we can just leverage bash internal parsing.
# This is a classic awk example but now we have the really useful function map
# which lets us do the same time in bash.
function size_name {
print $5 $9
}
function map {
local l;
while read -r l; do
$1 $l;
done
}
ls -l SonicField/src/cpp/lib/ | map size_name | column -t
168 build.sh
2108 stream.hpp
15676048 stream.hpp_out
# Let's try this as a one liner:
_tmp () { print $5 $9; }; ls -l SonicField/src/cpp/lib/ | map _tmp | column -t
# awk is simpler but not by much if we assume map is part of your utils
ls -l SonicField/src/cpp/lib/ | awk '{print $5, $9}' | column -t
168 build.sh
2108 stream.hpp
15676048 stream.hpp_out
# But remember that awk is a separate process space so you do not
# have access to the bash state in the same way. For example:
function add_size { [[ $5 != '' ]] && ((size+=$5)); }
function count_sizes { size=0; map <&0 add_size; print $size; }
ls -l SonicField/src/cpp/lib/ | count_sizes
15678324
# We don't actually have use awk - we can just leverage bash internal parsing.
# This is a classic awk example but now we have the really useful function map
# which lets us do the same time in bash.
function size_name {
print $5 $9
}
function map {
local l;
while read -r l; do
$1 $l;
done
}
ls -l SonicField/src/cpp/lib/ | map size_name | column -t
168 build.sh
2108 stream.hpp
15676048 stream.hpp_out
# Let's try this as a one liner:
_tmp () { print $5 $9; }; ls -l SonicField/src/cpp/lib/ | map _tmp | column -t
# awk is simpler but not by much if we assume map is part of your utils
ls -l SonicField/src/cpp/lib/ | awk '{print $5, $9}' | column -t
168 build.sh
2108 stream.hpp
15676048 stream.hpp_out
# But remember that awk is a separate process space so you do not
# have access to the bash state in the same way. For example:
function add_size { [[ $5 != '' ]] && ((size+=$5)); }
function count_sizes { size=0; map <&0 add_size; print $size; }
ls -l SonicField/src/cpp/lib/ | count_sizes
15678324
Comments
Post a Comment