将每行除以awk中的最大值
Divide each row by max value in awk
我试图将行除以该行中的最大值作为(所有列的行都为 NA)
r1 r2 r3 r4
a 0 2.3 1.2 0.1
b 0.1 4.5 9.1 3.1
c 9.1 8.4 0 5
我明白了
r1 r2 r3 r4
a 0 1 0.52173913 0.043478261
b 0.010989011 0.494505495 1 0.340659341
c 1 0.923076923 0 0.549450549
我试图通过执行
来计算每行的最大值
awk '{m=;for(i=1;i<=NF;i++)if($i>m)m=$i;print m}' file.txt > max.txt
然后将其作为最后一列粘贴到 file.txt 作为
paste file.txt max.txt > file1.txt
我正在尝试执行一个代码,其中最后一列将划分该行中的所有列,但首先我需要格式化每一行,因此我被困在
awk '{for(i=1;i<NF;i++) printf "%s " $i,$NF}' file1.txt
我正在尝试打印该行的每个组合,然后在新行上打印下一行组合。但是我想知道是否有更好的方法来做到这一点。
以下 awk
可能会对您有所帮助:
awk '
FNR==1{
print;
next
}
{
len=""
for(i=2;i<=NF;i++){
len=len>$i?len:$i};
printf("%s%s", , OFS)
}
{
for(i=2;i<=NF;i++){
printf("%s%s",$i>0?$i/len:0,i==NF?RS:FS)}
}
' Input_file
说明:现在在此处添加说明和解决方案:
awk '
FNR==1{ ##FNR==1 is a condition where it will check if it is first line of Input_file then do following:
print; ##printing the current line then.
next ##next is awk out of the box keyword which will skip all further statements now.
}
{
len="" ##variable named len(which contains the greatest value in a line here)
for(i=2;i<=NF;i++){ ##Starting a for loop here starting from 2nd field to till value of NF which means it will cover all the fields on a line.
len=len>$i?len:$i}; ##Creating a variable named len here whose value is if it is NULL and if it is greater than current then it remains same else will be
printf("%s%s", , OFS) ##Printing the 1st column value here along with space.
}
{
for(i=2;i<=NF;i++){ ##Starting a for loop here whose value starts from 2 to till the value of NF it covers all the field of current line.
printf("%s%s",$i>0?$i/len:0,i==NF?RS:FS)} ##Printing current field divided by value of len varible(which has maximum value of current line), it also checks a conditoin if value of i equals to NF then print new line else print space.
}
' Input_file ##mentioning the Input_file name here.
awk
救援!
$ awk 'NR>1 {m=; for(i=3;i<=NF;i++) if(>m) m=;
for(i=2;i<=NF;i++) $i/=m}1' file
r1 r2 r3 r4
a 0 1 0.521739 0.0434783
b 0.0222222 1 2.02222 0.688889
c 1 0.923077 0 0.549451
我试图将行除以该行中的最大值作为(所有列的行都为 NA)
r1 r2 r3 r4
a 0 2.3 1.2 0.1
b 0.1 4.5 9.1 3.1
c 9.1 8.4 0 5
我明白了
r1 r2 r3 r4
a 0 1 0.52173913 0.043478261
b 0.010989011 0.494505495 1 0.340659341
c 1 0.923076923 0 0.549450549
我试图通过执行
来计算每行的最大值 awk '{m=;for(i=1;i<=NF;i++)if($i>m)m=$i;print m}' file.txt > max.txt
然后将其作为最后一列粘贴到 file.txt 作为
paste file.txt max.txt > file1.txt
我正在尝试执行一个代码,其中最后一列将划分该行中的所有列,但首先我需要格式化每一行,因此我被困在
awk '{for(i=1;i<NF;i++) printf "%s " $i,$NF}' file1.txt
我正在尝试打印该行的每个组合,然后在新行上打印下一行组合。但是我想知道是否有更好的方法来做到这一点。
以下 awk
可能会对您有所帮助:
awk '
FNR==1{
print;
next
}
{
len=""
for(i=2;i<=NF;i++){
len=len>$i?len:$i};
printf("%s%s", , OFS)
}
{
for(i=2;i<=NF;i++){
printf("%s%s",$i>0?$i/len:0,i==NF?RS:FS)}
}
' Input_file
说明:现在在此处添加说明和解决方案:
awk '
FNR==1{ ##FNR==1 is a condition where it will check if it is first line of Input_file then do following:
print; ##printing the current line then.
next ##next is awk out of the box keyword which will skip all further statements now.
}
{
len="" ##variable named len(which contains the greatest value in a line here)
for(i=2;i<=NF;i++){ ##Starting a for loop here starting from 2nd field to till value of NF which means it will cover all the fields on a line.
len=len>$i?len:$i}; ##Creating a variable named len here whose value is if it is NULL and if it is greater than current then it remains same else will be
printf("%s%s", , OFS) ##Printing the 1st column value here along with space.
}
{
for(i=2;i<=NF;i++){ ##Starting a for loop here whose value starts from 2 to till the value of NF it covers all the field of current line.
printf("%s%s",$i>0?$i/len:0,i==NF?RS:FS)} ##Printing current field divided by value of len varible(which has maximum value of current line), it also checks a conditoin if value of i equals to NF then print new line else print space.
}
' Input_file ##mentioning the Input_file name here.
awk
救援!
$ awk 'NR>1 {m=; for(i=3;i<=NF;i++) if(>m) m=;
for(i=2;i<=NF;i++) $i/=m}1' file
r1 r2 r3 r4
a 0 1 0.521739 0.0434783
b 0.0222222 1 2.02222 0.688889
c 1 0.923077 0 0.549451