I want to run the basename command on a certain awk field.
echo "1 /this/is/a/path" | awk '{print $1" "system("/usr/bin/basename " $2)}'
but the output always produces a 0 from the system command. How do I print the real output?
POSIXly:
$ echo "1 /this/is/a/path" | awk '
{
cmd = "/usr/bin/basename -- " $2;
cmd | getline out;
print $1, out;
close(cmd);
}'
1 path
awk -F, '{printf "%s ",$1 ; system ("/usr/bin/basename " $2)}'
– Costas Feb 09 '15 at 17:18awk '{gsub(/\/.*\//,"",$1); print}'
– devnull Feb 09 '15 at 17:20awk
variable from a parameter. This one is concerned with getting output from a shell command executed within anawk
script – Chris Davies Sep 15 '16 at 10:00