2013-03-20 59 views
1

指数,我想知道是否有做在bash下面的一个优雅的方式:匹配值根据在bash

我需要检查列表一定价值,让称它为“1”。对于我找到这个值的每个条目,我需要在另一个列表中累积一个匹配的字符串(具有相同的索引),并最终将其打印出来。

例如: 让我们假设值的列表是"1 0 1 1 "

和字符串列表是"What a wonderful day"

所以输出字符串将"What wonderful day"

感谢

+0

列表值如何与字符串关联?对于值“1”,这是如何与字符串绑定的? – suspectus 2013-03-20 09:35:01

+0

按其索引。如果索引0中有“1”,那么字符串列表中索引为0的字符串应累积起来 – 2013-03-20 09:49:43

+0

好的谢谢。所以在这个例子中,精彩的有索引2.这些列表是否存储在文件中? – suspectus 2013-03-20 09:52:55

回答

2

这里我的建议解决方案:

#!/bin/sh 
myMatch=1 #This is the value you're looking for 
myString="What a wonderful day"; 
myList=($myString) #String to Array conversion 
count=0; 
for i in [email protected]; do #Iterate over the input parameters 
    if [ $i -eq $myMatch ]; then 
     echo -n "${myList[$count]} " #use -n to avoid newline and append space as a separator 
     count=$(($count+1)) 
    fi 
done 

所以调用脚本给出的值的列表:

$ . myScript.sh 1 0 1 1 

你想要的结果。