Johnny
Johnny

Reputation: 53

Sorting 2 row file.csv using UNIX

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

Upvotes: 0

Views: 32

Answers (2)

Serge Ballesta
Serge Ballesta

Reputation: 149195

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...

Upvotes: 1

Inian
Inian

Reputation: 85895

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

Upvotes: 1

Related Questions