我是shell脚本的新手。谁能告诉我上面拆分文字的方法。 这里我需要的值,如果真的可以在shell脚本中存储在数组中?
=============示例文本=====================
Reading application configuration data...
Beginning interaction for module default...
default: ['1', '52', '53', '54', 55-vm, '55', '56', '57', '58', '59', '60', '61',
'62', '63', '65', '66', '67', ah-builtin-python-bundle, export-backend, export,
lab, stage-test, test]
Success.
Cleaning up temporary files for module default...
============================================
============== RESULT ========================
['1', '52', '53', '54', 55-vm, '55', '56', '57', '58', '59', '60', '61',
'62', '63', '65', '66', '67', ah-builtin-python-bundle, export-backend, export,
lab, stage-test, test]
============================================
答案 0 :(得分:1)
这应该让你开始。但是它只会提取单行上的数组,只有每行只有一个数组时它才有用。
ARRAYTXT=$(sed -nr 's/.*(\[.*\]).*/\1/p' file.txt)
ARRAY=($(echo $ARRAYTXT | sed -r "s/'([^']*)'/\1/g;s/([^,]*),/\1/g;s/\[//;s/\]//"))
echo ${a[*]}
结果:
1 52 53 54 55-vm 55 56 57 58 59 60 61 62 63 65 66 67 ah-builtin-python-bundle export-backend export lab stage-test test
答案 1 :(得分:1)
我使用awk转换数据,并将其存储在数组中
ary=( $(
awk -v RS= '
match($0, /default: \[(.+)\]/, a) { gsub(/[\n,]/,"",a[1]); print a[1] }
' sample.txt
) )
并验证数组的内容:
$ for i in "${!ary[@]}"; do echo "$i ${ary[i]}"; done
0 '1'
1 '52'
2 '53'
3 '54'
4 55-vm
5 '55'
6 '56'
7 '57'
8 '58'
9 '59'
10 '60'
11 '61'
12 '62'
13 '63'
14 '65'
15 '66'
16 '67'
17 ah-builtin-python-bundle
18 export-backend
19 export
20 lab
21 stage-test
22 test