1

Please anyone know why this command is not giving me any output? allthe variables are correct but no result.

find . -name "$cdr_type*$DAYZ*.unl*" |
  xargs zcat |
  awk -F "|" '{if($14==$tdate && $22==$misdn) print $0}'|
  head

When I echo the command like this

echo "find . -name $cdr_type*$DAYZ*.unl* | xargs zcat | awk -F | '{if($14==$tdate && $22==$misdn) print $0}'|head" 

here is my output:

find . -name mgr*20160928*.unl* |
  xargs zcat |
  awk -F | '{if(4==20160928093911 && 2==2348094398953)}'|
  head

My observation is its showing $4 and $2 instead of $14 and $22

  • Try ${ cdr_type}, ${DAYZ}, ${14} and ${22} instead of $cdr_type, $DAYZ, $14 and $22. – Luis Oct 4 '16 at 15:48
  • 1
    How do you echo it? $14 as expanded by the shell would be the same as ${1}4 and if $1 is empty, that would give 4 as expected. In there, it looks like there are some $s that you want to pass untouched to awk and some for which you want shell variables to be expanded. – Stéphane Chazelas Oct 4 '16 at 15:49
  • echo "find . -name $cdr_type*$DAYZ*.unl* | xargs zcat | awk -F | '{if($14==$tdate && $22==$misdn) print $0}'|head" – yemmy Oct 4 '16 at 16:03
3

$14 when expanded by the shell in most Bourne-like shells is the same as ${1}4, and if $1 is empty or unset, that gives 4 as expected. Here you want to pass a litteral $14 to awk so that awk treats it as its 14th field.

Normally within single quotes, that $1 variable would not be expanded but you're not telling us how you echo that command.

In any case, here, it looks like you want:

find . -name "$cdr_type*$DAYZ*.unl*" -exec zcat {} + |
  awk -F "|" -v tdate="$tdate" -v misdn="$misdn" '
   $14 == tdate && $22 == misdn' |
 head

That is pass the content of the $tdate and $misdn shell variables as awk variables.

  • This worked, just what I wanted!!! Thanks! – yemmy Oct 4 '16 at 16:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.