Suppose you have a CSV file like this
pepperoni feta cheese mozzarella cheese beef,bacon,mozzarella cheese,onions,pineapple sausage pepperoni,artichokes four cheese
And you want to sort each lines contents alphabetically to this
pepperoni feta cheese mozzarella cheese bacon,beef,mozzarella cheese,onions,pineapple sausage artichokes,pepperoni four cheese
This script will do it line by line say it is called sortrows.sh
#!/bin/bash while IFS='' read -r line || [[ -n "$line" ]]; do sorted=`echo $line | egrep -o "[^,]+" | sort -f | paste -sd "," -` echo "$sorted" done < "$1"
- egrep -o "[^,]+" : Separate each lines contents by ,
- sort -f: Sort all the lines alphabetically
- paste -sd "," -: Paste the lines back together and separate by ,
Called in this manner
chmod 755 sortrows.sh ./sortrows.sh unsorted.csv > sorted.csv
It's not the fastest option, but it is decomposeable with bash pipelines.