- 论坛徽章:
- 0
|
本帖最后由 可可火山 于 2012-10-01 18:13 编辑
看了下 awk FAQ http://awk.freeshell.org/Frequently_Asked_Questions
正则中用变量需要显示并且不要 /***/括起来。
5. How do I use a variable as a regular expression?
The patterns between slashes like /pattern/ are called ERE constants, or regular expressions literals. As the names imply, they can only contain fixed, constant regular expressions. If you have a variable var that contains "abc(123)?r+" and try to match something against /var/, you are matching against the literal string "var", not against the regular expression. You can still use strings in places where regular expressions are expected, like this:
var="abc(123)?r+"
if ($1 ~ var){ # $1 matches, do something }
or
BEGIN{var="abc(123)?r+"}
$0 ~ var { # $0 matches, do something }
Also note that when you're using a string as a regular expression you must explicitly match it against the string you want to check; you can NOT use the string alone and expect awk to understand that you mean $0 ~ string, as happens instead for RE literals. Finally, using a string as a regex produces what's called a "computed" or "dynamic" regex. For a detailed discussion of computed regexes and the issues you should be aware of when using them, see the GNU awk manual.
我改了下脚本,基本满足要求,- {
- if(PRELINE==$0){
- print $0;
- }
- if($0 ~ REGEXRULE && NR>1 ){
- print PRELINE "\n" $0;
- }
- PRELINE=$0;
- REGEXRULE=$0" -s [0-9]*";
- }
复制代码 接下去就是让脚本更强壮些,
如果同名重复的脚本超过2个。
比如带-s参数的相同脚本带起了2个以上。
|
|