Home > Software engineering >  How can I use a variable as input in a awk command?
How can I use a variable as input in a awk command?

Time:01-20

I have this variable:

a='/08/OPT/imaginary/N/08_i_N.out'

I want to use "/" as a field separator.

Then, I want to extract the first pattern.

I have tried:

awk -F/ '{print $1}' "$a"

But I get:

awk: cannot open /08/OPT/imaginary/N/08_i_N.out (No such file or directory)

I do not want the file, only to work on the path of that file.

CodePudding user response:

Same way as any other command, either of these (or other alternatives, e.g. within "here-documents" or passed as awk variables or...):

printf '%s\n' "$a" | command
command <<<"$a"
  •  Tags:  
  • Related