Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 从awk导出变量_Bash_Variables_Awk_Export - Fatal编程技术网

Bash 从awk导出变量

Bash 从awk导出变量,bash,variables,awk,export,Bash,Variables,Awk,Export,我有一个相当简单的脚本 #!/bin/bash awk -v channels=4096 '{for (i=1;i<=NF;i++) $i=$i-(i-1)*channels} END{printf "There are %d detectors\n ",detectors=NF;}1' $1.dat >$1_Processed.dat rm -f $1.dat printf "There are %d detectors\n",$detectors exit 0 典型的输入文件

我有一个相当简单的脚本

#!/bin/bash

awk -v channels=4096 '{for (i=1;i<=NF;i++) $i=$i-(i-1)*channels} END{printf "There are %d detectors\n ",detectors=NF;}1' $1.dat >$1_Processed.dat
rm -f $1.dat
printf "There are %d detectors\n",$detectors
exit 0
典型的输入文件是

Kmax Event File - Text Format
1 6 1000 
1623 5730 8209 12307 16398 20495 
1505 5611 8208 12306 16395 20497 
934 5036 8208 12308 16399 20498 
1552 5656 8208 12306 16396 20495 
1622 5729 8208 12305 16397 20495 
1526 5631 8207 12305 16397 20496 
1522 5629 8208 12307 16396 20497 
1525 5630 8208 12306 16399 20496 
1625 5730 8208 12305 16396 20496 
1717 5822 8207 12305 16398 20495 
1690 5796 8209 12306 16397 20496 
1525 5630 8209 12305 16399 20495 
1625 5731 8208 12305 16397 20495 
1523 5629 8209 12307 16398 20497 
1719 5825 8208 12307 16398 20495 
1714 5821 8207 12305 16395 20496 
1525 5630 8208 12306 16399 20495 
1528 5634 8209 12306 16396 20496 
1624 5729 8208 12307 16397 20496 
1625 5729 8208 12305 16397 20495 
1524 5630 8209 12305 16397 20495 
1393 5497 8207 12306 16397 20495

你可能想要这个:

detectors=$(
    awk -v channels=4096 '
        {for (i=1;i<=NF;i++) $i=$i-(i-1)*channels; print} 
        END {print NF > "/dev/stderr"}
    ' $1.dat 2>&1 >$1_Processed.dat
)
表单
print NF>“/dev/stderr”
可能特定于GNU awk

我注意到第二行的第二个字段也是6。如果这是探测器的数量,那么提取它是一种更明显的方法

detectors=$( awk 'NR==2 {print $2; exit}' $1.dat )
# or
read a detectors b < <(sed -n '2{p;q}' $1.dat)
detectors=$(awk'NR==2{print$2;exit}'$1.dat)
#或

阅读a和b<请澄清您希望awk脚本输出的内容-您不能在其中做所有的数学运算,而只是在最后打印
NF
。此外,您的整个shell脚本几乎可以更简洁地重新编写为awk脚本(shell是调用工具的环境,awk是处理文本文件的标准UNIX工具),因此如果您告诉我们整个脚本的用途,并提供一些小但有代表性的示例输入和预期输出,我们可以帮你。给定输入文件,探测器的数量是多少?在结束块中,NF是文件最后一条记录中的字段数。在较大的脚本中,$detectors将始终为空:
detectors=$(awk-v channels=4096'blah blah blah blah blah'$1.dat>$1_Processed.dat)
--将awk的输出重定向到一个文件,所以变量没有输出可捕获。@glennjackman:在前面提到的输入文件中,检测器的数量应该是6。如何保存输出文件和变量?非常感谢您的回答!这真的救了我一天!非常感谢你的帮助!我正在试着理解
2>&1
做什么,但恐怕我不能!还有什么是“fd”?fd是“文件描述符”,它是一个表示打开文件的整数。stdin是文件描述符0,stdout是fd1,stderr是fd2。在您的计算机上,尝试以下命令:
ll-d/dev/{std*,fd}
。在我的解决方案中,我需要将逐行输出发送到一个位置,将结束打印输出发送到另一个位置,这样awk的stdout被重定向到一个文件,而stderr被重定向到stdout,以便变量可以获取它。除了秩序问题。
$ x=$(awk 'BEGIN {print "stdout"; print "stderr" > "/dev/stderr"}' 2>&1 >/tmp/foo)
$ echo "$x"
stderr
$ cat /tmp/foo
stdout
detectors=$( awk 'NR==2 {print $2; exit}' $1.dat )
# or
read a detectors b < <(sed -n '2{p;q}' $1.dat)