assigning column name to awk print in a loop
I am trying to assign column number/name to awk print option in a loop. But this is not working here. Can you help
Code:
|
do it inside awk
Code:
echo "$STR" | awk '{for(i=3;i<=13;i+=2) print $i}' |
That works, thanks. However I want to know why my code is not working. Also, since I am interested in extracting values from STR and using them as part of larger string , I want to use an outer loop. Can this be done ?
|
I'll answer your question with a question, who's variable is $val in the following line:
Code:
v1=`echo $STR | awk '{print $val;}'` I will also assume that you were just using the echoes to check the data but are aware that the extra val variable is not required. At least not if it is not to have any life past the awk. Lastly, not understanding what the STR represents, have you thought of just putting it in an array and then using the appropriate indices? (ie no awk required at all) |
This is what I was looking for:
Code:
#!/bin/sh val is not awk's variable. I explored using arrays and that worked too. Thanks for your help! |
I am sure your seq will still work too. Another small piece of advice which is less obvious in a small example like this
but may later be helpful. Try and use awk's variables to encompass the bash ones as later it will get confusing if you have many variables to change in and out of quotes. For example: Code:
#!/bin/sh |
All times are GMT -5. The time now is 09:34 AM. |