awk 如何输出到文件 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
lengjian
V2EX    Linux

awk 如何输出到文件

  •  
  •   lengjian May 19, 2022 2773 views
    This topic created in 1440 days ago, the information mentioned may be changed or developed.

    abc.log 中不断产生日志,想持续统计上一秒该日志文件记录下含有'发送'关键字的数量,已经用以下脚本实现了,

    tail -f /var/logs/abc.log | grep '发送' | awk 'BEGIN{FS="." } {if($1==tmp) {count[tmp]++;} else { print tmp, "\t"count[tmp]} tmp=$1;}' 

    以下是终端输出内容

    2022-05-19 23:49:55 8 2022-05-19 23:49:56 6 2022-05-19 23:49:57 8 2022-05-19 23:49:58 3 2022-05-19 23:49:59 6 2022-05-19 23:50:00 5 

    现在想把结果输出到文件中,请教 awk 内该怎么修改或者更好的办法达到目的

    Supplement 1    May 20, 2022

    abc.log 中持续日志如下格式

    2022-05-20 09:38:17.649 INFO 5407 --- [ntLoopGroup-6-5] c.y.s.d.netty.handler.DeviceMsgEncoder : 发送 XXXX 2022-05-20 09:38:17.650 INFO 5407 --- [ntLoopGroup-6-5] c.y.s.d.netty.handler.DeviceMsgEncoder : 处理 XX 
    17 replies    2022-05-20 14:58:34 +08:00
    DCCooper
        1
    DCCooper  
       May 20, 2022 via iPhone
    >
    herich
        2
    herich  
       May 20, 2022 via Android
    也可以了解一下 tee
    luckyrayyy
        3
    luckyrayyy  
       May 20, 2022
    >>
    sora2blue
        4
    sora2blue  
       May 20, 2022
    加个 | tee 同时输出到文件和终端
    thedrwu
        5
    thedrwu  
       May 20, 2022 via Android
    grep ‘发送’ 可以直接放到 awk 里 /发送 /{…}
    yEhwG10ZJa83067x
        6
    yEhwG10ZJa83067x  
       May 20, 2022
    > 1.txt
    nyfwan123
        7
    nyfwan123  
       May 20, 2022
    99 八十一难
    眼看到灵山脚下了
    你就差个>
    不应该啊。。。。
    huntagain2008
        8
    huntagain2008  
       May 20, 2022
    小白的看法,楼主的意思是在 awk 内部 输出到文件, 比如
    [jerry]$ awk 'BEGIN { print "Hello, World !!!" > "/tmp/message.txt" }'

    楼上的应该说的是在后面重定向输出到文件
    tail -f /var/logs/abc.log | grep '发送' | awk 'BEGIN{FS="." } {if($1==tmp) {count[tmp]++;} else { print tmp, "\t"count[tmp]} tmp=$1;}' >output
    或者同时输出到文件和终端
    tail -f /var/logs/abc.log | grep '发送' | awk 'BEGIN{FS="." } {if($1==tmp) {count[tmp]++;} else { print tmp, "\t"count[tmp]} tmp=$1;}' | tee output
    Davic1
        9
    Davic1  
       May 20, 2022
    @nyfwan123 哈哈哈哈哈哈, 只渡有缘人.
    lengjian
        10
    lengjian  
    OP
       May 20, 2022
    @DCCooper
    @luckyrayyy
    @sora2blue
    @justrand
    @nyfwan123
    @huntagain2008

    试了以下几种方式,输出文件内容总是 0 ,文件内没有任何内容,并且终端也不再有输出内容了

    ```bash
    # >
    tail -f //var/logs/abc.log | grep '发送' | awk 'BEGIN{FS="." } {if($1==tmp) {count[tmp]++;} else { print tmp, "\t"count[tmp] > output} tmp=$1;}'
    # >>
    tail -f /var/logs/abc.log | grep '发送' | awk 'BEGIN{FS="." } {if($1==tmp) {count[tmp]++;} else { print tmp, "\t"count[tmp] >> output} tmp=$1;}'


    tail -f /var/logs/abc.log | grep '发送' | awk 'BEGIN{FS="." } {if($1==tmp) {count[tmp]++;} else { print tmp, "\t"count[tmp]} tmp=$1;}' >output

    # tee
    tail -f /var/logs/abc.log | grep '发送' | awk 'BEGIN{FS="." } {if($1==tmp) {count[tmp]++;} else { print tmp, "\t"count[tmp]} tmp=$1;}' | tee output
    ```
    huntagain2008
        11
    huntagain2008  
       May 20, 2022
    #10 小白的看法,也许是 grep 的原因,改成
    awk '/发送 / {...}'
    lengjian
        12
    lengjian  
    OP
       May 20, 2022
    @huntagain2008
    改成如下
    tail -f /var/logs/abc.log | awk 'BEGIN{FS="." } {if($1==tmp && /发送 /) {count[tmp]++;} else { print tmp, "\t"count[tmp]} tmp=$1;}' | tee output

    然后终端与 output 文件都有输出了,但结果并不是想要的
    ```bash
    2022-05-20 10:01:53
    2022-05-20 10:01:53
    2022-05-20 10:01:53
    2022-05-20 10:01:53 1
    2022-05-20 10:01:53 1
    2022-05-20 10:01:53 1
    2022-05-20 10:01:53 1
    2022-05-20 10:01:53 2
    2022-05-20 10:01:54
    2022-05-20 10:01:54
    2022-05-20 10:01:54
    2022-05-20 10:01:54 1
    2022-05-20 10:01:54 1
    2022-05-20 10:01:54 1
    2022-05-20 10:01:54 1
    2022-05-20 10:01:54 2
    2022-05-20 10:01:54 2
    2022-05-20 10:01:54 2
    2022-05-20 10:01:54 2
    2022-05-20 10:01:54 3
    2022-05-20 10:01:54 3
    2022-05-20 10:01:54 3
    2022-05-20 10:01:54 3
    2022-05-20 10:01:54 4
    2022-05-20 10:01:54 4
    2022-05-20 10:01:54 4
    2022-05-20 10:01:54 4
    2022-05-20 10:01:54 4
    2022-05-20 10:01:54 5
    2022-05-20 10:01:54 5
    2022-05-20 10:01:54 5
    2022-05-20 10:01:54 5
    2022-05-20 10:01:54 6
    2022-05-20 10:01:54 6
    2022-05-20 10:01:54 6
    2022-05-20 10:01:54 6
    2022-05-20 10:01:54 7
    2022-05-20 10:01:55
    2022-05-20 10:01:55
    2022-05-20 10:01:55
    2022-05-20 10:01:55 1
    2022-05-20 10:01:55 1
    2022-05-20 10:01:55 1
    2022-05-20 10:01:55 1
    2022-05-20 10:01:55 2
    2022-05-20 10:01:55 2
    2022-05-20 10:01:55 2
    2022-05-20 10:01:55 2
    2022-05-20 10:01:55 3
    2022-05-20 10:01:55 3
    2022-05-20 10:01:55 3
    2022-05-20 10:01:55 3
    2022-05-20 10:01:55 4
    2022-05-20 10:01:55 4
    2022-05-20 10:01:55 4
    2022-05-20 10:01:55 4
    2022-05-20 10:01:55 5
    2022-05-20 10:01:56
    2022-05-20 10:01:56
    2022-05-20 10:01:56
    2022-05-20 10:01:56 1
    2022-05-20 10:01:56 1
    2022-05-20 10:01:56 1
    2022-05-20 10:01:56 1
    2022-05-20 10:01:56 2
    2022-05-20 10:01:56 2
    2022-05-20 10:01:56 2
    2022-05-20 10:01:56 2
    2022-05-20 10:01:56 3
    2022-05-20 10:01:56 3
    2022-05-20 10:01:56 3
    2022-05-20 10:01:56 3
    2022-05-20 10:01:56 4
    2022-05-20 10:01:56 4
    2022-05-20 10:01:56 4
    2022-05-20 10:01:56 4
    2022-05-20 10:01:56 5
    2022-05-20 10:01:56 5
    2022-05-20 10:01:56 5
    2022-05-20 10:01:56 5
    2022-05-20 10:01:56 6
    2022-05-20 10:01:56 6
    2022-05-20 10:01:56 6
    2022-05-20 10:01:56 6
    2022-05-20 10:01:56 7
    ```

    想要的是统计上一秒的日志输出中含有指定关键字的条目数
    jaredyam
        13
    jaredyam  
       May 20, 2022
    @lengjian #12

    试试?(你这个命令在我的 macOS 上无法跑通,需要使用 stdbuf --output=L 强制 stdout 为 line buffered ,以上 grep 无法跑通同理)
    > tail -f /var/logs/abc.log | awk 'BEGIN{FS="." } {if($1==tmp && /发送 /) {count[tmp]++;} else { print tmp, "\t"count[tmp]} tmp=$1;}' | tee output

    tail -f /var/logs/abc.log | awk -F. '/发送 / { if($1==tmp) { count[tmp]++; } else { print tmp"\t"count[tmp] }; tmp=$1; }' | tee output
    lengjian
        14
    lengjian  
    OP
       May 20, 2022
    @jaredyam CentOS7 上跑的,macOS 上没有试

    tail -f /var/logs/abc.log | awk -F. '/发送 / { if($1==tmp) { count[tmp]++; } else { print tmp"\t"count[tmp] }; tmp=$1; }' | tee output
    这条命令运行同 10#,也是终端、文件都没输出
    jaredyam
        15
    jaredyam  
       May 20, 2022
    @lengjian #14

    按照我在 #13 说的,在 awk 前加 stdbuf --output=L 试试?
    lindas
        16
    lindas  
       May 20, 2022
    应该是缓冲的问题,试一下 tail -f access.log |grep --line-buffered '128' | awk '{print $1};system("")' > output.file
    grep 加 --line-buffered
    awk 加 system("")
    lengjian
        17
    lengjian  
    OP
       May 20, 2022
    @jaredyam
    @lindas
    感谢!两种方法都 ok

    ```bash
    tail -f /var/logs/abc.log | stdbuf --output=L awk -F. '/发送 / {if($1==tmp) {count[tmp]++;} else { print tmp, "\t"count[tmp]} tmp=$1;}' | tee output

    tail -f /var/logs/abc.log | grep '发送' | stdbuf --output=L awk -F. '{if($1==tmp) {count[tmp]++;} else { print tmp, "\t"count[tmp]} tmp=$1;}' | tee output

    tail -f /var/logs/abc.log | grep --line-buffered '发送' | awk 'BEGIN{FS="." } {if($1==tmp) {count[tmp]++;} else { print tmp, "\t"count[tmp]} tmp=$1;} system("")' | tee output
    ```
    About     Help     Advertise     Blog     API     FAQ     Solana     3248 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 48ms UTC 13:20 PVG 21:20 LAX 06:20 JFK 09:20
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86