Help w/awk, korn shell & writing to a file

Help w/awk, korn shell & writing to a file

Post by Ms. Terry L » Thu, 28 Jul 1994 01:58:28



First, this post is cross posted to comp.unix.questions, .shell &
.programmer.....I desparately need help.

I am using korn shell on a Pyramid box running OSx Unix.

I am trying to write a shell script that will echo out a line of data (that is
read from a file) based upon a user's login id.

This is basically what I have:

#Based on the LOGNAME/USERID, a variable, namely flag, is set to 0,1 or 2 for
#how many to look thru file userid.dat looking for the correct string to
#output.  #For instance, userid.dat may have:

#LOGNAME|id#1|id#2|path
tmlee1|100|101|/shared_path_for_group_tmlee/unix
tmlee1|200|101|/shared_path_for_gorup_tmlee/unix

#end of file userid.dat
------------------------------

From this file, say userids of tmlee5* would use the 2nd line (userid's  
tmlee50-59), all other userids of tmlee* would use the 1st line.

Okay...here's part of the shell script:

awk 'BEGIN {FS = "|'; OFS=", "; ORS="" }

# Load array with the user privileges...also based on userid
FILENAME=="data.dat"{
     split($0,entry)
     id_array[entry[1]]=entry[2]
     next

Quote:}

0 ~ /'"$search"'/{
     if ($flag==2){       #  Loop 2 times to get to correct line in userid.dat
        flag=1
        continue
     }
     if ($flag==1){
        flag=0
        continue
    }

# This part is the problem.
 mycrc="crc"
 print $1 OFS $2 OFS $3 OFS $4 OFS $5 OFS $6 OFS id_array[$7] OFS ORS >> mycrc

Quote:}' data.data userid.dat

....other code to access file 'crc'.

This doesn't work....I always get the 1st line.

If I try:

if ($flag==0){
  mycrc="crc
  print ..... >> mycrc

Quote:}

the file is never created but if I use a test "echo" before printing I'll get
both the 1st line & part of the 2nd line.   I've tried moving 'mycrc="crc" '
before the awk & using $mycrc as well as plain mycrc....both don't work.  I've
tried using printf....that doesn't work or I can't get it to work.

Any clues?  Am I just making this harder that it has to be?  Please
help.....this is driving me crazy.