标签中带有标签最高值的列

时间:2012-04-18 12:32:11

标签: html bash awk sed tabular

根据下表,我想分别总结每一行,找出总和值的最高值和第二高值,并在相应的行中放置一个标记(例如html标记,第一行)求和值:<b>num1</b>和第二高值:<i>num2</i>):

cat input.txt
0.743745 0.00145661
0.740159 0.00208729
0.743907 0.0019944
0.769515 0.0051214
0.768735 0.00453603
0.755945 0.00203796

如果我们总结输入文件的每一行,我们将获得以下值:

0.7452
0.7422
0.7459
0.7746
0.7733
0.7580

给定这些计算值,所需的输出应如下所示:

cat output.txt
0.743745 0.00145661
0.740159 0.00208729
0.743907 0.0019944
<b>0.769515 0.0051214</b>
<i>0.768735 0.00453603</i>
0.755945 0.00203796

2 个答案:

答案 0 :(得分:1)

见下文:

kent$  cat test
0.743745 0.00145661
0.740159 0.00208729
0.743907 0.0019944
0.769515 0.0051214
0.768735 0.00453603
0.755945 0.00203796

kent$  awk '{a[NR]=$0;s=$1+$2;
if(m==""||s>m){m=s;mr=NR; }
if(n==""||(s>n&&s<m)){n=s;nr=NR;} }
END{for(i=1;i<=NR;i++)if(i==mr) print "<b>"a[i]"</b>" 
        else if(i==nr) print "<i>"a[i]"</i>"
        else print a[i]}' test
0.743745 0.00145661
0.740159 0.00208729
0.743907 0.0019944
<b>0.769515 0.0051214</b>
<i>0.768735 0.00453603</i>
0.755945 0.00203796

答案 1 :(得分:1)

awk '{print NR"\t"$0"\t"$1+$2}' input.txt |
  sort -k3gr |
    sed -r -e '1s#\t(.*)\t#\t<b>\1</b>\t#' -e '2s#\t(.*)\t#\t<i>\1</i>\t#' |
      sort -n |
        cut -f2

0.743745 0.00145661
0.740159 0.00208729
0.743907 0.0019944
<b>0.769515 0.0051214</b>
<i>0.768735 0.00453603</i>
0.755945 0.00203796