Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I use a variable as input in a awk command?

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading