Reputation: 79
I need to sort a file based on the number of chars in the first column.
I have no idea on how to go about this. I'm using Linux, so sed/awk/sort are all available.
.abs is bla bla 12 .abc is bla se 23 bla .fe is bla bla bla .jpg is pic extension .se is for swedish domains
What I want is to sort these lines, based on the length of the first column in each line. Some of the lines start with 4 characters, some start with 3, or 2. I want the result to be something like:
.fe is bla bla bla .se is for swedish domains .abs is bla bla 12 .abc is bla se 23 bla .jpg is pic extension
Is this even possible?
Upvotes: 4
Views: 3932
Reputation: 31548
or you can also use sed afterwards like this
awk '{print length($1)" "$0}' temp.txt | sort -k 1,2| sed -re 's/^[0-9]+ //'
Upvotes: 2
Reputation: 47099
You can also do it with coreutils, albeit rather inefficient:
paste -d' ' <(cut -d' ' -f1 infile | xargs -l sh -c 'echo "$1" | wc -c' '{}') infile |
sort -n | cut -d' ' -f2-
Or with GNU parallel if it is available:
paste -d' ' <(cut -d' ' -f1 infile | parallel wc -c '<<< {}') infile |
sort -n | cut -d' ' -f2-
Or with bash:
<infile while read c1 rest; do echo ${#c1} "$c1" "$rest"; done |
sort -n | cut -d' ' -f2-
Upvotes: 1
Reputation: 36049
Augment each line by the length of the first word, then sort:
awk '{ print length($1) " " $0; }' $FILE | sort -n
If necessary, cut out the helper field with cut -d ' ' -f 2-
afterwards.
Upvotes: 10