Perl 使用Awk处理数组
我有两个输入文件,一个只包含数字,例如 range.txtPerl 使用Awk处理数组,perl,shell,unix,sed,awk,Perl,Shell,Unix,Sed,Awk,我有两个输入文件,一个只包含数字,例如 range.txt 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 2s 4m 1s 10m 另一个文件具有这些要求。例如 requirements.txt 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 2s
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2s 4m
1s 10m
另一个文件具有这些要求。例如
requirements.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2s 4m
1s 10m
这意味着2套4人一组和1套10人一组
输出应该如下所示:
There is 1 apple;
There are 2 mangoes and 1 apple;
There are 3 mangoes and 1 apple;
There are 4 mangoes and 1 apple;
There is 5 apple;
There are 6 mangoes and 5 apple;
There are 7 mangoes and 5 apple;
There are 8 mangoes and 5 apple;
There is 9 apple;
There are 10 mangoes and 9 apple;
There are 11 mangoes and 9 apple;
There are 12 mangoes and 9 apple;
There are 13 mangoes and 9 apple;
There are 14 mangoes and 9 apple;
There are 15 mangoes and 9 apple;
There are 16 mangoes and 9 apple;
There are 17 mangoes and 9 apple;
There are 18 mangoes and 9 apple;
如何使用awk和shell脚本(甚至perl)实现这一点?我们拥有的awk版本是/usr/xpg4/bin/awk。我不熟悉阵列,因此需要一些帮助。谢谢 PS:刚刚更新了在芒果“值”后面附加苹果“值”的要求。
awk'
{
集合=0+1美元
mbrs=0+2美元
对于(i=1;i和awk
:
awk -F "[sm]" '{ for (set=1; set<=$1; set++) {
getline ct < "range.txt"
print "There is " ct " apple;"
for (member=1; member<=$2; member++) {
getline ct < "range.txt"
print "There are " ct " mangoes;"
}
}
}' requirements.txt
awk-F“[sm]”{for(set=1;set很难理解这两个文件是如何结合在一起形成输出的。你能试着进一步澄清一下吗?Florin,o/p文件只是使用requirements.txt和range.txt文件生成的值。+1用于理解需求和回答它。感谢Glenn,这一个也很好地工作了。不过,我只需要如下图所示,将苹果“计数”添加到“芒果”计数之后。希望这不是什么大不了的事!这应该不是什么大不了的事。事实上,我将把它作为练习留给读者;)你所要做的就是记住打印苹果行时的值,并将其添加到每个芒果的输出中。你需要做一些工作。谢谢Glenn!我将开始研究阵列。这是一个很好的开始。感谢你的帮助。Greg,这非常有效。但是我刚刚意识到我们需要稍微不同的o/pt格式,我们将“苹果”计数附加到“芒果”计数,如上图所示(对不起,我无法更好地缩进!)我希望这不会是对脚本的重大更改。