Problem with Substring

Hi,
i have a file from which i extracted 2nd column and put it into a variable Acct_Num . Now i need to extract first 3 digits from it and save in to other variable.
My Code is:

while read Record1
do
Acct_Num=`echo $Record1 | awk '{print $2 }'`
ID=`echo $Record1 | awk '{print substr($Acct_Num, 1, 3 ) }`
echo "ID is $ID"
echo "Acct_Num is $Acct_Num"
done < $REGSeqFileName

Code to extract 2nd column from file is working fine. But the code to extract first 3 digits from Acct_Num (ID) is not working fine .
Can anybody help me with this ASAP.

Thanks

Please put code inside

 tags.

 
  while read Record1
do
 Acct_Num=`echo $Record1 | awk '{print $2 }'`
 ID=`echo $Record1 | awk '{print substr($Acct_Num, 1, 3 ) }`

[/quote]

[INDENT]
The immediate problem is that a shell variable is not expanded inside single quotes. You can pass a variable to awk with -v:

awk -v Acct_Num="$Acct_Num" '{print substr(Acct_Num, 1, 3 ) }'

The other problem is that your script will run excruciatingly slowly because you are calling awk twice for every line of the file.

Instead of using a shell loop, let awk do all the work:

awk '{
  printf "AcctNum is %s\n", $2
  printf "ID is %s\n", substr($2,1,3)
   }' "$REGSeqFileName"

Thank you so much for solution...i'll try this way now...:slight_smile:

Can anybody tell me what this following code is doing?? i couldn't understand...

awk -F"," 'NR==FNR {col1[$1]=$3} NR!=FNR { if (col1[substr($2,2,3)]=="") {next} print $1}' $TargetSeqPath/Ref.tmp $TargetSeqPath/Master.tmp >> $TargetSeqPath/$OutFileName.dat

Basically $TargetSeqPath/Ref.tmp and $TargetSeqPath/Master.tmp are two files

Thanks for any help