2

The following works as expected.

awk -F'^' '{printf "set %s:%s %s\n",$1,$2, $7}' todel.txt | sed 's/$/\\r\\n/' >> tofile.txt

But when I try to add a variable to output as shown below, I get an error:

awk -F'^' '{printf "set %s:%s:%s %s\n",$1,$2,$myvar $7}' todel.txt | sed 's/$/\\r\\n/' >> tofile.txt

1 Answer 1

5

$myvar doesn't expand in single quote.
You can use the -v option to pass shell variable to awk:

awk -F'^' -v myvar=$myvar '{printf "set %s:%s:%s %s\n",$1,$2,myvar,$7}' todel.txt
Sign up to request clarification or add additional context in comments.

4 Comments

awk: (FILENAME=todel.txt FNR=1) fatal: not enough arguments to satisfy format string # does not seem to work as expected.
I added the missing comma. I'm not at my computer so cannot test. )-:
Yes, I haven't test the missing comma too.
+1 Best to use quotes for the -v argument to protect whitespace awk -v "myvar=$myvar" ...

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.