sed命令

今天看了一篇文章sed 简明教程,里面将address的时候,感觉不太对。
sed 的Address:[address[,address]][!]{cmd}
这里给出伪代码:

bool bexec = false
foreach line in file {
    if ( match(address1) ){
        bexec = true;
    }
 
    if ( bexec == true) {
        EXEC(sed_cmd);
    }
 
    if ( match (address2) ) {
        bexec = false;
    }
}

我举一个例子:
一个文件中内容如下

a b
b

现在运行命令sed -n '/a/,/b/p' file,如果按照上面的伪代码,应该输出
a b 结果输出了a b \n b。所以正确的逻辑应该是:

bool bexec = false
foreach line in file {
    if ( !bexec && match(address1) ){
        bexec = true;
        EXEC(sed_cmd);
        continue;
    }
 
    if ( bexec == true) {
        EXEC(sed_cmd);
    }
 
    if ( bexec && match (address2) ) {
        bexec = false;
    }
}
posted @ 2015-08-18 17:01  lhcpig  阅读(184)  评论(0编辑  收藏  举报