I have an awk script and I have passed a CSV file to it.awk -f script.awk /home/abc/imp/asgd.csvWhat am I doing is to get FILENAME within script.awk. FILENAME gives me the whole path. As I am in awk I cannot use basename FILENAME.print FILENAME;/home/abc/imp/asgd.csvI have tried with this within script.awkecho $FILENAME | awk -F/ '{print $NF}'but I cannot execute this within script.awk. How can I get asgd.csv within an awk program?
Extract file name from path in awk program
awk;filenames
Several options:awk '  function basename(file) {    sub(.*/, , file)    return file  }  {print FILENAME, basename(FILENAME)}' /path/to/fileOr:awk '  function basename(file, a, n) {    n = split(file, a, /)    return a[n]  }  {print FILENAME, basename(FILENAME)}' /path/to/fileNote that those implementations of basename should work for the common cases, but not in corner cases like basename /path/to/x/// where they return the empty string instead of x or / where they return the empty string instead of /, though for regular files, that should not happen.The first one will not work properly if the file paths (up to the last /) contain sequences of bytes that don't form valid characters in the current locale (typically this kind of thing happens in UTF-8 locales with filenames encoded in some 8 bit single byte character set). You can work around that by fixing the locale to C where every sequence of byte form valid characters.