How to sort by row from terminal
Solution 1
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 , $'\n' < <(printf -- "%s" "$line") | sort -g | tr $'\n' , | sed 's/,$/\n/';
done < file.txt
Solution 2
This should do about what you're looking for. It will read a file (test.txt) and create an output file (sorted.txt) with the sorted lines in the same order as they appear in the original file.
while read line; do j=$(echo "$line" | sed -e 's/,/\n/g' | sort -n); echo "$j"
| sed -e 's/ /,/g' >> sorted.txt; done < test.txt
Solution 3
Here comes an awk
solution,
while read -r line; do (echo $line | awk '{ n=split($1,a,","); asort(a); for(i=0;i<=n;i++) { print a[i];}}') | xargs | sed -e 's/ /,/g'; done < text.txt
Explanation:
-
awk '{ n=split($1,a,","); asort(a); for(i=0;i<=n;i++) { print a[i];}}'
awk Splits the field 1 according to the delimiter
,
and store each value into the arraya
finally the top position is stored into the variablen
. Nextasort(a)
function sort down the array values.Then the for loop inside the awk command prints the sorted values in the format of record by record. -
xargs | sed -e 's/ /,/g'
xargs
combines all the rows into a single row. -
sed -e 's/ /,/g'
It replaces the spaces with commas
,
-
while read -r line;
All the above
awk
,xargs
,sed
functions are done line by line with the help ofwhile
loop.
Solution 4
The only real difficulty in doing what you want using command-line scripting is that the available sort
function expects to sort lines within a file, rather than fields within a line. To work around that, you could replace the field delimiters, line-by-line, with newlines prior to the sort
function, then replace the newlines with delimiters again in each line after sorting.
There are a number of available text processing utilities which would allow you to do that (including sed
, awk
, or even the simple tr
) however the bash shell itself can do a lot nowadays. Assuming you meant for the input to be comma-delimited (you have a mix of commas and whitespace delimiters in your example) you could do:
while read line; do
sorted=$(sort -g -- <<< "${line//,/$'\n'}")
printf -- "${sorted//$'\n'/,}\n"
done < file
If you do need to handle space delimiters in the input, then you can add them as part of a character list [, ]
for the input substring replacement:
while read line; do
sorted=$(sort -g -- <<< "${line//[, ]/$'\n'}")
printf -- "${sorted//$'\n'/,}\n"
done < file
(note that the output remains strictly comma-delimited though).
Solution 5
python -c "your code here" input.txt
Or, if your program has multiple lines and eval()
is too slow:
python yourprogram.py input.txt
This oneliner works:
python -c "for l in open('input.txt'):print(','.join(sorted(l.strip().split(','), key=int)))"
Related videos on Youtube
Lynob
Updated on September 18, 2022Comments
-
Lynob over 1 year
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.
-
Lynob about 10 yearsI asked it here because I saw the other question, if it should not be asked here feel free to close it or delete it
-
terdon about 10 yearsI added the missing comma between
5
and14
in your input example. Based on your output, I assume that was a typo, please clarify if it was not.
-
-
terdon about 10 yearsHeh, I'd left my answer open for a while and hadn't seen this, but not the shortest :P
-
Sylvain Pineau about 10 yearsJe m'incline ;-)
-
Sylvain Pineau about 10 yearsNice solution, I up-vote immediately
-
terdon about 10 yearsAs is yours and as did I :)
-
terdon about 10 yearsYou could shorten this significantly with
perl -i -lne 'print join (",", sort {$a <=> $b} split(",", $_));' text.txt
. The-n
flag is the same aswhile(<>){}
and the-l
adds a\n
to every print call and also activates implicitchomp()
. -
Fredrik Pihl about 10 yearsbad usage of cat! To operate on each line, do
while read line; do ...; done < test.txt
-
jkt123 about 10 years@FredrikPhil: Thank you. I have edited my answer.
-
jobin about 10 yearsThe OP is not looking for a python-based solution.
-
GeorgiaTv about 10 yearsYes he is: "just want to see if it could be done from terminal, bash scripting or whatever."
-
jobin about 10 yearsLol, you nailed it, then!
-
Lynob about 10 yearsI hate it when I receive too many good answers :) and when you have to pick one randomly, it's like asking myself "what do I like more, chocolate bars or icecream?"