1

鉴于下表,我想分别总结每一行,找出总和值的最高值和第二高值,并在相应的行中放置一个标记(例如,一个 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
4

2 回答 2

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
于 2012-04-18T14:40:58.933 回答
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
于 2012-04-18T13:01:25.237 回答