简体   繁体   中英

How to pass a bash variable into awk

I have a variable that is created in bash, and want to use it within awk statement to create a file by concatenating it with a string for the filename.

awk -v value="${index}" 'BEGIN{}{print $9 >> "example_value.txt";}END{}'

How can I do this?

You have to use it outside of the double quotes. concatenates it without any other character:

awk -v value="${index}" 'BEGIN{}{print $9 >> "example_" value ".txt";}END{}'

You could use -v option as in the examples previously posted.

Or you could just use something like :

awk 'BEGIN{}{print $9 >> "example_'"$index"'.txt";}END{}'

Moreover, you don't have to use {} around the variable index in this case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM