免费注册 查看新帖 |

Chinaunix

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

问一个shell的写法 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-05-25 13:24 |只看该作者 |倒序浏览
输入是这样:

time:0,rate:0.000000,ip:112.4.2.48,id:35716461,SDK:5.0.1

顺序不一定是这样哦,time:0可能在任意的位置

想要的输出是:

time:0,id:3571646

求助一下。。

论坛徽章:
0
2 [报告]
发表于 2012-05-25 13:30 |只看该作者
  1. grep -oP '(time|id):\d+' a | sed 'N;s/\n/,/'
复制代码

论坛徽章:
0
3 [报告]
发表于 2012-05-25 13:42 |只看该作者
回复 2# __lxmxn__


    可以了  谢谢楼上

   再问下  sed是不是不支持非贪婪匹配阿  我用sed就是不行

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
4 [报告]
发表于 2012-05-25 13:43 |只看该作者
闸北陆小洪 发表于 2012-05-25 13:42
回复 2# __lxmxn__
确实不支持。不过可以用[^x]之类的过滤掉。

论坛徽章:
0
5 [报告]
发表于 2012-05-25 13:44 |只看该作者
回复 4# L_kernel


    ok  明白了  谢谢各位

论坛徽章:
13
15-16赛季CBA联赛之同曦
日期:2016-01-28 19:52:032015亚冠之北京国安
日期:2015-10-07 14:28:19NBA常规赛纪念章
日期:2015-05-04 22:32:03处女座
日期:2015-01-15 19:45:44卯兔
日期:2014-10-28 16:17:14白羊座
日期:2014-05-24 15:10:46寅虎
日期:2014-05-10 09:50:35白羊座
日期:2014-03-12 20:52:17午马
日期:2014-03-01 08:37:27射手座
日期:2014-02-19 19:26:54子鼠
日期:2013-11-30 09:03:56狮子座
日期:2013-09-08 08:37:52
6 [报告]
发表于 2012-05-25 13:57 |只看该作者
本帖最后由 ulovko 于 2012-05-25 14:16 编辑
__lxmxn__ 发表于 2012-05-25 13:30
  1. grep -oP '(time|id):\d+'| sed 'N;s/\n/,/'
复制代码
\t a tab character
\n a newline character
\r a carriage-r eturn character
\s matches any ™whitespaceš character (space, tab, newline, formfeed, and such)
\S anything not !\s"
\w ![a-zA-Z0-9R]" (useful as in !\w+", ostensibly to match a word)
\W anything not !\w", i.e., ![^a-zA-Z0-9R]"
\d ![0-9]", i.e., a digit
\D anything not !\d", i.e., ![^0-9]"

The "n" command will print out the current pattern space (unless the "-n" flag is used), empty the current pattern space, and read in the next line of input. The "N" command does not print out the current pattern space and does not empty the pattern space. It reads in the next line, but appends a new line character along with the input line itself to the pattern space.

The "d" command deleted the current pattern space, reads in the next line, puts the new line into the pattern space, and aborts the current command, and starts execution at the first sed command. This is called starting a new "cycle." The "D" command deletes the first portion of the pattern space, up to the new line character, leaving the rest of the pattern alone. Like "d," it stops the current command and starts the command cycle over again. However, it will not print the current pattern space. You must print it yourself, a step earlier. If the "D" command is executed with a group of other commands in a curly brace, commands after the "D" command are ignored. The next group of sed commands is executed, unless the pattern space is emptied. If this happens, the cycle is started from the top and a new line is read.

The "p" command prints the entire pattern space. The "P" command only prints the first part of the pattern space, up to the NEWLINE character. Neither the "p" nor the "P" command changes the patterns space.

论坛徽章:
15
2015年辞旧岁徽章
日期:2015-03-03 16:54:15双鱼座
日期:2015-01-15 17:29:44午马
日期:2015-01-06 17:06:51子鼠
日期:2014-11-24 10:11:13寅虎
日期:2014-08-18 07:10:55酉鸡
日期:2014-04-02 12:24:51双子座
日期:2014-04-02 12:19:44天秤座
日期:2014-03-17 11:43:36亥猪
日期:2014-03-13 08:13:51未羊
日期:2014-03-11 12:42:03白羊座
日期:2013-11-20 10:15:18CU大牛徽章
日期:2013-04-17 11:48:45
7 [报告]
发表于 2012-05-25 14:23 |只看该作者
by awk
  1. awk -F , '{for (i=1;i<=NF;i++) if ($i~/time:/||$i~/id:/) printf $i FS}' infile
复制代码

论坛徽章:
13
15-16赛季CBA联赛之同曦
日期:2016-01-28 19:52:032015亚冠之北京国安
日期:2015-10-07 14:28:19NBA常规赛纪念章
日期:2015-05-04 22:32:03处女座
日期:2015-01-15 19:45:44卯兔
日期:2014-10-28 16:17:14白羊座
日期:2014-05-24 15:10:46寅虎
日期:2014-05-10 09:50:35白羊座
日期:2014-03-12 20:52:17午马
日期:2014-03-01 08:37:27射手座
日期:2014-02-19 19:26:54子鼠
日期:2013-11-30 09:03:56狮子座
日期:2013-09-08 08:37:52
8 [报告]
发表于 2012-05-25 14:30 |只看该作者
rdcwayx 发表于 2012-05-25 14:23
by awk


这个好,我喜欢awk

论坛徽章:
0
9 [报告]
发表于 2012-05-25 14:35 |只看该作者
回复 7# rdcwayx


    thx  awk也不错
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP