Reputation: 3099
I have a file myinputfile.txt
that looks like this:
2.34 1.25 ./RUN1 ./RUN1/mod/file1.csv
3.34 1.25 ./RUN1 ./RUN1/mod/file2.csv
1.32 1.25 ./RUN2 ./RUN2/mod/file1.csv
4.35 1.25 ./RUN2 ./RUN2/mod/file2.csv
I want a new file that contains the line that has the maximum value of column 1 for each value of RUN*
. Desired contents of new file (myresultsfile.txt
):
3.34 1.25 ./RUN1 ./RUN1/mod/file2.csv
4.35 1.25 ./RUN2 ./RUN2/mod/file2.csv
I have a script that mostly gets me there, but doesn't put things on new lines:
#!/bin/bash
resultsfile="myresults.txt"
inputfile="myinputfile.txt"
res1=$(sort -k3,3r $inputfile | awk '{split($nF,d,"/")} !a[d[3]]++')
echo $res1 >> $resultsfile
Upvotes: 0
Views: 41
Reputation: 67537
with sort
assisted awk
$ sort -k1nr file | awk '!a[$3]++'
3.34 1.25 ./RUN1 ./RUN1/mod/file2.csv
4.35 1.25 ./RUN2 ./RUN2/mod/file2.csv
simply redirect to a new file
$ sort -k1nr file | awk '!a[$3]++' > sorted_file
Upvotes: 1
Reputation: 785731
You can use awk
like this:
awk '$1 > max[$3] { max[$3] = $1; rec[$3] = $0 } END { for (i in rec) print rec[i] }' file
3.34 1.25 ./RUN1 ./RUN1/mod/file2.csv
4.35 1.25 ./RUN2 ./RUN2/mod/file2.csv
max
is an associative array that holds max value of first column by the key as column 3rec
is an associative array that holds full line by the key as column 3Upvotes: 0