通过awk 和 sed 将多余的列剔除
名词注释:
awk
-F 指定分隔符
OFS 指定输出分隔符
sed
sed "s/|/test/2" a.log 将第二个 | 线替换为 test
sed "s/[^ ]+[[:space:]]/test/2" a.log 依照空格为分割
注:需求将现有的 40 列,减少到 38 列;将第二列于第三列数据删除;
1.查看文件按照 "|" 切分可以分为多少列;
[root@nq awk]# awk -F "|" '{print NF}' a.log |head -1
40
2.源文件:
[root@baolin awk]# cat a.log
2017-12-13 02:38:35|null|b9920e600ed6882e8bb04a|117.77.19.10|10|b992x0xcx9x9xdxbxex6x0x||es|US|||02:00:00:00:00:00|6.2.1|1.6.06|Android|LG-M13|MXB48T||1||19|0|0|||||0|0|1|0|2|2|0|0|16|6||20171213023824|1
2017-12-13 02:38:35|null|b9920e600ed6882e8bb04a|117.77.19.10|10|b9x9x2x0xcx9x9xdxbxex6a||es|US|||02:00:00:00:00:00|6.2.1|1.6.06|Android|LG-M13|MXB48T||1||19|0|0||coins5|||0|0|1|0|2|2|0|0|4|6||2017121302833|1
2017-12-13 02:38:35|null|b9920e600ed6882e8bb04a|117.77.19.10|10|b992x0xcx9x9xdxbxex6x0x||es|US|||02:00:00:00:00:00|6.2.1|1.6.06|Android|LG-M13|MXB48T||1||19|0|0||coins5|1|U|0|0|1|0|2|2|0|0|6|6||271213023833|1
2017-12-13 02:38:41|null|44e90866234d109954082a|117.242.13.4|10|4x4xex9x0x8x6x6x2x3x6xa||en|US|||02:00:00:00:00:00|7.1|1.6.06|Android|LG-M57|NRD90U|11|5|0|10|0|10|||||1|1|0|0|2|0|2|0|14|||20171213023636|1
2017-12-13 02:38:41|null|44e86623614bddd1965502|117.242.13.4|10|4x4xex9x0x8x6x6x2x3x6xa||en|US|||02:00:00:00:00:00|7.1|1.6.06|Android|LG-M57|NRD90U|11|5|0|10|0|10|||||1|1|0|0|2|0|2|0|13|||20171213023638|1
2017-12-13 02:38:41|null|44986264bd19715654082a|117.242.13.4|10|44xex9x0x8x6x6x2x3xx6xx||en|US|||02:00:00:00:00:00|7.1|1.6.06|Android|LG-M57|NRD90U|11|5|0|10|0|10|||||1|1|0|0|2|0|2|0|9|||20171213023647|1
2017-12-13 02:38:41|null|44e06264d096a15654082a|117.242.13.4|10|44e963a614bddd97654082a||en|US|||02:00:00:00:00:00|7.1|1.6.06|Android|LG-M57|NRD90U||5||10|0|10|||||1|1|0|0|1|0|2|0|1|||20171213023834|1
2017-12-13 02:38:43|null|6f4054396032da1454eb27|198.17.155.26|10|67f0450d54396032154eb27||en|US|||02:00:00:00:00:00|7.0.1|1.6.06|Android|Z65|Z965V1.1.11|10|5|0|9|0|0|||||0|1|0|0|2|0|1|0|14|||20171213022943|1
2017-12-13 02:38:43|null|705dc7a57c372d1454eb27|198.17.155.26|10|6f40543960c372d1454eb27||en|US|||02:00:00:00:00:00|7.0.1|1.6.06|Android|Z65|Z965V1.1.11|10|5|0|9|0|0|||||0|1|0|0|2|0|1|0|13|||20171213022947|1
2017-12-13 02:38:43|null|67f4054396032d1454eb27|198.17.155.26|10|6f40543960c372d1454eb27||en|US|||02:00:00:00:00:00|7.0.1|1.6.06|Android|Z65|Z965V1.1.11|10|5|0|9|0|0|||||0|1|0|0|2|0|1|0|9|||20171213022952|1
3.依照"|"为分割,将第二列于第三列数据删除,包括 "|"
[root@nq awk]# awk -F '|' 'OFS="|"{$2="";$3="";print}' a.log |sed "s/|//2" |sed "s/|//2"
2017-12-13 02:38:35|117.77.19.10|10|b992x0xcx9x9xdxbxex6x0x||es|US|||02:00:00:00:00:00|6.2.1|1.6.06|Android|LG-M13|MXB48T||1||19|0|0|||||0|0|1|0|2|2|0|0|16|6||20171213023824|1
2017-12-13 02:38:35|117.77.19.10|10|b9x9x2x0xcx9x9xdxbxex6a||es|US|||02:00:00:00:00:00|6.2.1|1.6.06|Android|LG-M13|MXB48T||1||19|0|0||coins5|||0|0|1|0|2|2|0|0|4|6||2017121302833|1
2017-12-13 02:38:35|117.77.19.10|10|b992x0xcx9x9xdxbxex6x0x||es|US|||02:00:00:00:00:00|6.2.1|1.6.06|Android|LG-M13|MXB48T||1||19|0|0||coins5|1|U|0|0|1|0|2|2|0|0|6|6||271213023833|1
2017-12-13 02:38:41|117.242.13.4|10|4x4xex9x0x8x6x6x2x3x6xa||en|US|||02:00:00:00:00:00|7.1|1.6.06|Android|LG-M57|NRD90U|11|5|0|10|0|10|||||1|1|0|0|2|0|2|0|14|||20171213023636|1
2017-12-13 02:38:41|117.242.13.4|10|4x4xex9x0x8x6x6x2x3x6xa||en|US|||02:00:00:00:00:00|7.1|1.6.06|Android|LG-M57|NRD90U|11|5|0|10|0|10|||||1|1|0|0|2|0|2|0|13|||20171213023638|1
2017-12-13 02:38:41|117.242.13.4|10|44xex9x0x8x6x6x2x3xx6xx||en|US|||02:00:00:00:00:00|7.1|1.6.06|Android|LG-M57|NRD90U|11|5|0|10|0|10|||||1|1|0|0|2|0|2|0|9|||20171213023647|1
2017-12-13 02:38:41|117.242.13.4|10|44e963a614bddd97654082a||en|US|||02:00:00:00:00:00|7.1|1.6.06|Android|LG-M57|NRD90U||5||10|0|10|||||1|1|0|0|1|0|2|0|1|||20171213023834|1
2017-12-13 02:38:43|198.17.155.26|10|67f0450d54396032154eb27||en|US|||02:00:00:00:00:00|7.0.1|1.6.06|Android|Z65|Z965V1.1.11|10|5|0|9|0|0|||||0|1|0|0|2|0|1|0|14|||20171213022943|1
2017-12-13 02:38:43|198.17.155.26|10|6f40543960c372d1454eb27||en|US|||02:00:00:00:00:00|7.0.1|1.6.06|Android|Z65|Z965V1.1.11|10|5|0|9|0|0|||||0|1|0|0|2|0|1|0|13|||20171213022947|1
2017-12-13 02:38:43|198.17.155.26|10|6f40543960c372d1454eb27||en|US|||02:00:00:00:00:00|7.0.1|1.6.06|Android|Z65|Z965V1.1.11|10|5|0|9|0|0|||||0|1|0|0|2|0|1|0|9|||20171213022952|1