How to read records in a file and sort it?

I have a file which has number of pipe delimited records.
I am able to read the records....but I want to sort it after reading.

i=0
while IFS="|" read -r usrId dataOwn expire email group secProf startDt endDt smhRole RoleCat DataProf SysRole MesgRole SearchProf 
do
 
print $usrId $dataOwn $expire $email $group $secProf $startDt $endDt $smhRole $RoleCat $DataProf $SysRole $MesgRole $SearchProf
 
done

But I want to sort these records as per $usrId

After reading is a little too late to sort, isn't it?

You sort with the sort command. To sort on the 3rd column you'd do sort -t'|' -k 3,3 inputfile > outputfile

More details depend on what your data looks like.

You can probably do it with awk command:-

awk -F"|" ' { for (i=1;i<=NF;i++) {print $i;} }' filename | sort

You can try it and let me know if any issues...

Hey Thanks for the reply

I dont want to generate another file (or) change the input file.
I need to read the records which are sorted to store in variables.

i=0
while IFS="|" read -r usrId dataOwn expire email group secProf startDt endDt smhRole RoleCat DataProf SysRole MesgRole SearchProf 
do

print $usrId $dataOwn $expire $email $group $secProf $startDt $endDt $smhRole $RoleCat $DataProf $SysRole $MesgRole $SearchProf

done

when I print I need to get the sorted records...so the rows should be sorted by $usrId

I will use the sorted records and do some functionality to generate few other files.

sort -t'|' -k 3,3 inputfile | while IFS="|" read ...
do
        ...
done

Note that because of the pipe, variables set inside the while-read loop won't be changed outside of it.

When you're setting that many variables at once with read, I'd be tempted to just set -- instead of typing out 37 names all the time.

OLDIFS="$IFS"
IFS="|"
sort -t'|' -k 3,3 inputfile | while IFS="" read LINE
do
        set -- $LINE
        # ${1} through ${14} are now set to column values
done
IFS="$OLDIFS"