linux awk--根据A列去重后按照B列统计

本文介绍了一种通过awk命令处理日志文件的方法,用于统计特定格式的日志中各项指标出现的频次,并提供了示例代码帮助理解如何操作。
  • 日志统计源格式
Ingest null xy.pull.yximgs.com gifshow gL4C7H60iB4 null 15.06 30014 2125237
Ingest null xy01.pull.yximgs.com gifshow BsXEEB9bRvs null 25.12 30014 4252273
Ingest null pl7.live.panda.tv live_panda 71f647c4111cbba3535af4f4be151bf5 null 24.76 30014 6781568
Ingest null xy01.pull.yximgs.com gifshow Af5c51X-Ck4 null 30.99 30014 5176675
Ingest null hdl61.kascend.com chushou_live 5ee009dbc328473dac83a876181de84f null 23.99 30014 7734469
Ingest null hdl61.kascend.com chushou_live a2335f8bc29346beaaf72b9c484c6e3f_500 null 19.99 30014 2331870
Ingest null xy0-flv.live.huajiao.com live_huajiao_v2 _LC_XY0_non_6719303814968062921570386_SX null 14.93 30014 4622499
Ingest null xy.pull.yximgs.com gifshow mrp-BUuDYEY null 15.06 30014 2268941
Play 119.124.90.93 xy.pull.yximgs.com gifshow vEKKsXtl6t8 183.57.36.130 14.78 12718 848808
Play 183.9.62.204 xy.pull.yximgs.com gifshow XeyZRLLQx58 183.57.36.130 14.96 26878 1710173
Play 14.20.129.136 hdl61.kascend.com chushou_live 5ee009dbc328473dac83a876181de84f 183.57.36.130 23.99 30016 7744293
Ingest null pl7.live.panda.tv live_panda 95044a4c38a5095cbbd62330aba73edb null 24.92 30012 6460547
Ingest null xl.live-play.acgvideo.com live-xl live_10372345_5240864 null 15.03 30012 2188106
Ingest null hdl61.kascend.com chushou_live 28d34e8c781740809bf6b08c0f453791 null 24.08 20768 2946198
Ingest null xy.pull.yximgs.com gifshow 8AxZti4_g-0 null 15.03 23752 1611642
Play 120.84.9.142 get.xycdn.kuwo.cn voicelive 181232893 183.57.36.130 4.48 23207 588222
Ingest null xy0-flv.live.huajiao.com live_huajiao_v2 _LC_XY0_non_2247003114968179501127237_SX null 9.78 30152 5128732
Ingest null get.xycdn.kuwo.cn voicelive 143003104 null 14.99 30016 1828485
Play 119.124.167.151 xy.pull.yximgs.com gifshow 4YJ5s6QiY6c 183.57.36.130 16.15 19075 1453976
Play 219.134.216.144 xy.pull.yximgs.com gifshow GIjOLc3AyIA 183.57.36.130 12.39 30017 2070905
Play 221.4.53.132 xy0-flv.live.huajiao.com live_huajiao_v2 _LC_XY0_non_11426331514968197561722639_OX 183.57.36.130 19.96 30017 2913820
Play 113.78.163.89 hdl61.kascend.com chushou_live c14273b7586144c6ba8bd09d48a3f637 183.57.36.130 24.11 40855 11563397
Play 124.225.62.60 pl7.live.panda.tv live_panda 76f18bde738262df150f85d4209fe4d1 183.57.36.130 29.92 30016 1621267
Play 223.198.122.72 pl8.live.panda.tv live_panda 043bb44c767506f79ce04b8876121ad2 183.57.36.130 29.98 30016 7271052
Play 180.218.27.14 xy0-flv.live.huajiao.com live_huajiao_v2 _LC_XY0_non_2035149914968188441266158_SX 183.57.36.130 18.89 30016 3998343
Play 183.31.235.209 pl7.live.panda.tv live_panda fe69a6688e80f9c309518efa8f702177_mid 183.57.36.130 19.99 30015 4113324
Play 116.22.55.249 pl7.live.panda.tv live_panda 76f18bde738262df150f85d4209fe4d1 183.57.36.130 29.92 30015 1610893
Play 183.3.187.155 xl.live-play.acgvideo.com live-xl live_6639802_2352392 183.57.36.130 29.85 30015 6040526
Play 113.65.20.229 xy01.pull.yximgs.com gifshow BsXEEB9bRvs 183.57.36.130 24.69 30016 4140644
Play 113.70.167.175 xy.pull.yximgs.com gifshow 4jAdN5vQrfg 183.57.36.130 14.93 30016 2113514
Play 113.80.107.174 xy.pull.yximgs.com gifshow iB5LSMc06lc 183.57.36.130 14.19 30016 1950748
Play 14.220.122.155 xy.pull.yximgs.com gifshow Yz_0bDnDE_A 183.57.36.130 15.03 30016 2033218
Play 58.62.183.33 xy01.pull.yximgs.com gifshow iMzAzFIX3RY 183.57.36.130 24.92 30018 3397743
Play 125.91.209.70 xl.live-play.acgvideo.com live-xl live_6639802_2352392 183.57.36.130 30.12 30018 6070742
Ingest null hdl-xycdn.zaizhibo.tv broadcast 11-1403575984129-1496820109012-328080386 null 23.75 30016 1401046
Ingest null pl7.live.panda.tv live_panda de2961e1e6f4207ec8f7c7a19bdf1807 null 60.17 30016 25233180
Play 119.120.168.209 xy.pr.live.fanxing.com live fx_hifi_989254 183.57.36.130 15.20 32098 5691670
Play 183.5.242.93 get.xycdn.kuwo.cn voicelive 265433231 183.57.36.130 15.80 40696 2647456
Play 113.91.35.162 get.xycdn.kuwo.cn voicelive 356897384 183.57.36.130 15.12 30018 3519213
Play 14.159.205.246 rtmp-xy.1234tv.com live 202304 183.57.36.130 14.99 30018 2104592
Play 14.159.207.203 rtmp-xy.1234tv.com live 202304 183.57.36.130 14.99 30018 2104592
Play 58.60.32.236 get.xycdn.kuwo.cn voicelive 310928779 183.57.36.130 14.99 30018 3282643
Play 183.38.63.8 xy.pr.live.fanxing.com live fx_hifi_110183147 183.57.36.130 15.46 30018 4754930
Play 183.56.76.216 xy.pr.live.fanxing.com live fx_hifi_342014692 183.57.36.130 16.59 35090 6005991
Play 125.91.146.247 xy.pr.live.fanxing.com live fx_hifi_23241362 183.57.36.130 17.52 36136 5878886
Play 119.29.216.22 xy.pr.live.fanxing.com live fx_hifi_33144330 183.57.36.130 14.26 30012 4453837
Play 27.38.142.68 get.xycdn.kuwo.cn voicelive 94888275 183.57.36.130 14.99 30012 1841845
Play 125.94.168.146 xy.pr.live.fanxing.com live fx_hifi_244430965 183.57.36.130 15.26 30012 4775051
Play 14.211.38.39 xy.pull.yximgs.com gifshow XOpjA_pJuTo 183.57.36.130 14.59 19050 1236052
Ingest null get.xycdn.kuwo.cn voicelive 71164097 null 20.68 30851 2599993
Ingest null hdl61.kascend.com chushou_live ddb0b31d310447b99db1fc2c5b55e98d null 23.76 16749 2262949
Play 183.8.245.30 hdl61.kascend.com chushou_live c5d371b86560428597086b1226f80924 183.57.36.130 24.15 17601 3180722
Ingest null xy.pull.yximgs.com gifshow sudeC1l0Sbo null 12.83 12625 696740
Ingest null xy0-flv.live.huajiao.com live_huajiao_v2 _LC_XY0_non_11426331514968197561722639_OX null 20.02 30014 2892840
Ingest null xy.pull.yximgs.com gifshow DLFtgQBvR8s null 14.99 30014 2181646
Play 183.13.201.87 xy0-flv.live.huajiao.com live_huajiao_v2 _LC_XY0_non_5631867814968201171661343_OX 183.57.36.130 16.59 31773 5548671
Play 219.136.198.74 pl8.live.panda.tv live_panda c9febad12e6c3cd81803bd23ee9ad32c 183.57.36.130 30.08 30015 6106360
Ingest null get.xycdn.kuwo.cn voicelive 364020074 null 14.99 30014 2795944
Ingest null xy.pr.live.fanxing.com live fx_hifi_188555749 null 15.36 30014 4790986
Ingest null xy.pr.live.fanxing.com live fx_hifi_51910260 null 15.13 30014 4643364
Ingest null xy.pr.live.fanxing.com live fx_hifi_70439820 null 14.56 30014 4484272
Ingest null xy.pr.live.fanxing.com live fx_hifi_365997602 null 12.93 30014 3989497
Ingest null xy.pr.live.fanxing.com live fx_hifi_375424828 null 15.43 30016 4742712
Ingest null xy.pr.live.fanxing.com live fx_hifi_121955046 null 15.23 30016 4657126
Play 119.130.228.183 pl8.live.panda.tv live_panda 8dd539552902c2ec70382e90bf67057d 183.57.36.130 29.99 30015 5849047
Play 183.39.234.40 xl.live-play.acgvideo.com live-xl live_6639802_2352392 183.57.36.130 30.05 30015 6084780
Play 171.107.52.37 pl7.live.panda.tv live_panda de2961e1e6f4207ec8f7c7a19bdf1807_mid 183.57.36.130 20.06 30015 5375624
Play 116.5.44.67 pl8.live.panda.tv live_panda 8db3b56f66234f09adb2c970f52f0af3 183.57.36.130 25.09 30015 6553960
Play 119.123.228.91 xy01.pull.yximgs.com gifshow 0tQunl6g20Q 183.57.36.130 24.78 13114 119025
Play 112.66.38.240 pl8.live.panda.tv live_panda 8db3b56f66234f09adb2c970f52f0af3_mid 183.57.36.130 19.92 30019 4336090
Play 14.126.123.212 pl8.live.panda.tv live_panda 043bb44c767506f79ce04b8876121ad2 183.57.36.130 29.98 30019 7184628
Play 222.216.227.135 pl8.live.panda.tv live_panda 043bb44c767506f79ce04b8876121ad2_mid 183.57.36.130 19.99 30019 4073912
Play 61.145.228.58 xl.live-play.acgvideo.com live-xl live_6639802_2352392 183.57.36.130 30.05 30019 6061246
Play 58.59.203.164 pl8.live.panda.tv live_panda 8db3b56f66234f09adb2c970f52f0af3 183.57.36.130 24.98 36351 10031046
Play 116.7.65.9 hdl61.kascend.com chushou_live a2335f8bc29346beaaf72b9c484c6e3f 183.57.36.130 23.72 30019 8258224
Play 183.25.194.197 hdl61.kascend.com chushou_live a2335f8bc29346beaaf72b9c484c6e3f 183.57.36.130 23.72 30019 8258224
Ingest null xy.pull.yximgs.com gifshow aU1WdiqTLA0 null 14.99 30014 2011369
Ingest null xy.pull.yximgs.com gifshow rV7Cvi6uIKw null 15.03 30014 2185128
Play 183.6.27.25 pl7.live.panda.tv live_panda 7d9bdfd8beca4be796bc4b757503decd_mid 183.57.36.130 19.99 30018 4302418
Play 121.8.239.194 pl8.live.panda.tv live_panda 93d8751b0ea555f2f045ab1f554266a9 183.57.36.130 24.99 30018 8032893
Play 119.126.206.250 pl7.live.panda.tv live_panda 1658ee9eca24c19208aff4f627d581f3 183.57.36.130 20.19 30018 4198084
Play 59.41.156.116 hdl61.kascend.com chushou_live a2335f8bc29346beaaf72b9c484c6e3f 183.57.36.130 23.75 30018 8268678
Play 14.153.236.25 xy.pull.yximgs.com gifshow 6wx_iqrZFEA 183.57.36.130 16.17 37285 2887937
Ingest null pl7.live.panda.tv live_panda 321558d5085c720e0550ccdf10b06b0a null 17.83 18560 3996806
Play 14.153.149.73 xy.pr.live.fanxing.com live fx_hifi_365117734 183.57.36.130 15.52 12567 1990202
Ingest null get.xycdn.kuwo.cn voicelive 310928779 null 14.99 30016 3300641
Play 14.18.236.187 xy.pf.live.fanxing.com live fx_hifi_109939548 183.57.36.130 14.86 30018 4563947
Play 183.32.164.132 hdl61.kascend.com chushou_live a2335f8bc29346beaaf72b9c484c6e3f 183.57.36.130 23.75 30016 8173126
Play 113.87.220.98 hdl61.kascend.com chushou_live a2335f8bc29346beaaf72b9c484c6e3f 183.57.36.130 23.75 30018 8169413
Play 119.137.204.141 xy.pull.yximgs.com gifshow iB5LSMc06lc 183.57.36.130 14.29 30013 1926590
Play 14.153.14.103 pl7.live.panda.tv live_panda 9efab81ece0d102aef49dd2cd5140562 183.57.36.130 25.12 30013 6561119
Play 183.38.248.13 pl7.live.panda.tv live_panda 7d9bdfd8beca4be796bc4b757503decd 183.57.36.130 29.98 30017 8060412
Ingest null xy.pull.yximgs.com gifshow Yz_0bDnDE_A null 14.96 30015 2030986
Ingest null hdl61.kascend.com chushou_live a2335f8bc29346beaaf72b9c484c6e3f null 23.75 30015 8200290
Play 183.36.69.17 xy.pr.live.fanxing.com live fx_hifi_989254 183.57.36.130 15.09 30227 5354041
Play 14.114.95.251 xy.pr.live.fanxing.com live fx_hifi_92254313 183.57.36.130 14.75 39873 5943764
Play 14.16.8.90 xy.pr.live.fanxing.com live fx_hifi_366867429 183.57.36.130 15.02 30018 4646029
Play 220.173.207.114 get.xycdn.kuwo.cn voicelive 181232893 183.57.36.130 14.99 30018 2038412
Play 61.141.165.64 rtmp-xy.1234tv.com live 203010 183.57.36.130 14.99 30018 2701260
Play 113.72.9.144 xy.pr.live.fanxing.com live fx_hifi_365997602 183.57.36.130 12.96 30018 4035485
Play 116.16.97.125 xy.pr.live.fanxing.com live fx_hifi_56186331 183.57.36.130 15.29 30012 4725117
Play 183.39.91.196 xy.pr.live.fanxing.com live fx_hifi_332206706 183.57.36.130 15.49 30012 4780414
Play 119.120.180.135 xy.pr.live.fanxing.com live fx_hifi_231639064 183.57.36.130 15.66 30012 4878931
Play 119.120.36.117 xy.pr.live.fanxing.com live fx_hifi_302900128 183.57.36.130 15.16 30012 4681747
  • awk命令实现
awk '!stream[$5]++{S[$1]++}END{for(key in S) print key,S[key]}' filename
awk '{if(!stream[$5]++)S[$1]++}END{for(key in S) print key,S[key]}' filename

按照第五列去重后按照第一列统计
如果多列去重如3、4、5列可按如下调整

awk '!stream[$3" "$4" "$5]++{S[$1]++}END{for(key in S) print key,S[key]}' filename

转载于:https://www.cnblogs.com/dforward2017/p/6957638.html

<think>我们正在讨论Linux管道符'|'的使用。用户想了解管道符后面可以跟随哪些命令或操作。 根据引用[1],管道符'|'的作用是将左边命令的输出作为右边命令的输入。因此,管道符后面可以跟随任何能够从标准输入读取数据的命令或操作。 引用[3]和引用[5]提供了一些具体例子,比如使用xargs命令(可以处理标准输入并将其作为参数传递给其他命令)以及一些常见的文本处理命令(如grep, less, more等)。 此外,引用[4]提到了环境变量PATH的要性,因为管道符后面的命令需要是系统可执行的,即该命令必须在PATH环境变量所包含的目录中,或者使用绝对路径指定。 因此,我们可以总结:管道符后面可以跟任何可以从标准输入读取数据的命令,包括但不限于: 1. 文本处理命令:grep, sed, awk, sort, uniq, cut, tr, wc, head, tail, less, more等。 2. 可以将输入作为参数的命令:xargs(它可以将输入转换成后面命令的参数) 3. 其他任意可以从标准输入读取并处理的命令,甚至可以是用户自己编写的脚本或程序。 同时,管道符可以连续使用,即个命令通过管道连接,形成一个处理链。 注意:有些命令默认不从标准输入读取数据,但可以通过特殊符号(如xargs)或者命令参数(如grep可以直接从标准输入读取)来使用。 下面我们给出一些例子: 例1:使用grep过滤ls的输出 ls -l | grep "txt" 例2:使用个管道符,先排序再 cat file.txt | sort | uniq 例3:使用xargs将前面的输出作为参数传递给后面的命令 echo "file1 file2 file3" | xargs touch 例4:使用awk处理文本 ps aux | awk '{print $2}' 例5:使用less分页查看 dmesg | less 另外,需要注意的是,管道符后面的命令必须能够处理文本流(因为管道传递的是文本流)。如果某个命令需要文件参数而不是标准输入,那么我们可以使用xargs来转换,或者使用命令替换。 最后,引用[2]提醒我们,虽然管道符很方便,但也要注意性能问题,特别是在处理大数据时,过的管道可能会降低效率。 根据以上分析,我们可以回答用户的问题。</think>### Linux管道符`|`后面可跟随的内容 在Linux中,管道符`|`的作用是将左侧命令的**标准输出(stdout)**作为右侧命令的**标准输入(stdin)**。因此,管道符右侧可跟随**任何能从标准输入读取数据的命令或操作**,主要包括以下类型: --- ### 一、常见可跟随命令类型 1. **文本处理工具** - `grep`(过滤内容): ```bash ps aux | grep "nginx" # 筛选包含"nginx"的进程 ``` - `sed`(流编辑器): ```bash echo "hello" | sed 's/hello/world/' # 替换文本 ``` - `awk`(文本分析): ```bash df -h | awk '{print $1, $5}' # 提取磁盘使用率 ``` - `sort`/`uniq`/`wc`(排序、、计数): ```bash cat log.txt | sort | uniq -c | wc -l # 统计唯一行数 ``` 2. **分页查看器** - `less`(交互式分页浏览): ```bash dmesg | less # 分页查看内核日志 ``` - `more`(基础分页显示): ```bash cat large_file.txt | more # 逐页显示大文件 ``` 3. **数据转换工具** - `xargs`(将输入转为参数): ```bash find /tmp -name "*.log" | xargs rm # 删除所有.log文件 ``` 支持自定义占位符(如`-I %`): ```bash echo "file1 file2" | xargs -I % sh -c 'touch %; ls %' # 创建文件并出[^3] ``` - `tr`(字符替换/删除): ```bash echo "HELLO" | tr 'A-Z' 'a-z' # 转为小写 → hello ``` 4. **压缩/解压工具** - `gzip`/`gunzip`: ```bash cat data.log | gzip > compressed.gz # 压缩输出 ``` - `zcat`(直接读取压缩文件): ```bash zcat file.gz | grep "error" # 在压缩文件中搜索 ``` 5. **自定义脚本与程序** 任何可执行文件(如Python/Bash脚本),只要支持从`stdin`读取数据: ```bash echo "input_data" | ./my_script.sh # 调用自定义脚本处理 ``` --- ### 二、管道使用注意事项 1. **命令兼容性** - 右侧命令**必须能接受标准输入**。例如`ls | echo`无效,因为`echo`默认不读取`stdin`(需改为`ls | xargs echo`)。 - 若命令需参数(如`rm`),需配合`xargs`(见上文示例)。 2. **性能优化** 避免过度使用管道链(如`cmd1 | cmd2 | cmd3 | cmd4`),每个管道都会创建新进程,可能降低效率[^2]。 **优化方案**:合并命令功能(如用`awk`替代`grep+cut`)。 3. **错误流处理** 管道默认仅传递标准输出(`stdout`),错误信息(`stderr`)会直接显示到终端。若需定向错误流: ```bash cmd 2>&1 | grep "error" # 将stderr合并到stdout再过滤 ``` 4. **环境变量依赖** 管道右侧的命令必须在`PATH`环境变量定义的路径中,否则需指定绝对路径: ```bash ls | /usr/local/bin/my_tool # 使用绝对路径 ``` --- ### 三、典型应用场景 ```mermaid graph LR A[生成数据] -->|ps aux| B[过滤进程] -->|grep nginx| C[提取PID] -->|awk '{print $2}'| D[操作结果] -->|xargs kill| E[结束进程] ``` 完整命令示例: ```bash ps aux | grep "nginx" | awk '{print $2}' | xargs kill -9 ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值