交换两个字段并保持选项卡正确

Swapping two fields AND keeping the tabs right

我有这个

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]

我能得到的最好的是这个(可以交换队列 ID 和进程名称,但不能保持列整洁),阅读 this and this :

root@messagerie-secours[10.10.10.19] ~ # awk '{temp=; =; =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] ~ #

您可以使用以下 awk 命令:

awk '{t=;=;=t"\t"}1' file

它将第 4 列存储在临时变量 t 中,将 $5 分配给 $4,最后将 t 分配给 </code> 并附加一个 <code>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]

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