[英]Why doesn't this sed statement work?
I have been working with regular expressions for the past couple days in class. 我在课堂上过去几天一直在使用正则表达式。 I cannot determine why this expression doesn't work.
我无法确定为什么这个表达式不起作用。 I am looking for expressions like the following:
我正在寻找以下表达式:
value = (struct node){ 5, NULL};
value2 = (structname){ 1, 2 };
*pointer = ( bla ){3,4};
Here is the expression I am using: 这是我正在使用的表达式:
sed -n '/^[*0-9A-Za-z_]+ *= *( *[0-9A-Za-z_]+ *[0-9A-Za-z_]* *) *{[0-9A-Za-z, ]};/p' structfile
What am I missing because it returns nothing. 我错过了什么,因为它什么也没有回报。 Also a side note I have used [^,] in some expressions and I still get lines with , s.
另外一个侧面说明我在某些表达式中使用[^,]并且我仍然使用s得到行。 What am I missing?
我错过了什么?
You need to escape +
; 你需要逃避
+
; and fix {[0-9A-Za-z, ]}
to {[0-9A-Za-z, ]*}
. 并将
{[0-9A-Za-z, ]}
修复为{[0-9A-Za-z, ]*}
。
sed -n '/^[*0-9A-Za-z_]\+ *= *( *[0-9A-Za-z_]\+ *[0-9A-Za-z_]* *) *{[0-9A-Za-z, ]*};/p' structfile
me@localhost:~$ cat structfile
value = (struct node){ 5, NULL};
value2 = (structname){ 1, 2 };
*pointer = ( bla ){3,4};
not astruct
notastructstr =
me@localhost:~$ sed -n '/^[*0-9A-Za-z_]\+ *= *( *[0-9A-Za-z_]\+ *[0-9A-Za-z_]* *) *{[0-9A-Za-z, ]*};/p' structfile
value = (struct node){ 5, NULL};
value2 = (structname){ 1, 2 };
*pointer = ( bla ){3,4};
This might work for you (GNU sed): 这可能适合你(GNU sed):
sed '/^\S\+\s\+=\s\+(\s*\S\+\(\s\+\S\+\)\?\s*){\s*\S\+\s*,\s*\S\+\s*};/!d' file
\\s
), non-whitespace ( \\S
) and literals ( =
, (
, )
, {
, }
and ;
) \\s
),非空格( \\S
)和文字( =
, (
, )
, {
, }
和;
)构建一个松散的正则表达式 \\s\\+
, zero-or-more non-whitespace would be \\S*
and zero-or-one grouped would be \\(\\s\\+\\S\\+\\)\\?
\\s\\+
,零或多个非空格将是\\S*
和零或一组分组将是\\(\\s\\+\\S\\+\\)\\?
^
for start of string, $
end of string and in a multiline regexp \\n
for newline ^
作为字符串的开头,字符串的$
end和多行regexp \\n
中的新行来锚定正则表达式 \\{1,3\\}
) or 3 ( \\{3\\}
) of course a single class, literal or group need not be qualified \\{1,3\\}
)或3( \\{3\\}
)当然单个类,文字或组不需要合格 HTH HTH
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.