0

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?

2
  • What I want is to change value with index to create multiple files such as example_1.txt, example_2.txt, etc. Commented Jan 5, 2014 at 13:11
  • 1
    This question is very common.. Strange that these type of things cannot be googled :) There must be at least 100 similar questions.. Commented Jan 5, 2014 at 13:41

2 Answers 2

4

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{}'
Sign up to request clarification or add additional context in comments.

Comments

2

You could use -voption 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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.