I'm having a problem with the following snippet:
while read line
do
team1=`echo "${line% *}"`
team2=`echo "${line#* }"`
newline=`printf "%-8.2f\t%-8.2f\n" "$team1" "$team2"`
#this line give perfect output
# echo "$newline"
##################problem is here########################
final_list=("${final_list[@]}" "$newline")
#########################################################
done <input.txt
#this line is losing all the formatting I gave
echo -e ${final_list[@]} | sort -t$'\t' -k1 -nr
This is my input file
3.01 16.12
1.12 13.22
3.01 14.12
My expected output is
1.12 13.22
3.01 14.12
3.01 16.12
Use sort -n to sort the file (if needed redirect it to new file like > newfile.txt and use it)?
Replace:
with:
This achieves your expected output:
There were two required changes. First,
sort
works line-by-line and theecho
output produces a single line. Second, because your desired output was numerically sorted in ascending order, the-r
flag needed to be removed fromsort
.What went wrong
This line does not do what one would think:
The problem is that, when the shell does command substitution, any trailing newline characters are deleted. So, the
\n
in theprintf
format is removed before the output is assigned tonewline
.That means that when you get to here:
There are no newlines. The
echo
output all appears on one line.Now, consider:
That works because
echo
adds its own newline to the output.A simpler script
I don't know the structure of your big project, but, as a possible alternative to the code presented, consider:
Still simpler, but possibly not helpful in the big project, would be this code which reformats and sorts
input.txt
: