0

I need some help sorting this this .csv The sort needs to be accouring to the first row with y and y related so they have to sort along and so on...

y, d, a, w, c,.......
y, d, a, w, c,.......

output

a, c ,d ,w ,y
a, c ,d ,w ,y

Thanks

2
  • can't we write a small java program that can read this and sort as you want. If so I can write one? Commented Oct 6, 2016 at 9:46
  • i need it using unix shell script :/ Commented Oct 6, 2016 at 9:47

2 Answers 2

1

If your input is pretty straight forward comma separated list of strings this list hacky use of tr and sort could do the job for you.

$ echo " y, d, a, w, c" | tr , "\n" | sort | tr "\n" , | sed 's@,$@\n@'
 a, c, d, w, y

For a more generic-solution try using GNU awk as following:-

$ cat script.awk
#!/bin/gawk

BEGIN {
    FS=","                          # Setting input-field-separator to ','
    OFS=","                         # Setting output-field-separator to ','
}

{
    split($0, words)                # Split the strings and store it in array
    asort(words)                    # Using gawk's inherent sort function 'asort' sort the words alphabetically
    for (i in words) $i=words[i]    # Store the words back in the array and printing the same
    print
}

And a sample input file

$ cat input.csv
pineapple,blueberries,strawberries
pencil,crayon,chalk,marker
bus,car,train,motorcycle,bicycle,skateboard

Run the script using gawk as

$ gawk -f script.awk input.csv
blueberries,pineapple,strawberries
chalk,crayon,marker,pencil
bicycle,bus,car,motorcycle,skateboard,train
Sign up to request clarification or add additional context in comments.

5 Comments

@Johnny: Use the gawk solution, in the recent update
I really loved the hacky first!
@SergeBallesta: Being a long-time user you can acknowledge it through a nice "upvote" :) unless you meant it ironically
@Inian was about to do it (and I've done now), but I was preparing a way based on your working for multi line inputs. Feel free to include it in your answer if you like it and ping me so I can remove mine.
@SergeBallesta: Cheers!
1

Here is a way based on Inian's original answer - credits should remain to him...

while true
do read line
if [ "x${line}" == "x" ]
then break
else
echo $line | tr , "\n" | sort | tr "\n" ,
echo
fi
done

Simply use it that way: line_sort.sh < orig_file > sorted_file


Disclaimer: this works only for very simple csv. CSV produced by spreadsheets can be far more complex because CSV can support newlines and commas inside fields. If I wanted a robust solution I would use a true language and a CSV library. Python comes with an excellent CSV module included...

2 Comments

nice approach with < > re-direction :)
while read line; do echo $line | tr , "\n" | sort | tr "\n" , | sed 's@,$@\n@'; done or while read line; do <<<"$line" tr , "\n" | sort | tr "\n" , | sed 's@,$@\n@'; done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.