Friday, May 3, 2024
5
rated 0 times [  5] [ 0]  / answers: 1 / hits: 8797  / 2 Years ago, fri, may 6, 2022, 10:32:04

I was looking at this question and I wondered if the following could be done from terminal. I did it in python, just want to see if it could be done from terminal, bash scripting or whatever.



Suppose I have a file that looks like so:



2,4,5,14,9
40,3,5,10,1


could it be sorted like so, by rows (lines)



2,4,5,9,14
1,3,5,10,40


or is it too complicated? I did it using python, I just want to know if it could be done so next time I might not use python. What I have done, is creating lists and sorting them.


More From » command-line

 Answers
4

Here's another Perl approach:



perl -F, -lane 'print join ",",sort {$a<=>$b} @F' file.txt


And another shell/coreutils one (though personally, I prefer steeldriver's excellent answer which uses the same idea):



while read line; do 
tr , $'
' < <(printf -- "%s" "$line") | sort -g | tr $'
' , | sed 's/,$/
/';
done < file.txt

[#26041] Sunday, May 8, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
antebrow

Total Points: 291
Total Questions: 135
Total Answers: 117

Location: New Caledonia
Member since Thu, Mar 23, 2023
1 Year ago
;