Shell 在匹配模式前后将内容存储在数组中

Shell 在匹配模式前后将内容存储在数组中,shell,Shell,文件内容包括: 1 name linux unix --- he is going to learn 4 this is my second difference --- how to store each difference in a variable 对于(数字(1)和模式“---”)之间的每个内容,我需要将值存储在一个数组中:before和after 例如: echo$before[0] name linux unix [0]之后的echo$ he is going to learn

文件内容包括:

1
name
linux
unix
---
he is
going to
learn
4
this is my
second difference
---
how to store
each difference in a variable
对于(数字(1)和模式“---”)之间的每个内容,我需要将值存储在一个数组中:before和after

例如: echo$before[0]

name
linux
unix
[0]之后的echo$

he is
going to
learn
现在我们看到数字:4 现在我们需要移动数组的索引并将内容存储在数组中。 echo$before[1]

this is my
second difference
[1]之后的echo$

how to store
each difference in a variable
我对壳牌公司不熟悉。你能帮我吗? 提前谢谢

这是片段

i=0;
while read line;
do
if grep '[0-9].*' == $line // checks if it starts with a digit
then
$line=$line+1 ( from the next line before " ---" store it in base )
while "$line" != "---"
$line=$line+1
do
base[$i]=$line 
i=$(( i+1 ));
echo $base[$i]
done
fi
done <rit.log
echo "$base"
i=0;
读行时;
做
如果grep'[0-9].*'=$line//检查它是否以数字开头
然后
$line=$line+1(从“--”之前的下一行开始,将其存储在base中)
而“$line”!="---"
$line=$line+1
做
基本[$i]=$line
i=$((i+1));
echo$base[$i]
完成
fi
完成
#/usr/bin/env bash
前=()后=()
del=$“\n”在=1 i=-1之后是_
而IFS=读取-r行;做
[$line=~^[0-9]$]&&((i++)
[[$line=~^([0-9]+|--)$]&&{((is_after=!is_after));继续;}
如果((后面是_));然后
[i]+=$line$del之后
其他的
在[i]+=$line$del之前
fi
完成<数据
#修剪最后一个分隔符
after=(“${after[@]]?}”)
before=(“${before[@]]?}”)

我有点困惑。这些数字是什么?它是起始索引吗?所以[1]
之后的
他是
,[5]
之前的
第二个差异
等等,或者这些数字是没有意义的,你只是想让所有的第一句话(在
--
之前)进入[0]
之前的
等等?我可以用python来做,但是我们的服务器有非常低端的模块模块。所以我必须坚持到底。我有一个逻辑,因为我花了几个小时来实现它。你能帮我吗?文件内容实际上是(diff file1和file2)的结果。diff结果以这种方式给出输出。(行号)@PesaThe这些数字实际上显示了从该行号开始的两个文件内容的差异。当数字为1时:从1到--(即文件1的内容)之后的内容,以及下一个数字(即4)之前“--”之后的内容是文件2的内容。因此,它们基本上没有意义。您是否需要存储在单独数组元素中的行,或者[0]
之前的单个元素
应该包含
name\nlinux\nunix
,例如[1]
之后的
应该包含最后两句话?
#!/usr/bin/env bash

before=() after=()
del=$'\n' is_after=1 i=-1

while IFS= read -r line; do
    [[ $line =~ ^[0-9]$ ]] && ((i++))
    [[ $line =~ ^([0-9]+|---)$ ]] && { ((is_after=!is_after)); continue; }

    if ((is_after)); then
        after[i]+=$line$del
    else
        before[i]+=$line$del
    fi
done < data

# trim the last delimiter
after=("${after[@]%?}")
before=("${before[@]%?}")