Results 1 to 2 of 2
|
Enjoy an ad free experience by logging in. Not a member yet? Register.
|
|
-
03-11-2013 #1
- Join Date
- Feb 2013
- Posts
- 19
awk(ward) question: How does awk handle expansions with bash?
I'm trying to iteratively narrow down a list of values held in the variable $toprint. Each value is seperated by a newline.
I want to remove lines whose fields $3 and $4, together, contain the same value as a variable I defined previously in bash called $seccord.
The best way to do this is obviously to tell awk to print only lines for which the string $seccord does not equal fields $3 and $4 in each line:
Code:toprint=`printf '%b\n' $toprint | awk 'BEGIN { FS="," } { if($3$4!='$seccord') print }'`
I figure it's because of some weirdness in how bash vs. awk expand variables. Does anyone have an idea?
-Deut
EDIT:
I apologize. I got really desperate and experimented with all sorts of quote combinations. This one works:
" ' $seccord ' "
That is, single quotes inside to expand the variable in bash;
Double quotes outside the single quotes to tell awk that it's looking for a string equal to the value of $seccords
You can delete this thread if you want, or if it's a commmon newbie problem you can keep it.Last edited by deuteros; 03-11-2013 at 01:43 AM.
-
03-11-2013 #2
- Join Date
- Mar 2013
- Posts
- 2
You're on the right track, there is no shell expansion in single quotes in bash. Another approach instead of the bash concentration approach is to use awk's variable setting, e.g. -v testCase="${seccord}" ' awk script ...'.
PS: If the issue is solved, please marked is solved.