免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 2889 | 回复: 12

[文本处理] 如何对不同列的文本进行分别处理 [复制链接]

论坛徽章:
0
发表于 2014-07-31 11:54 |显示全部楼层
对不起,本人的shell水平还是有点菜
如下:
[23/Jul/2014:18:14:29        /install?os=4.1.1&clickid
[23/Jul/2014:18:14:29        /install?os=4.1.1&clickid
[23/Jul/2014:18:14:29        /install?os=4.1.1&clickid
[23/Jul/2014:18:14:29        /install?os=4.1.1&clickid
[23/Jul/2014:18:14:29        /install?os=4.1.1&clickid
[23/Jul/2014:18:14:29        /install?os=4.1.1&clickid
我想分别截掉第一列的[符号,第二列的/install?
只保留23/Jul/2014:18:14:29  os=4.1.1&clickid
并将第一列处理成时间戳的形式
请问怎么做,谢谢!

论坛徽章:
0
发表于 2014-07-31 12:08 |显示全部楼层
  1. sed 's/\[//;s#/install?##' file
复制代码
时间戳是什么格式?求解。

论坛徽章:
1
处女座
日期:2014-09-18 16:18:00
发表于 2014-07-31 12:13 |显示全部楼层
  1. awk '{sub(/\[/,"",$1);sub(/\/install\?/,"",$2)}1' testfile
复制代码
同样想知道,时间戳是具体什么格式,纯数字字符串?

论坛徽章:
145
技术图书徽章
日期:2013-10-01 15:32:13戌狗
日期:2013-10-25 13:31:35金牛座
日期:2013-11-04 16:22:07子鼠
日期:2013-11-18 18:48:57白羊座
日期:2013-11-29 10:09:11狮子座
日期:2013-12-12 09:57:42白羊座
日期:2013-12-24 16:24:46辰龙
日期:2014-01-08 15:26:12技术图书徽章
日期:2014-01-17 13:24:40巳蛇
日期:2014-02-18 14:32:59未羊
日期:2014-02-20 14:12:13白羊座
日期:2014-02-26 12:06:59
发表于 2014-07-31 12:35 |显示全部楼层
回复 1# hao3721

would you like this ...

$ awk '{gsub(/[[/]/," ",$1);sub(/:/," ",$1);gsub(/\/install\?/,"",$2);cmd="date -d \""$1"\" +%s";cmd |& getline s;print s,$2}' FILE
1406110469 os=4.1.1&clickid
1406110469 os=4.1.1&clickid
1406110469 os=4.1.1&clickid
1406110469 os=4.1.1&clickid
1406110469 os=4.1.1&clickid
1406110469 os=4.1.1&clickid


   

论坛徽章:
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
发表于 2014-07-31 13:40 |显示全部楼层
回复 4# jason680


少了个close(cmd) ,否则后面的时间戳数据都不正常了! 另外,我想知道, cmd |& getline ,这里的 & 有什么作用?
  1. [root@localhost ~]#
  2. [root@localhost ~]# awk '{gsub(/[[/]/," ",$1);sub(/:/," ",$1);gsub(/\/install\?/,"",$2);cmd="date -d \""$1"\" +%s";cmd | getline s;print s,$2;close(cmd)}' /tmp/file
  3. 1406110469 os=4.1.1&clickid
  4. 1406110479 os=4.1.1&clickid
  5. 1406110489 os=4.1.1&clickid
  6. 1406110499 os=4.1.1&clickid
  7. 1406110529 os=4.1.1&clickid
  8. 1406110539 os=4.1.1&clickid
  9. 1406110469 os=4.1.1&clickid
  10. 1406110469 os=4.1.1&clickid
  11. [root@localhost ~]# awk '{gsub(/[[/]/," ",$1);sub(/:/," ",$1);gsub(/\/install\?/,"",$2);cmd="date -d \""$1"\" +%s";cmd | getline s;print s,$2;}' /tmp/file         
  12. 1406110469 os=4.1.1&clickid
  13. 1406110479 os=4.1.1&clickid
  14. 1406110489 os=4.1.1&clickid
  15. 1406110499 os=4.1.1&clickid
  16. 1406110529 os=4.1.1&clickid
  17. 1406110539 os=4.1.1&clickid
  18. 1406110539 os=4.1.1&clickid
  19. 1406110539 os=4.1.1&clickid
  20. [root@localhost ~]# cat /tmp/file
  21. [23/Jul/2014:18:14:29        /install?os=4.1.1&clickid
  22. [23/Jul/2014:18:14:39        /install?os=4.1.1&clickid
  23. [23/Jul/2014:18:14:49        /install?os=4.1.1&clickid
  24. [23/Jul/2014:18:14:59        /install?os=4.1.1&clickid
  25. [23/Jul/2014:18:15:29        /install?os=4.1.1&clickid
  26. [23/Jul/2014:18:15:39        /install?os=4.1.1&clickid
  27. [23/Jul/2014:18:14:29        /install?os=4.1.1&clickid
  28. [23/Jul/2014:18:14:29        /install?os=4.1.1&clickid
  29. [root@localhost ~]#



复制代码

论坛徽章:
16
IT运维版块每日发帖之星
日期:2015-10-02 06:20:00IT运维版块每月发帖之星
日期:2015-09-11 19:30:52IT运维版块每周发帖之星
日期:2015-09-11 19:20:31IT运维版块每日发帖之星
日期:2015-08-26 06:20:00每日论坛发贴之星
日期:2015-08-20 06:20:00IT运维版块每日发帖之星
日期:2015-08-20 06:20:002015年辞旧岁徽章
日期:2015-03-03 16:54:15金牛座
日期:2014-05-04 16:58:09双子座
日期:2013-12-17 16:44:37辰龙
日期:2013-11-22 15:20:59狮子座
日期:2013-11-18 22:55:08射手座
日期:2013-11-12 10:54:26
发表于 2014-07-31 13:48 |显示全部楼层
本帖最后由 wenhq 于 2014-07-31 13:49 编辑

[root@1 ~]# perl -p -lne 'print if s/\[// && s#/install\?##'  test
23/Jul/2014:18:14:29        os=4.1.1&clickid
23/Jul/2014:18:14:29        os=4.1.1&clickid
23/Jul/2014:18:14:29        os=4.1.1&clickid
23/Jul/2014:18:14:29        os=4.1.1&clickid
23/Jul/2014:18:14:29        os=4.1.1&clickid
23/Jul/2014:18:14:29        os=4.1.1&clickid
23/Jul/2014:18:14:29        os=4.1.1&clickid
23/Jul/2014:18:14:29        os=4.1.1&clickid
23/Jul/2014:18:14:29        os=4.1.1&clickid
23/Jul/2014:18:14:29        os=4.1.1&clickid
23/Jul/2014:18:14:29        os=4.1.1&clickid
23/Jul/2014:18:14:29        os=4.1.1&clickid

论坛徽章:
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
发表于 2014-07-31 13:48 |显示全部楼层
回复 4# jason680
  1.        command | getline [var]
  2.                              Run command piping the output either into $0 or var, as above.

  3.        command |& getline [var]
  4.                              Run command as a co-process piping the output either into $0 or var, as above.  Co-processes are a gawk extension.
复制代码
这两个有何本质区别呀? .

论坛徽章:
16
IT运维版块每日发帖之星
日期:2015-10-02 06:20:00IT运维版块每月发帖之星
日期:2015-09-11 19:30:52IT运维版块每周发帖之星
日期:2015-09-11 19:20:31IT运维版块每日发帖之星
日期:2015-08-26 06:20:00每日论坛发贴之星
日期:2015-08-20 06:20:00IT运维版块每日发帖之星
日期:2015-08-20 06:20:002015年辞旧岁徽章
日期:2015-03-03 16:54:15金牛座
日期:2014-05-04 16:58:09双子座
日期:2013-12-17 16:44:37辰龙
日期:2013-11-22 15:20:59狮子座
日期:2013-11-18 22:55:08射手座
日期:2013-11-12 10:54:26
发表于 2014-07-31 13:50 |显示全部楼层
回复 7# reyleon


     co-process ?

论坛徽章:
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
发表于 2014-07-31 13:53 |显示全部楼层
回复 8# wenhq


    字面上是协同进程的意思! 可惜不懂呀..   看来需要恶补呀.

论坛徽章:
145
技术图书徽章
日期:2013-10-01 15:32:13戌狗
日期:2013-10-25 13:31:35金牛座
日期:2013-11-04 16:22:07子鼠
日期:2013-11-18 18:48:57白羊座
日期:2013-11-29 10:09:11狮子座
日期:2013-12-12 09:57:42白羊座
日期:2013-12-24 16:24:46辰龙
日期:2014-01-08 15:26:12技术图书徽章
日期:2014-01-17 13:24:40巳蛇
日期:2014-02-18 14:32:59未羊
日期:2014-02-20 14:12:13白羊座
日期:2014-02-26 12:06:59
发表于 2014-07-31 13:58 |显示全部楼层
回复 5# reyleon

1. thank you for your information with close(cmd)

2. getline please refer manual ...
Note: I think using "|" can do it well in this case

http://www.gnu.org/software/gawk/manual/gawk.html#Getline_002fCoprocess

4.9.10 Summary of getline Variants

Table 4.1 summarizes the eight variants of getline, listing which built-in variables are set by each one, and whether the variant is standard or a gawk extension. Note: for each variant, gawk sets the RT built-in variable.
Variant        Effect        Standard / Extension
getline        Sets $0, NF, FNR, NR, and RT        Standard
getline var        Sets var, FNR, NR, and RT        Standard
getline < file        Sets $0, NF, and RT        Standard
getline var < file        Sets var and RT        Standard
command | getline        Sets $0, NF, and RT        Standard
command | getline var        Sets var and RT        Standard
command |& getline        Sets $0, NF, and RT        Extension
command |& getline var        Sets var and RT        Extension

...

4.9.7 Using getline from a Coprocess

Input into getline from a pipe is a one-way operation. The command that is started with ‘command | getline’ only sends data to your awk program.

On occasion, you might want to send data to another program for processing and then read the results back. gawk allows you to start a coprocess, with which two-way communications are possible. This is done with the ‘|&’ operator. Typically, you write data to the coprocess first and then read results back, as shown in the following:

print "some query" |& "db_server"
"db_server" |& getline


which sends a query to db_server and then reads the results.

The values of NR and FNR are not changed, because the main input stream is not used. However, the record is split into fields in the normal manner, thus changing the values of $0, of the other fields, and of NF and RT.

Coprocesses are an advanced feature. They are discussed here only because this is the section on getline. See Two-way I/O, where coprocesses are discussed in more detail.

...
4.9.8 Using getline into a Variable from a Coprocess

When you use ‘command |& getline var’, the output from the coprocess command is sent through a two-way pipe to getline and into the variable var.

In this version of getline, none of the built-in variables are changed and the record is not split into fields. The only variable changed is var. However, RT is set.     
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP