I have a 4-column CSV file. I want to sort the lines, such that, the lines containing any number somewhere within the third column are pushed to the end of the document, lines without numbers in the third column are put to the beginning. How can I sort the file in this way?
Update:
To clarify, I need to move lines which contain any number (i.e. a match for [0-9]
) somewhere within the letters of the third column (the third column of the line might contain other symbols). Spaces are not important. E.g.
dog, eats chicken, has 4 legs, does not like cats
cat, eats mice, has a tail, does not like water
mouse, eats bugs, has 4 legs, does not like cats
elephant, eats peanuts, has a trunk, does not like mice
Would be sorted to:
cat, eats mice, has a tail, does not like water
elephant, eats peanuts, has a trunk, does not like mice
dog, eats chicken, has 4 legs, does not like cats
mouse, eats bugs, has 4 legs, does not like cats
Something like this should work:
awk 'BEGIN {FS=","; OFS=","}; {print match($3,/[0-9]/), $0}' <file> | sort | cut -d, -f2-
The strategy is to
awk
to insert at the beginning of each line the index in which a digit is found (or 0 if no digit is found)sort
to sort all the linescut
to remove the number that was prepended by awk
.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With