[英]Bash, how can I apply one arithmetic expression to every line?
I have two scripts, A and B. 我有两个脚本,A和B。
I want to execute them and read respectively two values. 我想执行它们并分别读取两个值。 V and VALS.
V和VALS。
V is just a floating point number, let's say 0.5 V只是一个浮点数,比方说0.5
VALS has the following format: VALS具有以下格式:
1 10
2 20
3 60
4 45
and so on. 等等。
What I'm trying to do is to get a new variable where the second column of VALS (10, 20, ...) is divided by V. 我想做的是获取一个新变量,其中VALS的第二列(10,20,...)除以V。
As I understand this can be implemented with a mix of xargs and cut but I'm not really familiar with these tools. 据我了解,可以同时使用xargs和cut来实现,但是我对这些工具并不真正熟悉。
#!/bin/bash
V=`./A`
VALS=`./B`
RESULT=#magic happens
The final result with the previous data should be: 先前数据的最终结果应为:
1 20
2 40
3 120
4 90
Bash's builtin arithmetic expansion only works for integers. Bash的内置算术扩展仅适用于整数。 You can use
awk
for data extraction and floating point numbers. 您可以将
awk
用于数据提取和浮点数。
V=`./A`
# No VALS needed
RESULT=($(./B | awk "{print \$2 / $V"}))
Note the escaped dollar sign in \\$2
. 请注意
\\$2
的转义美元符号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.