Formatting Problem

Hi
Suppose we have a file consisting of nos in following format

123 - 789
123 - 828
345 - 989
345 - 792

I require the following output

123, 789,828
345, 989,792
Means Unique nos in 1st Column and Corresponding two nos in comma separated 2nd Column
Please help me out...

awk -F" - " '{ if( $1 == prev ) { printf ",%s", $2 } else { printf "\n%s,%s", $1, $2; prev=$1; } }END {printf "\n"}' filename
awk -F"-" ' 
{ 
   gsub(" +","",$0) ;
   arr[$1]=arr[$1] "," $2 
}
END { 
   for( key in arr ) { print key arr[key] } 
} ' file

## This is a traditional shell programming solution.
## It works, it can handle different cases,
## The only drawback is it has several lines of code.
##
## Run it as: cat <input_file> | <this_shell>
##
savefld1=""
Out=""
while read iLine
do
fld1=`echo $iLine | cut -f1 -d"-"`
fld2=`echo $iLine | cut -f2 -d"-"`
if [[ $fld1 = $savefld1 ]]; then
Out=$Out","$fld2
echo $Out
Out="" else
if [[ $Out != "" ]]; then

[INDENT] echo $Out
Out="" fi
Out=$fld1","$fld2 fi
savefld1=$fld1[/INDENT]done
if [[ $Out != "" ]]; then
echo $Outfi

This is not a traditional shell script; it is not even a POSIX shell script. It includes non-standard syntax, and doesn't take advantage of the features of the POSIX shell.

Do not run it like that! There is no need for cat:

path_to_script < INPUTFILE

In a POSIX shell, you do not need cut to split up a string:

fld1=${iLine%%-*}
fld2=${iLine#*-}

That is non-standard syntax; use the standard [ ... ] instead:

if [ "$fld1" = "$savefld1" ]; then

Thank you for your information, cfajohnson.

I was just trying to help Pradeepred solve his problem.

We all know that there are innumerous ways to solve a problem in unix.

For the past 26 years, I have been working for several companies where,
the four most important things are:
1- To get the job done.
2- It does not break.
3- It is easy to understand.
4- It is easy to maintain it.

Again, I was just sincerely trying to help Pradeepred.

Sorry if I broke any rule writing any code that is not considered POSIX
shell script.

Best regards,
Marcos

The point is that it WILL break if the original poster is not using the same shell as you.

The reason for using a POSIX shell is to make the script portable. If you use a shell-specific, non-standard syntax, you may not be helping anyone.

Every *nix system has a POSIX shell, but it may not be bash or ksh93.