直接在 AWK 中设置 BASH 环境变量(在 AWK 一行中)

发布于 2024-09-06 16:31:55 字数 561 浏览 3 评论 0原文

我有一个包含两列浮点值的文件。我还有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

终遇你 2024-09-13 16:31:55

你只需在 awk 中完成所有操作

awk '{cmd="c_program "$1; cmd|getline l;print $1,$2-l}' file

you just do everything in awk

awk '{cmd="c_program "$1; cmd|getline l;print $1,$2-l}' file
杀お生予夺 2024-09-13 16:31:55

这显示了如何在 awk 中执行命令:

ls | awk '/^a/ {system("ls -ld " $1)}'

您可以改用 bash 脚本:

while read line
do 
    FIRST=`echo $line | cut -d' ' -f1`
    SECOND=`echo $line | cut -d' ' -f2`
    OUT=`expr $SECOND \* 4`
    echo $FIRST $OUT `expr $OUT - $SECOND`
done

This shows how to execute a command in awk:

ls | awk '/^a/ {system("ls -ld " $1)}'

You could use a bash script instead:

while read line
do 
    FIRST=`echo $line | cut -d' ' -f1`
    SECOND=`echo $line | cut -d' ' -f2`
    OUT=`expr $SECOND \* 4`
    echo $FIRST $OUT `expr $OUT - $SECOND`
done
尝蛊 2024-09-13 16:31:55

shell 是一个更好的工具,它使用了一些常用的功能。 shell中有一个变量IFS,它是sh在解析时用来分割命令行的输入字段分隔符;它默认为 这就是为什么 ls foo 被解释为两个单词。

set 被赋予- 开头的参数时,它会将 shell 的位置参数设置为通过 IFS 分割的参数内容,因此:

#!/bin/sh
while read line ; do
    set $line
    subtrahend=`c_program $1`     
    echo $1 `expr $2 - $subtrahend`
done < data_file

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 why ls 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:

#!/bin/sh
while read line ; do
    set $line
    subtrahend=`c_program $1`     
    echo $1 `expr $2 - $subtrahend`
done < data_file
海夕 2024-09-13 16:31:55

纯 Bash,除了您的程序之外不使用任何外部可执行文件:

#!/bin/bash
while read num1 num2
do
    (( result = $(c_program num2) - num1 ))
    echo "$num1 $result"
done

Pure Bash, without using any external executables other than your program:

#!/bin/bash
while read num1 num2
do
    (( result = $(c_program num2) - num1 ))
    echo "$num1 $result"
done
爺獨霸怡葒院 2024-09-13 16:31:55

正如其他人指出的那样:awk 并没有很好地胜任这项工作。这是 bash 中的建议:

#!/bin/sh

data_file=$1

while read column_1 column_2 the_rest
do

    ((result=$(c_program $column_1)-$column_2))
    echo $column_1 $result "$the_rest"

done < $data_file

将其保存到文件中,例如 myscript.sh,然后将其调用为:

sh myscript.sh data_file

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:

#!/bin/sh

data_file=$1

while read column_1 column_2 the_rest
do

    ((result=$(c_program $column_1)-$column_2))
    echo $column_1 $result "$the_rest"

done < $data_file

Save this to a file, say myscript.sh, then invoke it as:

sh myscript.sh data_file

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文