That's a shell command I can break it into digestible chunks if you wish
perl -ne ' #Call the Perl interpreter and iterate over every line in the files listed as arguments with the following script
chomp; # Strip newlines
@r=split/,/; #Create an array r of each field (separated by ",") in the record
push @{$fields{$r[0]}},$r[1] if $r[2] eq "Y"; # if the falg is "Y" then add the id to an array stored in a hash and keyed on name
}{ # Eskino's nose execute the remainder when you've finished processing the file(s)
for $fruit (keys %fields){ # for each name field we stored an array for
print "hive -s -e \"create ${fruit}_view as select ", join",",@{$fields{$fruit}}," from main table\"\n"} # Print the required string for the named joining the id's with a ","
' tmp.dat # for this file
Thanks a lot, what i was trying to say is instead of using perl interpretor can i use any other unix interpreter and i would like to pass the fruit as parameter
And you want to pass one single fruit only to the function, not two or even more? Had you said that from the beginning, much effort could have been avoided!
create_query() { unset FND SEP
read
while IFS=, read NAME ID FLAG
do if [ "$1" = "$NAME" ]
then if [ -z "$FND" ]
then printf "hive -s -e \"create ${NAME}_view as select "
fi
FND="Y"
if [ "$FLAG" = "Y" ]
then printf "%1s%s" $SEP $ID
SEP=","
fi
fi
done
if [ "$FND" ]
then printf " from main_table;\"\n"
return 0
fi
return 1
} < $2