2个文本文件,需要对相同的列,来合并对应列的值
文件1 cat /tmp/test-zone-test-private-access-hardcode.txt
wgdc-drn-03.cloud.zone.domain. 300 IN A 192.168.192.13apps.cf.wgdc-drn-03.cloud.zone.domain. 300 IN A 192.168.192.19704.cloud.zone.domain. 300 IN A 192.168.192.222
文件2
wgdc-drn-03.cloud.zone.domain. 300 IN A 10.171.192.156apps.cf.wgdc-drn-03.cloud.zone.domain. 300 IN A 10.171.192.15604.cloud.zone.domain. 300 IN A 11.192.159.23
将如下文本按第一列为主key,将相同的第五列值追加到同一行
awk '{a[$1]=a[$1]"|"$5}END{for (i in a) {print i,a[i]"|"}}' /tmp/dev-xu-dev-private-access-hardcode.txt /tmp/dev-xu-domain-file.txtawk '{a[$1]=a[$1]" "$5}END{for (i in a) {print i,a[i]}}' /tmp/dev-xu-dev-private-access-hardcode.txt /tmp/dev-xu-domain-file.txtawk '{S[$1]=$5","S[$1]}END{for (a in S) print a, S[a]}' /tmp/dev-xu-dev-private-access-hardcode.txt /tmp/dev-xu-domain-file.txtawk '{S[$1]=$5" "S[$1]}END{for (a in S) print a, S[a]}' /tmp/dev-xu-dev-private-access-hardcode.txt /tmp/dev-xu-domain-file.txtawk '{S[$1]=$5","S[$1]}END{for (a in S) print a, S[a]}' /tmp/dev-xu-dev-private-access-hardcode.txt /tmp/dev-xu-domain-file.txt|sed 's/,$//g'awk '{S[$1]=$5","S[$1]} END {for(a in S) print a, S[a]}' /tmp/dev-xu-dev-private-access-hardcode.txt /tmp/dev-xu-domain-file.txt|sed 's/,$//g'下面2个仅仅是分隔符号的不同awk 'NR==FNR{a[$1]=$5;next} a[$1]{print $0","a[$1]}' /tmp/dev-xu-dev-private-access-hardcode.txt /tmp/dev-xu-domain-file.txtawk 'NR==FNR{a[$1]=$5;next} a[$1]{print $0"|"a[$1]}' /tmp/dev-xu-dev-private-access-hardcode.txt /tmp/dev-xu-domain-file.txt对file1执行“NR==FNR{…}”第一个循环,建立哈希数组FNR The input record number in the current input file. #已读入当前文件的记录数NR The total number of input records seen so far. #已读入的总记录数 FNR ordinal number of the current record in the current file. FS regular expression used to separate fields; also settable by option -Ffs. NF number of fields in the current record. NR ordinal number of the current record.
公共列是1和2,比上面多一个条件,上面第一列为key,下面这个是第一和第二/将相同的第五列值追加到同一行
awk 'NR==FNR{a[$1,$2]=$5;next} a[$1,$2]{print $0"|"a[$1,$2]}' /tmp/dev-xu-dev-private-access-hardcode.txt /tmp/dev-xu-domain-file.tx
awk累加
1 默认的awk --help
Examples:
awk '{ sum += $1 }; END { print sum }' file awk -F: '{ print $1 }' /etc/passwd
累加第二列的数值
方法一(无条件累加)
cat /tmp/dev-xu-dev-private-access-hardcode.txt|awk '{a=a+$2}END{print a}'答案是900
其他几个例子:
0102;030405;0607;弄成01 02;03 04 05;06 07;
如果操作
awk '{if ($0~/;$/) printf("%s\n", $0);else printf("%s ",$0)}'
查看服务的链接态
# netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'ESTABLISHED 283FIN_WAIT1 1TIME_WAIT 12:~# ss -ant | awk 'NR>1 {++s[$1]} END {for(k in s) print k,s[k]}'LISTEN 18ESTAB 277FIN-WAIT-1 1TIME-WAIT 12
综上,走一步算一步 用到哪学到哪
还没有评论,来说两句吧...