- 论坛徽章:
- 0
|
如何找到包含特殊字符的行,然后删除此行?
原帖由 "unixplus" 发表:
比如我要找到包含/*的注释行,然后删除,怎么做?
我用shell不多,不太会,请各位多帮忙,谢了
- #!/bin/sh
- test $# -lt 2 && echo "Usage: $0 [-d] FILE" && exit 1
- if [ x"$1" = "x-d" ]; then
- PARA=''
- PRNT='d'
- shift
- else
- PARA='-n'
- PRNT='p'
- fi
- sed $PARA "{
- /\/\/.*/p
- /\/\*/!b
- :a
- /\*\//!{
- N
- ba
- }
- $PRNT
- }" $1
复制代码
使用,假定该脚本命名为 a.sh
1. 只显示注释行:
$ ./a.sh file.c
/******************************************************************
* program
* hehe
******************************************************************/
/* pw = getpwnam(argv[1]);
enc = crypt( argv[2], pw->pw_passwd );
*/
2. 屏蔽所有注释行
$ ./a.sh -d file.c
#include <pwd.h>
#include <crypt.h>
#include <sys/types.h>
#include <string.h>
#include <stdio.h>
int main ( int argc, char *argv[] ) {
char *enc;
struct passwd *pw;
...
}
exit(0);
}
如果要输出到文件中只要在命令中用 > newfile 就可以了 |
|