0

I have this

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] ~ # 

I want this (swap queue ID and process name then 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]

Best I could get is this (could swap queue ID and process name, but coudln't keep the columns nice and tidy), after reading this and this :

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] ~ #
Community
  • 1
  • 1
ychaouche
  • 4,922
  • 2
  • 44
  • 52

1 Answers1

1

You can use the following awk command:

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

It stores column 4 in a temporary variable t, assigns $5 to $4 and finally assigns t to $5 and appends a tab. The 1 will always evaluate to true and therefore awk prints the current (modified) line.

This outputs:

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]

... which is not exactly the same as you described in the question but I hope it will be good enough.

hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • On the contrary, it's perfect. Thanks a bunch. I used your solution but instead of adding 1 I just added a print instruction like this `awk '{temp=$4; $4=$5; $5=temp"\t"; print}' /tmp/first20lines | sort` – ychaouche Jul 22 '15 at 11:24
  • You are welcome. Yes you can also use `print` if you prefer it for readability. It leads to the same result. – hek2mgl Jul 22 '15 at 11:35