sparkssssssss
V2EX  ›  问与答

请教一个文本处理的方法!

  •  
  •   sparkssssssss · Apr 13, 2016 · 2491 views
    This topic created in 3771 days ago, the information mentioned may be changed or developed.

    有些文本内容如下: a1 b1 a1 c1 a1 d1 ... a2 b2 a2 c2 a2 d2 ......

    每天大约有几百万条这种格式的日志,目前想处理下,把以下两条第二列的内容合并, a1 b1 a1 c1

    做成以下格式的 a1 b1 c1 a1 d1 ...... a2 b2 c2 a2 d2 ......

    有什么可行的办法呢.多谢大家

    Supplement 1  ·  Apr 13, 2016
    真不好意思,日志的格式换行没有打出来日志的格式如下:
    20160412xxxx id1 内容 a \n
    20160412xxxx id1 内容 b \n
    20160412xxxx id1 内容 c \n
    20160412xxxx id1 内容 d \n
    ........
    20160412xxxx id2 内容 a1 \n
    20160412xxxx id2 内容 b1 \n
    20160412xxxx id2 内容 c1 \n
    20160412xxxx id2 内容 d1 \n
    ........
    20160412xxxx id3 内容 a2 \n
    20160412xxxx id3 内容 b2 \n
    20160412xxxx id3 内容 c2 \n
    20160412xxxx id3 内容 d2 \n
    ........

    目前想实现:
    20160412xxxx id1 内容 a + 内容 b \n
    20160412xxxx id1 内容 c \n
    20160412xxxx id1 内容 d \n
    ........
    20160412xxxx id2 内容 a1 +内容 b1 \n
    20160412xxxx id2 内容 c1 \n
    20160412xxxx id2 内容 d1 \n
    ........
    20160412xxxx id3 内容 a2 +内容 b2 \n
    20160412xxxx id3 内容 c2 \n
    20160412xxxx id3 内容 d2 \n
    ........


    其中内容 ax bx 都是非常固定的格式.
    6 replies    2016-04-13 11:30:51 +08:00
    sparkssssssss
        1
    sparkssssssss  
    OP
       Apr 13, 2016
    换行怎么没打出来......
    Devin
        2
    Devin  
       Apr 13, 2016 via iPhone
    用 awk 应该可以
    楼主 append 把问题描述清楚吧
    sparkssssssss
        3
    sparkssssssss  
    OP
       Apr 13, 2016
    @Devin awk 我一般处理单行居多,如何合并特定的两行,我不太会
    我目前是用 rsyslog 来做日志格式处理的,但是也是基于单行
    clino
        4
    clino  
       Apr 13, 2016
    写几行 python 吧
    irenicus
        5
    irenicus  
       Apr 13, 2016 via Android
    python 或者 perl
    wvuu
        6
    wvuu  
       Apr 13, 2016
    awk '{if($2 != a){b=$3" "$4;c=1}else{print(c != 0)?$1" "$2" "b" "$3" "$4:$0;c=0}a=$2}'

    这个命令只能按照你目前的格式处理,如果你的日志根这个格式不一样,结果会有问题。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3164 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 12:13 · PVG 20:13 · LAX 05:13 · JFK 08:13
    ♥ Do have faith in what you're doing.