问题描述
我使用'grep' & 提取了以下数据'sed' 管道,现在我想对最后两个数字执行数学方程,删除它们并用一个数字替换它们.
I have extracted the following data using 'grep' & 'sed' pipes from a file and now I want to perform a mathematical equation on the last two numbers, delete them and replace them with a single number.
数学运算
Add the numbers together
divide by 2
multiply by 141
ROUNDUP to whole number
文件数据
AJ29 IO_0_VRN_10 77.234 78.011
AJ30 IO_L1P_T0_100M 89.886 90.789
AJ31 IO_L1N_T0_100S 101.388 102.406
AK29 IO_L2P_T0_101M 66.163 66.828
AL29 IO_L2N_T0_101S 63.626 64.266
所以从 AJ29 开始的行应该显示为:
So the line starting AJ29 should appear as:
AJ29 IO_0_VRN_10 10945
我可以将它放在 MS excel/Open Office calc 中并执行此操作,但希望避免 MS 并将其保留在单个 linux 脚本中(如果可能).希望你能帮忙.到目前为止,我拥有的脚本如下,理想情况下,我想添加更多管道来实现这一点.
I could put it in MS excel / Open Office calc and do this but want to avoid MS and keep it in a single linux script if it is possible. Hope you can help. The script I have so far is below and ideally I'd like to add a few more pipes to achieve this.
grep IOB xc7vx690tffg1930.pkg | sed 's/pin//g' | sed 's/IOB_[A-Za-z0-9]*//g' | sed 's/ /-/g' | sed 's/\t//g' | sed 's/^[-]*//g' | sed 's/-/ /g' | sed 's/ [0-9][0-9] //g' | sed 's/[[:space:]]\+/,/g' | sed 's/,X[0-9A-Z]*,//g' | sed 's/,[0-9]*[A-Z],//g' | sed 's/N\.A\.,/,/g' | sed 's/,$//g' | sed 's/,/ /g'
推荐答案
对于计算,使用 awk
!
$ awk '{$(NF-1)=sprintf("%.0f", ($(NF-1) + $NF)/2 * 141); NF--}1' file
AJ29 IO_0_VRN_10 10945
AJ30 IO_L1P_T0_100M 12738
AJ31 IO_L1N_T0_100S 14367
AK29 IO_L2P_T0_101M 9376
AL29 IO_L2N_T0_101S 9016
这会将倒数第二个字段替换为 (penultimate*last)/2 * 141)
的结果.为了使它圆,我们使用 %.0f
格式,如 Awk printf 数字的宽度和圆起来.
This replaces the penultimate field with the result of (penultimate*last)/2 * 141)
. To make it round, we use %.0f
format as indicated in Awk printf number in width and round it up.
此外,在我看来,您正在处理太多事情:我计算了对 grep
的一次调用和对 sed
的 13 (!) 次调用.您可能可以使用 sed -e 'first block' -e 'second block' ...
代替.
Also, it looks to me that you are piping way too many things: I counted one call to grep
and 13 (!) to sed
. You can probably use sed -e 'first block' -e 'second block' ...
instead.
在awk
中,NF
是指当前行的字段数.由于 $n
指的是字段编号 n
,所以 $(NF-1)
我们指的是倒数第二个字段.
In awk
, NF
refers to the number of fields on the current line. Since $n
refers to the field number n
, with $(NF-1)
we refer to the penultimate field.
{...}1
做一些事情,然后打印结果行.1
评估为 True,任何 True 都会触发awk
执行其默认操作,即打印当前行.$(NF-1) + $NF)/2 * 141
执行计算:`(倒数第二+最后)/2 * 141{$(NF-1)=sprintf( ... )
将之前计算的结果分配给倒数第二个字段.将sprintf
与%.0f
结合使用,我们确保执行舍入,如上所述.- {...;NF--} 一旦计算完成,我们在倒数第二个字段中得到它的结果.要删除最后一列,我们只需说嘿,减少字段数"即可.以便删除"最后一个.
{...}1
do stuff and then print the resulting line.1
evaluates as True and anything True triggersawk
to perform its default action, which is to print the current line.$(NF-1) + $NF)/2 * 141
perform the calculation: `(penultimate + last) / 2 * 141{$(NF-1)=sprintf( ... )
assign the result of the previous calculation to the penultimate field. Usingsprintf
with%.0f
we make sure the rounding is performed, as described above.{...; NF--}
once the calculation is done, we have its result in the penultimate field. To remove the last column, we just say "hey, decrease the number of fields" so that the last one gets "removed".
这篇关于对包含数字的文件进行多项数学运算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!