如果文件中不存在的数组元素需要将其作为数组元素打印为零

时间:2016-03-04 06:40:37

标签: linux bash shell awk

考虑一个数组元素:

file

现在我需要检查文件中的数组元素。 将文件视为:

a 10 c 30

grep ${args[@]/#/-e } file

a 10 c 30

输出:

a 10


b 0


c 30

预期产出:

private void button1_Click(object sender, EventArgs e){
Microsoft.Office.Interop.Word.Application WordApp = null;  
WordApp = new Microsoft.Office.Interop.Word.Application(); //make the word application
documents = WordApp.Documents;
document = documents.Add(); //open a document in my created application
button2.visible=true;
}

private void button2_Click(object sender, EventArgs e){
button1.visible=false;
selection = WordApp.Selection;
inlineShapes = selection.InlineShapes;
inlineShape = inlineShapes.AddPicture(@"C:\MyPicture1.Jpeg"); // add picture to my created application document
}

2 个答案:

答案 0 :(得分:1)

如果找不到匹配项,我认为grep中没有选项可以打印字符串。

我会用以下脚本执行此操作:

for i in ${args[@]}; do
    grep $i file.txt
    if [ $? -ne 0 ]; then
        echo $i"  0"
    fi
done

答案 1 :(得分:0)

使用awkprocess substitution这更简单:

args=("a" "b" "c")

awk 'FNR==NR{a[$1]=$0; next} {
     print ($1 in a) ? a[$1] : $1, 0}' file <(printf "%s\n" "${args[@]}")

a 10
b 0
c 30