How to apply AWK to a directory path?

I have this command:

Folder=./GAS/NNN_/Neutral
awk -F / '{print $(NF-1)}' $Folder

But I got: awk: read error (Is a directory)

I have tried:

awk -F / '{print $(NF-1)}' $(Folder)
awk -F / '{print $(NF-1)}' $($Folder)
awk -F / '{print $(NF-1)}' $(echo $Folder)

And I got the same response.

Asked By: Another.Chemist

||

The syntax of awk is either awk 'awk script' input_file or input_stream | awk 'awk script'. You are trying to pass the variable as a file name and since the variable’s value is a directory, awk tries to open that directory as a file and fails.

So, you either need to print out the value and then pass that to awk (see Why is printf better than echo? for why I am using printf here):

$ printf '%sn' "$Folder" | awk -F / '{print $(NF-1)}'
NNN_

Or you can use a here string:

$ awk -F / '{print $(NF-1)}' <<< "$Folder"
NNN_
Answered By: terdon
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.