Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

This question already has an answer here:

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?

share|improve this question

marked as duplicate by jasonwryan, Gilles, John WH Smith, slm Feb 9 at 19:39

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1  
awk -F, '{printf "%s ",$1 ; system ("/usr/bin/basename " $2)}' –  Costas Feb 9 at 17:18
    
awk '{gsub(/\/.*\//,"",$1); print}' –  DevNull Feb 9 at 17:20

1 Answer 1

POSIXly:

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.