交换两个字段并保持标签正确

时间:2015-07-22 10:53:34

标签: awk field swap

我有这个

root@messagerie-secours[10.10.10.19] ~ # cat /tmp/first20lines
Jul  7 19:35:23 smtpd    3D95D1E923C8 client=localhost[127.0.0.1]
Jul  7 19:35:23 cleanup  3D95D1E923C8 message-id=1436290501c8e5fc05ec3357fc654e7bd6b0c72a18@
Jul  7 19:35:23 qmgr     3D95D1E923C8 from=<xxx@infomaniak.com> nrcpt=1
Jul  7 19:35:23 pipe     3D95D1E923C8 to=<xxx@algerian-radio.dz> status=sent (delivered via maildrop service)
Jul  7 19:35:23 qmgr     3D95D1E923C8 removed
Jul  5 03:10:02 pickup   3165F1E923C8 from=<www-data>
Jul  5 03:10:02 cleanup  3165F1E923C8 message-id=<xxx@messagerie.algerian-radio.dz>
Jul  5 03:10:02 qmgr     3165F1E923C8 from=<xxx@messagerie.algerian-radio.dz> nrcpt=1
Jul  5 03:10:05 smtp     3165F1E923C8 to=<xxx@messagerie.algerian-radio.dz> queued as 900031E923C7
Jul  5 03:10:05 qmgr     3165F1E923C8 removed
Jul  5 03:10:05 smtpd    900031E923C7 client=localhost[127.0.0.1]
Jul  5 03:10:05 cleanup  900031E923C7 message-id=<xxx@messagerie.algerian-radio.dz>
Jul  5 03:10:05 qmgr     900031E923C7 from=<xxx@messagerie.algerian-radio.dz> nrcpt=1
Jul  5 03:10:05 local    900031E923C7 to=<xxx@messagerie.algerian-radio.dz> status=sent (delivered to command: procmail -a "$EXTENSION")
Jul  5 03:10:05 qmgr     900031E923C7 removed
Jul  7 11:49:25 pickup   7E57B1E923E8 from=<>
Jul  7 11:49:25 cleanup  7E57B1E923E8 message-id=<xxx@radio-lms>
Jul  7 11:49:25 smtp     7E57B1E923E8 to=<xxx@backup.algerian-radio.dz> queued as 9494B1E923C8
Jul  7 11:49:25 qmgr     7E57B1E923E8 removed
Jul  7 11:49:25 smtpd    9494B1E923C8 client=localhost[127.0.0.1]
root@messagerie-secours[10.10.10.19] ~ # 

我想要这个(交换队列ID和进程名称然后排序)

Jul 5 03:10:02 3165F1E923C8 cleanup message-id=<xxx@messagerie.algerian-radio.dz>
Jul 5 03:10:02 3165F1E923C8 pickup  from=<www-data>
Jul 5 03:10:02 3165F1E923C8 qmgr    from=<xxx@messagerie.algerian-radio.dz> nrcpt=1
Jul 5 03:10:05 3165F1E923C8 qmgr    removed
Jul 5 03:10:05 3165F1E923C8 smtp    to=<xxx@messagerie.algerian-radio.dz> queued as 900031E923C7
Jul 5 03:10:05 900031E923C7 cleanup message-id=<xxx@messagerie.algerian-radio.dz>
Jul 5 03:10:05 900031E923C7 local   to=<xxx@messagerie.algerian-radio.dz> status=sent (delivered to command: procmail -a "$EXTENSION")
Jul 5 03:10:05 900031E923C7 qmgr    from=<xxx@messagerie.algerian-radio.dz> nrcpt=1
Jul 5 03:10:05 900031E923C7 qmgr    removed
Jul 5 03:10:05 900031E923C7 smtpd   client=localhost[127.0.0.1]
Jul 7 11:49:25 7E57B1E923E8 cleanup message-id=<xxx@radio-lms>
Jul 7 11:49:25 7E57B1E923E8 pickup  from=<>
Jul 7 11:49:25 7E57B1E923E8 qmgr    removed
Jul 7 11:49:25 7E57B1E923E8 smtp    to=<xxx@backup.algerian-radio.dz> queued as 9494B1E923C8
Jul 7 11:49:25 9494B1E923C8 smtpd   client=localhost[127.0.0.1]
Jul 7 19:35:23 3D95D1E923C8 cleanup message-id=1436290501c8e5fc05ec3357fc654e7bd6b0c72a18@
Jul 7 19:35:23 3D95D1E923C8 pipe    to=<xxx@algerian-radio.dz> status=sent (delivered via maildrop service)
Jul 7 19:35:23 3D95D1E923C8 qmgr    from=<xxx@infomaniak.com> nrcpt=1
Jul 7 19:35:23 3D95D1E923C8 qmgr    removed
Jul 7 19:35:23 3D95D1E923C8 smtpd   client=localhost[127.0.0.1]

在阅读thisthis之后,我能得到的最好是(可以交换队列ID和进程名称,但不能让列保持整洁):

root@messagerie-secours[10.10.10.19] ~ # awk '{temp=$4; $4=$5; $5=temp; print}' /tmp/first20lines | sort
Jul 5 03:10:02 3165F1E923C8 cleanup message-id=<xxx@messagerie.algerian-radio.dz>
Jul 5 03:10:02 3165F1E923C8 pickup from=<www-data>
Jul 5 03:10:02 3165F1E923C8 qmgr from=<xxx@messagerie.algerian-radio.dz> nrcpt=1
Jul 5 03:10:05 3165F1E923C8 qmgr removed
Jul 5 03:10:05 3165F1E923C8 smtp to=<xxx@messagerie.algerian-radio.dz> queued as 900031E923C7
Jul 5 03:10:05 900031E923C7 cleanup message-id=<xxx@messagerie.algerian-radio.dz>
Jul 5 03:10:05 900031E923C7 local to=<xxx@messagerie.algerian-radio.dz> status=sent (delivered to command: procmail -a "$EXTENSION")
Jul 5 03:10:05 900031E923C7 qmgr from=<xxx@messagerie.algerian-radio.dz> nrcpt=1
Jul 5 03:10:05 900031E923C7 qmgr removed
Jul 5 03:10:05 900031E923C7 smtpd client=localhost[127.0.0.1]
Jul 7 11:49:25 7E57B1E923E8 cleanup message-id=<xxx@radio-lms>
Jul 7 11:49:25 7E57B1E923E8 pickup from=<>
Jul 7 11:49:25 7E57B1E923E8 qmgr removed
Jul 7 11:49:25 7E57B1E923E8 smtp to=<xxx@backup.algerian-radio.dz> queued as 9494B1E923C8
Jul 7 11:49:25 9494B1E923C8 smtpd client=localhost[127.0.0.1]
Jul 7 19:35:23 3D95D1E923C8 cleanup message-id=1436290501c8e5fc05ec3357fc654e7bd6b0c72a18@
Jul 7 19:35:23 3D95D1E923C8 pipe to=<xxx@algerian-radio.dz> status=sent (delivered via maildrop service)
Jul 7 19:35:23 3D95D1E923C8 qmgr from=<xxx@infomaniak.com> nrcpt=1
Jul 7 19:35:23 3D95D1E923C8 qmgr removed
Jul 7 19:35:23 3D95D1E923C8 smtpd client=localhost[127.0.0.1]
root@messagerie-secours[10.10.10.19] ~ #

1 个答案:

答案 0 :(得分:1)

您可以使用以下awk命令:

awk '{t=$4;$4=$5;$5=t"\t"}1' file

它将第4列存储在临时变量t中,将$ 5分配给$ 4,最后将t分配给$5并附加tab1将始终评估为true,因此awk会打印当前(已修改)的行。

输出:

Jul 7 19:35:23 3D95D1E923C8 smtpd    client=localhost[127.0.0.1]
Jul 7 19:35:23 3D95D1E923C8 cleanup  message-id=1436290501c8e5fc05ec3357fc654e7bd6b0c72a18@
Jul 7 19:35:23 3D95D1E923C8 qmgr     from=<xxx@infomaniak.com> nrcpt=1
Jul 7 19:35:23 3D95D1E923C8 pipe     to=<xxx@algerian-radio.dz> status=sent (delivered via maildrop service)
Jul 7 19:35:23 3D95D1E923C8 qmgr     removed
Jul 5 03:10:02 3165F1E923C8 pickup   from=<www-data>
Jul 5 03:10:02 3165F1E923C8 cleanup  message-id=<xxx@messagerie.algerian-radio.dz>
Jul 5 03:10:02 3165F1E923C8 qmgr     from=<xxx@messagerie.algerian-radio.dz> nrcpt=1
Jul 5 03:10:05 3165F1E923C8 smtp     to=<xxx@messagerie.algerian-radio.dz> queued as 900031E923C7
Jul 5 03:10:05 3165F1E923C8 qmgr     removed
Jul 5 03:10:05 900031E923C7 smtpd    client=localhost[127.0.0.1]
Jul 5 03:10:05 900031E923C7 cleanup  message-id=<xxx@messagerie.algerian-radio.dz>
Jul 5 03:10:05 900031E923C7 qmgr     from=<xxx@messagerie.algerian-radio.dz> nrcpt=1
Jul 5 03:10:05 900031E923C7 local    to=<xxx@messagerie.algerian-radio.dz> status=sent (delivered to command: procmail -a "$EXTENSION")
Jul 5 03:10:05 900031E923C7 qmgr     removed
Jul 7 11:49:25 7E57B1E923E8 pickup   from=<>
Jul 7 11:49:25 7E57B1E923E8 cleanup  message-id=<xxx@radio-lms>
Jul 7 11:49:25 7E57B1E923E8 smtp     to=<xxx@backup.algerian-radio.dz> queued as 9494B1E923C8
Jul 7 11:49:25 7E57B1E923E8 qmgr     removed
Jul 7 11:49:25 9494B1E923C8 smtpd    client=localhost[127.0.0.1]

...这与你在问题中描述的不完全相同,但我希望它足够好。