## 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:
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.