直接在 AWK 中设置 BASH 环境变量(在 AWK 一行中)
我有一个包含两列浮点值的文件。我还有一个 C 程序,它采用浮点值作为输入并返回另一个浮点值作为输出。
我想做的是:对于原始文件中的每一行,使用第一列中的值作为输入执行 C 程序,然后打印出第一列(不变),然后打印出第二列减去结果C 程序的。
例如,假设 c_program 返回输入的平方,其行为如下:
$ c_program 4
16
$
并假设 data_file 如下所示:
1 10
2 11
3 12
4 13
在本例中,我想作为输出返回的是
1 9
2 7
3 3
4 -3
为了用非常粗略的伪代码编写这个,我想做这样的事情:
awk '{print $1, $2 - `c_program $1`}' data_file
但是,当然,我不能只传递 $1,awk 变量,进入对 c_program 的调用。执行此操作的正确方法是什么,最好是如何在保留“awk one-liner”的同时执行此操作? (我不想拿出大锤并编写一个成熟的 C 程序来做到这一点。)
I have a file that has two columns of floating point values. I also have a C program that takes a floating point value as input and returns another floating point value as output.
What I'd like to do is the following: for each row in the original, execute the C program with the value in the first column as input, and then print out the first column (unchanged) followed by the second column minus the result of the C program.
As an example, suppose c_program returns the square of the input and behaves like this:
$ c_program 4
16
$
and suppose data_file looks like this:
1 10
2 11
3 12
4 13
What I'd like to return as output, in this case, is
1 9
2 7
3 3
4 -3
To write this in really sketchy pseudocode, I want to do something like this:
awk '{print $1, $2 - `c_program $1`}' data_file
But of course, I can't just pass $1, the awk variable, into a call to c_program. What's the right way to do this, and preferably, how could I do it while still maintaining the "awk one-liner"? (I don't want to pull out a sledgehammer and write a full-fledged C program to do this.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你只需在 awk 中完成所有操作
you just do everything in awk
这显示了如何在 awk 中执行命令:
您可以改用 bash 脚本:
This shows how to execute a command in awk:
You could use a bash script instead:
shell 是一个更好的工具,它使用了一些常用的功能。 shell中有一个变量
IFS
,它是sh在解析时用来分割命令行的输入字段分隔符;它默认为
这就是为什么ls foo
被解释为两个单词。当
set
被赋予不以-
开头的参数时,它会将 shell 的位置参数设置为通过 IFS 分割的参数内容,因此:The shell is a better tool for this using a little used feature. There is a shell variable
IFS
which is the Input Field Separator that sh uses to split command lines when parsing; it defaults to<Space><Tab><Newline>
which is whyls foo
is interpreted as two words.When
set
is given arguments not beginning with-
it sets the positional parameters of the shell to the contents of the arguments as split via IFS, thus:纯 Bash,除了您的程序之外不使用任何外部可执行文件:
Pure Bash, without using any external executables other than your program:
正如其他人指出的那样:awk 并没有很好地胜任这项工作。这是 bash 中的建议:
将其保存到文件中,例如 myscript.sh,然后将其调用为:
read 命令从数据文件(已重定向到标准输入)中读取每一行,并将前 2 列分配给 $ column_1 和 $column_2 变量。该行的其余部分(如果有)存储在 $the_rest 中。
接下来,我根据您的要求计算结果,并根据您的要求打印出该行。请注意,我用引号将 $the_rest 括起来以保留间距。如果不这样做,将导致输入文件中的多个空格被压缩为一个。
As others have pointed out: awk is not not well equipped for this job. Here is a suggestion in bash:
Save this to a file, say myscript.sh, then invoke it as:
The read command reads each line from the data file (which was redirected to the standard input) and assign the first 2 columns to $column_1 and $column_2 variables. The rest of the line, if there is any, is stored in $the_rest.
Next, I calculate the result based on your requirements and prints out the line based on your requirements. Note that I surround $the_rest with quotes to reserve spacing. Failure to do so will result in multiple spaces in the input file to be squeezed into one.