I have to sort the 4th column of an excel/csv file. I tried the following command
sort -u --field-separator=, --numeric-sort -k 2 -n dinesh.csv > test.csv
But, it's not working. Moreover, I have to do the same for more than 30 excel/csv file. So please help me to do the same.
Try:
sort -t, -k4,4n file
As to the -u switch, that does is not part of the standard when we are selecting fields.
This should work:
sort -t, -k4,4n file | uniq
Some sorts can do this:
sort -ut, -k4,4n file
--
Note: the csv format allows for field separators to be used as regular characters inside double quotes. If that is the case in your input file, then you need a bit of additional programming or a specialty program to do this..
2 Likes
Thanks Scrutinizer, Please help me to implement for multiple files. say for example, I would like to sort the 4 th column for 30 excel files.
--- Post updated at 05:29 AM ---
Thanks Scrutinizer, Please help me to do the same for multiple files. say for example, I would like to sort the 4 th column for 30 excel files.
You're welcome. Try:
for file in *.csv
do
sort -t, -k4,4n "$file" | uniq > "new_${file}"
done
--
Please note the note in post #2
1 Like
It works perfectly. Many many thanks:)
Dear Scrutinizer,
I ran your script successfully for multiple files. But, right now the script is not working. Even I tried the following command separately on terminal
sort -t, -k4,4n myfile.csv > dinesh.csv
But, its not sorting now like earlier. It's a kind of mystery to understand:confused:
Dear Scrutinizer,
It works with the following command,
sort -k4n,4 -t $'\t'