【发布时间】:2014-09-02 15:49:33
【问题描述】:
如果存在文本块,我想执行一个命令,即
if [ multiple line string exists ]; then
<execute command>
fi
有问题的多行字符串之一是以下所有内容:
Which test would you like to run? 3
[led.test]
: This test will light LEDs 1 and 2
:
: LED1 LED2
: --------------
: ON OFF
: OFF ON
:
------> : Did the LEDs light up as indicated above? (y/n):n:
FAILED : User indicates LEDs did not light up properly
正则表达式可用于某些文本,但我需要确保此失败与此测试相对应。 FAILED 行对于所有 LED 测试都很常见,因此我希望只搜索确切的文本块。
如何搜索多行字符串?我正在考虑将该字符串放入变量中,例如
string1=" Which test would you like to run? 3
[led.test]
: This test will light LEDs 1 and 2
:
: LED1 LED2
: --------------
: ON OFF
: OFF ON
:
------> : Did the LEDs light up as indicated above? (y/n):n:
FAILED : User indicates LEDs did not light up properly"
然后以某种方式检查 string1 是否在文件中。如果它存在,那么我将执行一个命令,例如echo "字符串存在"
【问题讨论】:
-
你的问题是什么?
-
@AmalMurali 我正在尝试编写一个条件语句来搜索文件中的多行字符串,但我无法做到。
-
好的,但是你到底想做什么? “编写条件语句以在文件中搜索多行字符串”描述性不够;至少对我来说。
-
恕我直言,您需要匹配此类字符串的设计并不是最好的。再次尝试重新思考问题,并选择更好的方法。无论如何,多行匹配是可能的,例如使用
perl。 -
if [[ $(<file.txt) = *"$string1"* ]]; then...
标签: regex string bash multiline