4

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?

Sten Kin
  • 391

1 Answers1

5

POSIXly:

$ echo "1 /this/is/a/path" | awk '
{
  cmd = "/usr/bin/basename -- " $2;
  cmd | getline out; 
  print $1, out;
  close(cmd);
}'
1 path
cuonglm
  • 153,898