我需要提取可以在一行中任何地方出现的序列号。序列号始终具有以下正则表达式格式:
S[XN]00-[0-9]{3}
Run Code Online (Sandbox Code Playgroud)
输出:
SX00-123
Run Code Online (Sandbox Code Playgroud)
我echo只想要这个。我可以grep使用序列号,并且正在考虑使用正则表达式,但不确定如何使用。
我写了(从谷歌复制并粘贴并简化)一个C程序,使用getopt打印出从Unix命令行传入的参数值.
从Unix命令行:
./myprog -a 0 -b 1 -c 2
Run Code Online (Sandbox Code Playgroud)
我的C代码是:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
int i;
while ((i = getopt(argc, argv, "abc")) != -1) {
switch (i) {
case 'a':
printf("A = %s\n", optarg);
break;
case 'b':
printf("B = %s\n", optarg);
break;
case 'c':
printf("C = %s\n", optarg);
break;
default:
break;
}
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我想编程打印出传递的每个值,例如
A = 0
B = 1
C = 2
Run Code Online (Sandbox Code Playgroud)
但它根本不打印任何东西.
如果存在文本块,我想执行命令,即
if [ multiple line string exists ]; then
<execute command>
fi
Run Code Online (Sandbox Code Playgroud)
有问题的多行字符串之一是以下所有内容:
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
Run Code Online (Sandbox Code Playgroud)
正则表达式可用于本文的某些内容,但我需要确保此失败与此测试相对应。FAILED 行在所有 LED 测试中都很常见,因此我希望只搜索确切的文本块。
如何搜索多行字符串?我正在考虑将该字符串放入变量中,例如
string1=" Which test would you like to run? 3
[led.test] …Run Code Online (Sandbox Code Playgroud)