将awk发现的每个事件存储到数组中 [英] Store each occurence found by awk to an array
问题描述
我之前的问题被标记为重复",我被指向 this 和
My previous question was flagged "duplicate" and I was pointed to this and this. The solutions provided on those threads does not solve this at all.
file.txt的内容:
Content of file.txt:
Some line of text 0
Some line of text 1
Some line of text 2
PATTERN1
Some line of text 3
Some line of text 4
Some line of text 5
PATTERN2
Some line of text 6
Some line of text 7
Some line of text 8
PATTERN1
Some line of text 9
Some line of text 10
Some line of text 11
PATTERN2
Some line of text 12
Some line of text 13
Some line of text 14
我需要提取"PATTERN1"和"PATTERN2"在它们之间加上+行,并且以下命令可以完美地完成此操作:
I need to extract "PATTERN1" and "PATTERN2" + lines in between, and the following command does this perfectly:
awk'/PATTERN1//,/PATTERN2/'./file.txt
awk '/PATTERN1 /,/PATTERN2/' ./file.txt
输出:
PATTERN1
Some line of text 3
Some line of text 4
Some line of text 5
PATTERN2
PATTERN1
Some line of text 9
Some line of text 10
Some line of text 11
PATTERN2
但是现在我正在尝试创建一个bash脚本,该脚本:
But now I am trying to create a bash script that:
- 使用awk查找PATTERN1和PATTERN2之间的线
- 存储每次出现的PATTERN1 +之间的行+ PATTERN2 数组
- 执行1& 2,直到文件结尾.
- uses awk to find the lines between PATTERN1 and PATTERN2
- store each occurrence of PATTERN1 + lines in between + PATTERN2 in an array
- does 1 & 2 until the end of file.
要澄清.意思是在引号内存储以下行:
To clarify. Means store the following lines inside the quotes:
"PATTERN1
Some line of text 3
Some line of text 4
Some line of text 5
PATTERN2"
到array[0]
并在引号内存储以下行:
and store the following lines inside the quotes:
"PATTERN1
Some line of text 9
Some line of text 10
Some line of text 11
PATTERN2"
到array[1]
以此类推.....如果出现PATTERN1和PATTERN2的次数更多
and so on..... if there are more occurrence of PATTERN1 and PATTERN2
我目前拥有的东西:
#!/bin/bash
var0=`cat ./file.txt`
mapfile -t thearray < <(echo "$var0" | awk '/PATTERN1 /,/PATTERN2/')
以上操作无效.
而且,我尽可能不使用mapfile,因为该脚本可能在不支持该文件的系统上执行.
The above does not work.
And as much as possible I do not want to use mapfile, because the script might be executed on a system that does not support it.
基于提供的此链接:
myvar=$(cat ./file.txt)
myarray=($(echo "$var0" | awk '/PATTERN1 /,/PATTERN2/'))
但是当我做echo ${myarray[1]}
我得到一个空白的答复.
I get a blank response.
当我做echo ${myarray[0]}
我得到:
PATTERN1
Some line of text 3
Some line of text 4
Some line of text 5
PATTERN2
PATTERN1
Some line of text 9
Some line of text 10
Some line of text 11
PATTERN2
回显${myarray[0]}
PATTERN1
Some line of text 3
Some line of text 4
Some line of text 5
PATTERN2
我echo ${myarray[1]}
PATTERN1
Some line of text 9
Some line of text 10
Some line of text 11
PATTERN2
任何帮助都会很棒.
推荐答案
根据 Charles 的建议...
while IFS= read -r -d '' x; do array+=("$x"); done < <(awk '
/PATTERN1/,/PATTERN2/ { if ( $0 ~ "PATTERN2" ) { x=$0; printf "%s%c",x,0; next }
print }' ./file.txt)
我重新格式化了.有点忙,难以阅读.
I reformatted it. It was getting kinda busy and hard to read.
并对其进行测试-
$: echo "[${array[1]}]"
[PATTERN1
Some line of text 9
Some line of text 10
Some line of text 11
PATTERN2]
顺便说一句,在数据元素中包含冗余的哨兵值对我来说似乎很奇怪,所以如果要剥离这些值:
As an aside, it seems very odd to me to include the redundant sentinel values in the data elements, so if you want to strip those:
$: while IFS= read -r -d '' x; do array+=("$x"); done < <(
awk '/PATTERN1/,/PATTERN2/{ if ( $0 ~ "PATTERN1" ) { next }
if ( $0 ~ "PATTERN2" ) { len--;
for (l in ary) { printf "%s%c", ary[l], l<len ? "\n" : 0; }
delete ary; len=0; next }
ary[len++]=$0;
}' ./file.txt )
$: echo "[${array[1]}]"
[Some line of text 9
Some line of text 10
Some line of text 11]
这篇关于将awk发现的每个事件存储到数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!