本文介绍了使用bash以类似字典的格式进行迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在输入文件中有一个类似python的字典:
I have a python like dictionary in an input file:
$ cat test.txt db={1:['a','b','c','d'], 2:['aa','bb','cc','dd']}字典中的每个列表只有4个索引,且该索引不小于或大于.我需要这样的结果:
Each list in dictionary only has 4 indexes not less or more. I need a result like:
one1="a" two1="b" three1="c" four1="d" one2="aa" two2="bb" three2="cc" four2="dd"如果我们在这里使用python,我知道这很简单,但是我应该在bash脚本中完成这项工作.是否可以?如何使用bash脚本完成这项工作?
I know this is simple if we use python here, but I should do the job in bash script. Is it possible? How can I do the job using bash script?
推荐答案您只需要剥离所有不必要的字符并循环遍历它们即可得到结果
you just need to strip off all the unnecessary characters and loop through them to get your result
#!/bin/bash db="{1:['a','b','c','d'], 2:['aa','bb','cc','dd']}" count=1 for items in `echo $db|sed 's/{//;s/}//'` do echo one${count} = `echo $items|sed 's/^.*\[//;s/\].*$//'|cut -d ',' -f1` echo two${count} = `echo $items|sed 's/^.*\[//;s/\].*$//'|cut -d ',' -f2` echo three${count} = `echo $items|sed 's/^.*\[//;s/\].*$//'|cut -d ',' -f3` echo four${count} = `echo $items|sed 's/^.*\[//;s/\].*$//'|cut -d ',' -f4` echo '' count=`expr $count + 1` done输出
one1 = 'a' two1 = 'b' three1 = 'c' four1 = 'd' one2 = 'aa' two2 = 'bb' three2 = 'cc' four2 = 'dd'更多推荐
使用bash以类似字典的格式进行迭代
发布评论