Find the answer to your Linux question:
Results 1 to 2 of 2
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    awk(ward) question: How does awk handle expansions with bash?

    Howdy folks.

    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:

    toprint=`printf '%b\n' $toprint | awk 'BEGIN { FS="," } { if($3$4!='$seccord') print }'`
    This doesn't work, however. It prints EVERY line, even though I know for sure that the fields $3 and $4 are equal to $seccord in at least some of them.

    I figure it's because of some weirdness in how bash vs. awk expand variables. Does anyone have an idea?



    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.

  2. #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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts