I am writing my first unix bash script and I find it really difficult because I've never used linux before. It is homework from university. I tried a lot of stuff and I've been looking for hours but I simply can't find the right solution. Can someone give me some directions of what to use to sort strings from array which look like this:
room_100056.dat 4
room_8973.dat 2
room_7764.dat 1
room_2092.dat 20
to this:
room_7764.dat 1
room_8973.dat 2
room_10056.dat 4
room_2092.dat 20
When sorting the initial array, I am creating a new array which contains the sorted data. The problem is that in the sorted array, the data is printed like this:
room_100056.dat
room_8973.dat
room_7764.dat
room_2092.dat
1
2
4
20
Code:
file=$1/*.dat
count_visits=()
for room in $file
do
visits=$(grep "<Visit>" $room | wc -l)
filename=$(basename $room)
count_visits+=($filename$'\t'$visits)
done
sorted_visits=($(echo ${count_visits[@]} | tr " " "\n" | sort -g))
printf '%s\n' "${sorted_visits[@]}"
exit
count_visits+=( "$filename"$'\t'"$visits" ), and similarly quoting"$room". In general, you should probably run this code through shellcheck.net and follow its advice.sort -nneeded, without the-k2).