Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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
Arrays shell脚本从两个字符串之间的文件中获取字符串_Arrays_String_Shell_Awk_Sed - Fatal编程技术网

Arrays shell脚本从两个字符串之间的文件中获取字符串

Arrays shell脚本从两个字符串之间的文件中获取字符串,arrays,string,shell,awk,sed,Arrays,String,Shell,Awk,Sed,我需要以这样的方式提取文件中存在的字符串:input=之后的字符串到output之前的字符串 在下面指定的示例中,我有三个测试用例,我想将字符串存储在一个数组中 array[0]="3 45 60" array[1]="10 62 12" array[2]="5 72 12 67 89" 示例 File.txt case=test case 1 input=3 45 60 output=Enter the age: Meenu got 27 coins case=test case 2 in

我需要以这样的方式提取文件中存在的字符串:input=之后的字符串到output之前的字符串 在下面指定的示例中,我有三个测试用例,我想将字符串存储在一个数组中

array[0]="3
45
60"
array[1]="10
62
12"
array[2]="5
72
12
67
89"
示例 File.txt

case=test case 1
input=3
45
60
output=Enter the age:
Meenu got 27 coins

case=test case 2
input=10
62
12
output=Enter the age:
Meenu got 1000 coins

case=test case 3
input=5
72
12
67
89
output=Enter the age:
Meenu got 125 coins

无论如何,这里有一个快速而肮脏的解决方案。谢谢你给我一个练习awk的机会。它适用于
gawk

awk '/^case/, /^output/ {
#get case number
    if($0 ~ /^case/) {
        i=1
        s=""
        split($0, a)
        c = int(a[3]) - 1
    }
#get first input
    if($0 ~ /^input/) {
        split($0, b, "=")
        arr[i++]=int(b[2])
    }
#get other inputs
    if($0 ~ /^[0-9]+/) {
        arr[i++]=int($0)
    }
#print formatted string
    if($0 ~ /^output/) {
        for(j=1;j<i-1;j++){
            s=s""arr[j]"\n"
        }
        s=s""arr[i-1]"\"\n"
        printf("array[%d]=\"%s", c, s)
    }
}' File.txt
awk'/^case/,/^output/{
#获取案例编号
如果($0~/^case/){
i=1
s=“”
拆分(每年0美元)
c=int(a[3])-1
}
#获取第一个输入
如果($0~/^input/){
拆分($0,b,“=”)
arr[i++]=int(b[2])
}
#获取其他输入
如果($0~/^[0-9]+/){
arr[i++]=int($0)
}
#打印格式化字符串
如果($0~/^output/){

对于(j=1;jOops,你忘了发布代码了!StackOverflow是帮助人们修复代码。它不是免费的编码服务。任何代码都比没有代码好。即使你不知道如何编写程序,元代码也会证明你认为程序应该如何工作。考虑到你包含的标记,我希望看到shell、awk和在你的问题中使用sed代码。