Can someone help me search the minimal value of a two column csv file with a single command or bashscript?
2
0
Entering edit mode
8 weeks ago
mxm189 • 0

Hi, question is in the title.

I have a two column csv file (like below) and I tried to get the min value out of it.

0.24380519999999706,4.47

0.25453569999999814,2.06

0.2668407999999971,-3.05

0.2775802999999968,1.72

0.2885514999999934,2.69


I tried datamash but it keeps returning: invalid numeric value in line 1 field 1. (I also tried this: https://www.baeldung.com/linux/min-max-median-mean, script but it only returns the median value for some reason). So I have a bit of a workaround, where I first print the second column of the file into a different file and then use awk to find my minimal value and put it into another file. I was wondering if someone can help me with this so that I can do this with just one command line and only generate one file.

$awk -F',' '{print$2}' test.csv > mintest.txt
$min=$(awk '!/col/ {print $1}' mintest.txt | sort -nr | tail -1)$ echo "\$min" > mintest.csv

grep text csv • 475 views
3
Entering edit mode
8 weeks ago
seidel 11k

You want the minimum value of the second column of a csv file? You could use sort like so:

cat foo.txt
0.24380519999999706,4.47
0.25453569999999814,2.06
0.2668407999999971,-3.05
0.2775802999999968,1.72
0.2885514999999934,2.69

sort -t ',' -n -k 2,2 foo.txt | head -1 | cut -f2 -d ","
-3.05


This uses sort with -t to set the delimiter, then sorts numeric on the second field, ascending, placing the lowest value at the top, which you can grab using head, and then taking the second field.

0
Entering edit mode

This works :) thank you. I am still new to this, I should look into some tutorials or do some more reading on working with linux

1
Entering edit mode
8 weeks ago
size_t ▴ 60
cut -d "," -f2 | foo.txt | sort -n | head -1

0
Entering edit mode

I tried this and it did not work, but thanks for the comment ;) The answer posted by seidel did work

2
Entering edit mode

This is a simpler solution. Since you only care about the second column, cut it out, sort it numeric ascending, return the first value. The only reason it doesn't work is because @size_t has an extra pipe character between cut and the file argument. It should be:

cut -d "," -f2 foo.txt | sort -n | head -1