3

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.

1 Answers1

6

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 '%s\n' "$Folder" | awk -F / '{print $(NF-1)}'
NNN_

Or you can use a here string:

$ awk -F / '{print $(NF-1)}' <<< "$Folder"
NNN_
terdon
  • 100,812