免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 8846 | 回复: 76
打印 上一主题 下一主题

[文本处理] sed中 n命令的疑惑 [复制链接]

论坛徽章:
9
寅虎
日期:2013-12-02 12:49:22巨蟹座
日期:2014-07-18 18:13:34卯兔
日期:2014-07-25 14:43:08子鼠
日期:2014-07-26 10:10:51酉鸡
日期:2014-07-31 15:09:562015亚冠之本尤德科
日期:2015-05-19 09:27:382015亚冠之卡尔希纳萨夫
日期:2015-06-20 10:30:212015亚冠之阿尔纳斯尔
日期:2015-07-01 20:46:062015亚冠之城南
日期:2015-07-15 21:12:00
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-07-02 15:58 |只看该作者 |正序浏览
关于n命令 sed awk 101 hacks中是这样说的:

The sed n command lets you change that flow. The sed n command
will print the current pattern space, clear the current pattern space,
read the next line from the input-file, and continue the command
flow.

Let us assume that you have 2 sed commands before and 2 after the
n command as shown below.
sed-command-1
sed-command-2
n
sed-command-3
sed-command-4

In this case, sed-command-1 and sed-command-2 will be applied to
the current line in the pattern space; when sed encounters the n
command, it will clear the current line from the pattern space, read
the next line from the input-file, and apply sed-command-3 and sedcommand-
4 to this newly read line in the sed pattern space.

在实际试验中
sed -n n employee.txt     假如n有打印功能的话,为什么这里什么也不打印?
sed n employee.txt         假如n没有打印功能的话,这里为什么会打印所有行而不是偶数行?

论坛徽章:
0
77 [报告]
发表于 2014-07-04 13:08 |只看该作者
回复 63# lifayi2008

6楼说了那么嘲讽的话,还好意思说我是来吵架的,看来和你相比,我的脸皮还需要恶补啊

论坛徽章:
32
处女座
日期:2013-11-20 23:41:20双子座
日期:2014-06-11 17:20:43戌狗
日期:2014-06-16 11:05:00处女座
日期:2014-07-22 17:30:47狮子座
日期:2014-07-28 15:38:17金牛座
日期:2014-08-05 16:34:01亥猪
日期:2014-08-18 13:34:25白羊座
日期:2014-09-02 15:03:55金牛座
日期:2014-11-10 10:23:58处女座
日期:2014-12-02 09:17:52程序设计版块每日发帖之星
日期:2015-06-16 22:20:002015亚冠之塔什干火车头
日期:2015-06-20 23:28:22
76 [报告]
发表于 2014-07-04 11:05 |只看该作者
回复 74# blackold

原理这东西,实在不想研究太深,作为一个使用者,我知道怎么用就行了~
   

论坛徽章:
5
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:50:282015年亚洲杯之朝鲜
日期:2015-03-13 22:47:33IT运维版块每日发帖之星
日期:2016-01-09 06:20:00IT运维版块每周发帖之星
日期:2016-03-07 16:27:44
75 [报告]
发表于 2014-07-04 10:58 |只看该作者
回复 53# lifayi2008


  从源码看,n 没有直接改变控制流,它通过返值来让其它函数改变控制流。

论坛徽章:
5
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:50:282015年亚洲杯之朝鲜
日期:2015-03-13 22:47:33IT运维版块每日发帖之星
日期:2016-01-09 06:20:00IT运维版块每周发帖之星
日期:2016-03-07 16:27:44
74 [报告]
发表于 2014-07-04 10:56 |只看该作者
回复 54# yestreenstars

关键就是这里理解错了。

所谓的 auto-print 并没有专门的代码块(函数)来实现,由 no_default_output 所控制的
输出都称为 auto-print。

在源码中,它可能在这里,也可能在那里。

论坛徽章:
5
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:50:282015年亚洲杯之朝鲜
日期:2015-03-13 22:47:33IT运维版块每日发帖之星
日期:2016-01-09 06:20:00IT运维版块每周发帖之星
日期:2016-03-07 16:27:44
73 [报告]
发表于 2014-07-04 10:50 |只看该作者
之前把 auto-print 理解为sed处理流程中的固定一块代码,所有的auto-print都在这里处
理。

从源码看,auto-print 只是由全局变量 no_default_output 所控制的输出。

这样说来,q, N 也有打印功能。

而其它命令,如i和c命令, 的输出,不称为 auto-print, 因为不受 no_default_output
控制。

论坛徽章:
32
处女座
日期:2013-11-20 23:41:20双子座
日期:2014-06-11 17:20:43戌狗
日期:2014-06-16 11:05:00处女座
日期:2014-07-22 17:30:47狮子座
日期:2014-07-28 15:38:17金牛座
日期:2014-08-05 16:34:01亥猪
日期:2014-08-18 13:34:25白羊座
日期:2014-09-02 15:03:55金牛座
日期:2014-11-10 10:23:58处女座
日期:2014-12-02 09:17:52程序设计版块每日发帖之星
日期:2015-06-16 22:20:002015亚冠之塔什干火车头
日期:2015-06-20 23:28:22
72 [报告]
发表于 2014-07-04 10:48 |只看该作者
回复 71# blackold

向黑哥学习!
   

论坛徽章:
5
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:50:282015年亚洲杯之朝鲜
日期:2015-03-13 22:47:33IT运维版块每日发帖之星
日期:2016-01-09 06:20:00IT运维版块每周发帖之星
日期:2016-03-07 16:27:44
71 [报告]
发表于 2014-07-04 10:41 |只看该作者
本帖最后由 blackold 于 2014-07-04 10:53 编辑

@lifayi2008
@ly5066113@yestreenstars@yinyuemi

从源码看,确实如 Tim 所言,n 有打印功能。

我的理解是错误的。

主要代码(http://ftp.gnu.org/gnu/sed/sed-4.2.tar.bz2)如下:

sed.c

  1. /* If set, don't write out the line unless explicitly told to */
  2. bool no_default_output = false;

  3. ...

  4. main (...)
  5. {
  6.     ...
  7.     # 命令行参数处理
  8.             switch (opt)
  9.                 {
  10.                 case 'n':
  11.                     no_default_output = true;
  12.                     break;
  13.     ...

  14.     check_final_program(the_program);

  15.     return_code = process_files(the_program, argv+optind);

  16. }
复制代码
execute.c

  1. process_files (...)
  2. {
  3. ...
  4.         status = EXIT_SUCCESS;
  5.         while (read_pattern_space(&input, the_program, false))
  6.             {
  7.                 status = execute_program(the_program, &input);
  8.                 if (status == -1)
  9.                     status = EXIT_SUCCESS;
  10.                 else
  11.                     break;
  12.             }
  13.         closedown(&input);
  14.         ...
  15. }

  16. execute_program(...)
  17. {
  18. ...
  19.         switch(...)
  20.         # 命令处理

  21.                             case 'n':
  22.                                 if (!no_default_output)
  23.                                     output_line(line.active, line.length, line.chomped, &output_file);
  24.                                 if (test_eof(input) || !read_pattern_space(input, vec, false))
  25.                                     return -1;
  26.                                 break;
  27. ...
  28.                             case 'q':
  29.                                 if (!no_default_output)
  30.                                     output_line(line.active, line.length, line.chomped, &output_file);
  31. ...
  32.                             case 'N':
  33.                                 str_append(&line, "\n", 1);

  34.                                 if (test_eof(input) || !read_pattern_space(input, vec, true))
  35.                                     {
  36.                                         line.length--;
  37.                                         if (posixicity == POSIXLY_EXTENDED && !no_default_output)
  38.                                             output_line(line.active, line.length, line.chomped,
  39.                                                         &output_file);
  40.                                         return -1;
  41.                                     }
  42.                                 break;
  43. ...



  44.         if (!no_default_output)
  45.             output_line(line.active, line.length, line.chomped, &output_file);
  46.         return -1;
  47. }
复制代码

论坛徽章:
16
IT运维版块每日发帖之星
日期:2015-08-24 06:20:00综合交流区版块每日发帖之星
日期:2015-10-14 06:20:00IT运维版块每日发帖之星
日期:2015-10-25 06:20:00IT运维版块每日发帖之星
日期:2015-11-06 06:20:00IT运维版块每日发帖之星
日期:2015-12-10 06:20:00平安夜徽章
日期:2015-12-26 00:06:302016猴年福章徽章
日期:2016-02-18 15:30:34IT运维版块每日发帖之星
日期:2016-04-15 06:20:00IT运维版块每日发帖之星
日期:2016-05-21 06:20:00综合交流区版块每日发帖之星
日期:2016-08-16 06:20:002015七夕节徽章
日期:2015-08-21 11:06:17IT运维版块每日发帖之星
日期:2015-08-14 06:20:00
70 [报告]
发表于 2014-07-04 10:03 |只看该作者

捞2分

论坛徽章:
60
20周年集字徽章-20	
日期:2020-10-28 14:04:3015-16赛季CBA联赛之北京
日期:2016-07-06 15:42:0715-16赛季CBA联赛之同曦
日期:2016-06-12 10:38:0915-16赛季CBA联赛之佛山
日期:2016-05-27 11:54:56黄金圣斗士
日期:2015-12-02 11:44:35白银圣斗士
日期:2015-11-25 14:32:43白银圣斗士
日期:2015-11-23 12:53:352015亚冠之布里斯班狮吼
日期:2015-10-21 16:55:482015亚冠之首尔
日期:2015-09-01 16:46:052015亚冠之德黑兰石油
日期:2015-08-31 11:39:192015亚冠之萨济拖拉机
日期:2015-08-28 21:06:5315-16赛季CBA联赛之广东
日期:2016-07-12 14:58:53
69 [报告]
发表于 2014-07-04 09:53 |只看该作者
不明真相的人围观路过.
  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP