CSV format problem

studentNo=$1
tmp=/tmp/acc2courses$ID.$$
grep -w "$studentNo" CSE1001 >/dev/null 2>&1
[ $? -eq 0 ] && echo CSE1001 >>$tmp
grep -w "$studentNo" CSE1002 >/dev/null 2>&1
[ $? -eq 0 ] && echo CSE1002 >>$tmp
grep -w "$studentNo" CSE9001 >/dev/null 2>&1
[ $? -eq 0 ] && echo CSE9001 >>$tmp

This is my code,
The output in tmp file should be like
CSE1001
CSE1002
CSE9001

My problem is how can I convert this to comma separate format in one line?
which kind of tool should I use? awk? sed? please help,

should be like CSE1001, CSE1002, CSE9001
while read w1
do
read w2
read w3
echo ${w1}${w2}${w3}
done < $tmp

but It doesn't work with only 1 or 2 words, it will be like CSE1001, CSE1002,
Would someone give an AWK solution about it?

You can try to append all the strings in one variable and print the variable. Something like below

studentNo=$1 
tmp=/tmp/acc2courses$ID.$$ 
grep -w "$studentNo" CSE1001 >/dev/null 2>&1
[ $? -eq 0 ] && tmpString="CSE1001"
grep -w "$studentNo" CSE1002 >/dev/null 2>&1
[ $? -eq 0 ] && tmpString=$tmpString,"CSE1002"
grep -w "$studentNo" CSE9001 >/dev/null 2>&1
[ $? -eq 0 ] && tmpString=$tmpString,"CSE9001"
echo $tmpString >> $tmp

thanks for replying, I still can't get it work, is there any AWK way to solve the problem?

Look at this,

sh -x test.sh 1000
+ studentNo=1000
+ grep -w 1000 CSE1001
+ '[' 0 -eq 0 ']'
+ tmpString=CSE1001
+ grep -w 1000 CSE1002
+ '[' 0 -eq 0 ']'
+ tmpString=CSE1001,CSE1002
+ grep -w 1000 CSE9001
+ '[' 0 -eq 0 ']'
+ tmpString=CSE1001,CSE1002,CSE9001
+ echo CSE1001,CSE1002,CSE9001
CSE1001,CSE1002,CSE9001

Or do you want to merge all the lines in your tmp file to form a comma separated line?

But sometime it may has CSE9001 only. or CSE1002

Here is
tomoo@Tomoo:~$ bash std_db_acc2courses cs123456
std_db_acc2courses: line 15: CSE1002: command not found
std_db_acc2courses: line 17: CSE9001: command not found
CSE1001

awk '/^[1-9]/{print ""}
{gsub("\"", "")
printf"%s,", $0}
END{print ""}' <<EOF | sed 's/,$//'
1,ad,"adc
sdfd",edf
2,asd,"def
fde",asd
EOF

1,ad,adc,sdfd,edf
2,asd,def,fde,asd

This is the solution from AWK in another post. I consider they can be similar solution by changing code a little bit, I am not very familiar with AWK, so....

I am getting this clear. what is your line 15 and 17, if possible please post the entire code so that we could help.

#!/bin/bash

. ${HOME}/.std_dbrc
cd $STD_DBROOT
studentNo=$1
tmp=/tmp/acc2courses$ID.$$

grep -w "$studentNo" CSE1001 >/dev/null 2>&1
[ $? -eq 0 ] && tmpString=CSE1001
grep -w "$studentNo" CSE1002 >/dev/null 2>&1
[ $? -eq 0 ] && tmpString="$tmpString", CSE1002 ########Line 15
grep -w "$studentNo" CSE9001 >/dev/null 2>&1
[ $? -eq 0 ] && tmpString="$tmpString", CSE9001 ########Line 17
echo $tmpString

#IN TERMINAL
bash file_name cs012345
#This will return CSE2001 CSE2002 by my database file.
#This script will find student number in my database file to print course #number.

I think the extra space after the tmpString is causing the error.

tomoo@Tomoo:~$ bash std_db_acc2courses cs123456
std_db_acc2courses: line 15: CSE1002: command not found
std_db_acc2courses: line 17: CSE9001: command not found

Genius!
Thanks for helping
It works now.