A list of non-zero-padded unsorted lines is given:
seq 100 | shuf
piping that into sort
would give:
1
10
100
11
12
13
...
because sort
sorts lexicographical by default. Thus, it has -n
option for numerical sort, which would yield the expected result. However, if strings are not totally numeric, this wouldn’t work:
seq 100 | shuf | sed s/^/E/ | sort -n
Or for a more complex case:
paste -dS <(seq 100 | shuf | sed 's/^/E/' | sort -n) <(seq 100 | shuf)
---
E1S70
E10S75
E100S41
E11S53
...
- What’s an efficient way to sort non-zero-padded mixed strings?
- Given a non-zero-padded mixed string, how to zero-pad it up to the number of digits of the longest number?