Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 使用bash从与另一个变量匹配的字符串变量中提取子字符串_String_Bash_Shell_Variables_Substring - Fatal编程技术网

String 使用bash从与另一个变量匹配的字符串变量中提取子字符串

String 使用bash从与另一个变量匹配的字符串变量中提取子字符串,string,bash,shell,variables,substring,String,Bash,Shell,Variables,Substring,Var1=$(对于i.am中的i/First:Statement i.am/Second:Statement i.am/First:句子i.am/First:Line i.am/Second.Line i.am/Third:Line;do echo$i;done) Var2=$(对于第一秒钟的j;执行echo$j;完成) 给定上述两个变量,我想从Var1中提取与Var2的子字符串匹配的子字符串 所以,在输出中,我想 I.am/First:Statement I.am/First:Sentence

Var1=$(对于i.am中的i/First:Statement i.am/Second:Statement i.am/First:句子i.am/First:Line i.am/Second.Line i.am/Third:Line;do echo$i;done)

Var2=$(对于第一秒钟的j;执行echo$j;完成)

给定上述两个变量,我想从Var1中提取与Var2的子字符串匹配的子字符串

所以,在输出中,我想

I.am/First:Statement
I.am/First:Sentence
I.am/First:Line 
I.am/Second:Statement
I.am/Second.Line

我试过以下方法

for r in $Var2;
do
  if [ -z "${Var1##*$r*}" ]
    then
      echo "$r"
  fi
done
这只打印

First
Second
有什么想法吗?
谢谢

请添加一个shebang,然后将您的脚本粘贴到,并尝试实现那里提出的建议。好的脚本按照编码的方式运行,您为每个“r”(第一个/第二个)说如果条件为真,则打印“r”…这就是它的作用。因此。。您想使用
grep
?比如
grep-f thanky you@Cyrus,这有助于纠正语法errors@KamilCuk-你能解释一下代码是如何工作的吗<代码>grep-f