LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
12
返回列表 发新帖
楼主: xqkz

将注释从php文件中提取[基本解决]

[复制链接]
发表于 2004-4-6 20:03:02 | 显示全部楼层

  1. [root@home root]# cat gawkscript
  2. BEGIN{
  3.         FS="\n"
  4.         flag=0
  5. }
  6. {
  7.         for(i=1;i<=NF;i++){
  8.                 if($i ~ /^\*\/$/){
  9.                         print $i
  10.                         flag=0
  11.                         continue
  12.                 }
  13.                 if(flag){
  14.                         print $i
  15.                         continue
  16.                 }
  17.                 if($i ~ /^\/\*\*$/){
  18.                         flag=1
  19.                         print $i
  20.                 }
  21.         }

  22. }
  23. [root@home root]# gawk -f gawkscript myfile
  24. /**
  25. * 得到错误分类
  26. *
  27. * @return int
  28. * @access public
  29. */
复制代码

javalee老大厉害,我的脚本很长,呵呵,解释一下你的脚本吧。

有时侯用sed解决不了或很难解决的问题,应该用awk来解决。
发表于 2004-4-6 20:17:59 | 显示全部楼层
最初由 home_king 发表
  1. [root@home root]# cat gawkscript
  2. BEGIN{
  3.         FS="\n"
  4.         flag=0
  5. }
  6. {
  7.         for(i=1;i<=NF;i++){
  8.                 if($i ~ /^\*\/$/){
  9.                         print $i
  10.                         flag=0
  11.                         continue
  12.                 }
  13.                 if(flag){
  14.                         print $i
  15.                         continue
  16.                 }
  17.                 if($i ~ /^\/\*\*$/){
  18.                         flag=1
  19.                         print $i
  20.                 }
  21.         }
  22. }
  23. [root@home root]# gawk -f gawkscript myfile
  24. /**
  25. * 得到错误分类
  26. *
  27. * @return int
  28. * @access public
  29. */
复制代码
javalee老大厉害,我的脚本很长,呵呵,解释一下你的脚本吧。

有时侯用sed解决不了或很难解决的问题,应该用awk来解决。

那里那里~~,
awk '/^*/||/^\//' urdoc #找出以*开头或以/开头的所有记录,shell贵在简炼,但是具体情况要具体分析
发表于 2004-4-6 21:26:13 | 显示全部楼层
我那个修改为:sed -n '/\/\*\*/,/\*\//p' old.php > new.php 即可
发表于 2004-4-6 21:46:09 | 显示全部楼层
我也给条题目大家做。
尝试把下面文件中的注释提取出来。

  1. /*This is the first
  2. comment segment.*/

  3. <a>[url]http://www.linuxsir.cn[/url]</a> /*well,this is the second
  4.                                  comment segment.
  5.                                  */
  6. <img>1.jpg</img>

  7. /*This is the third comment segment.*/


复制代码

  1. /*This is the first
  2. comment segment.*/
  3. /*well,this is the second
  4.   comment segment.
  5.   */
  6. /*This is the third comment segment.*/
复制代码
发表于 2004-4-7 01:37:00 | 显示全部楼层
sed:
  1. cat file|sed 's/<[a-z].*[a-z]>//g;s/^ //g;/^$/d'
复制代码
awk+tr:
  1. /home/javalee:cat file|awk '!/<[a-z].*[a-z]>/'|tr -d '\t'|tr -s ' |\n'
  2. /*This is the first
  3. comment segment.*/
  4. comment segment.
  5. */
  6. /*This is the third comment segment.*/
复制代码
发表于 2004-4-7 09:18:40 | 显示全部楼层
提取的注释好像不完整哦;另外,我要求"相对前导空格"必须与原文保持一致。
 楼主| 发表于 2004-4-7 13:08:01 | 显示全部楼层
我是来对地方了。这么多热心的兄弟,真让我感动得热泪盈眶啊。:thank :thank :p
发表于 2004-4-7 13:46:10 | 显示全部楼层
最初由 home_king 发表
提取的注释好像不完整哦;另外,我要求"相对前导空格"必须与原文保持一致。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
发表于 2004-4-7 14:01:09 | 显示全部楼层
嗯,一直想写个通用的提取C语言注释的脚本。
当然,我们GNU emacs能很轻易做到这一点。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表