OOPS! Sorry, I didn't read your query carefully enough and proceeded on the basis that the "cmd" numers were 4-figure. You will need to extract what follows "cmd" as a separate field. The following mix of awk, sed and sort works (the source file is called "temp.txt" here, and I have added a 3-figure "cmd" entry):
$cat temp.txt 20110905-000107 803 cmd1853 - Host:/127.0.0.1 opened command interface from port: 54365 20110905-000108 761 cmd1854 - Command: 20110905-000108 766 cmd1853 - Response: 201 Service ready 20110905-000108 766 cmd1854 - Response: 201 Service ready 20110905-000108 803 cmd1854 - Host:/127.0.0.1 opened command interface from port: 57052 20110905-000108 804 cmd1853 - Host:/127.0.0.1 closed command interface from port: 54365 20110905-000113 804 cmd1854 - Host:/127.0.0.1 closed command interface from port: 57052 20110905-000313 761 cmd1858 - Command: 20110905-000313 761 cmd858 - Command: 20110905-000313 766 cmd1856 - Response: 201 Service ready 20110905-000313 766 cmd1858 - Response: 201 Service ready
cat temp.txt | awk '{print $3 " " $0}' | sed 's/^cmd//' | sort -n | awk '{$1=""};{print $0}' | sed 's/^ //'
20110905-000313 761 cmd858 - Command: 20110905-000107 803 cmd1853 - Host:/127.0.0.1 opened command interface from port: 54365 20110905-000108 766 cmd1853 - Response: 201 Service ready 20110905-000108 804 cmd1853 - Host:/127.0.0.1 closed command interface from port: 54365 20110905-000108 761 cmd1854 - Command: 20110905-000108 766 cmd1854 - Response: 201 Service ready 20110905-000108 803 cmd1854 - Host:/127.0.0.1 opened command interface from port: 57052 20110905-000113 804 cmd1854 - Host:/127.0.0.1 closed command interface from port: 57052 20110905-000313 766 cmd1856 - Response: 201 Service ready 20110905-000313 761 cmd1858 - Command: 20110905-000313 766 cmd1858 - Response: 201 Service ready
Sorry for the previous error! Ted.
On 06-Sep-11 12:58:40, Ted Harding wrote:
On 06-Sep-11 12:35:57, Mark Rogers wrote:
I have a log file which looks like this: 20110905-000107 803 cmd1853 - Host:/127.0.0.1 opened command interface from port: 54365 20110905-000108 761 cmd1854 - Command: 20110905-000108 766 cmd1853 - Response: 201 Service ready 20110905-000108 766 cmd1854 - Response: 201 Service ready 20110905-000108 803 cmd1854 - Host:/127.0.0.1 opened command interface from port: 57052 20110905-000108 804 cmd1853 - Host:/127.0.0.1 closed command interface from port: 54365 20110905-000113 804 cmd1854 - Host:/127.0.0.1 closed command interface from port: 57052 20110905-000313 761 cmd1858 - Command: 20110905-000313 766 cmd1856 - Response: 201 Service ready 20110905-000313 766 cmd1858 - Response: 201 Service ready
I want to sort it in numeric order on the cmd value. Note that the cmd number isn't always 4 digits and I want cmd9 to appear before cmd10, etc.
Can I do this with sort?
I've seen examples that suggest that something along the lines of sort -n -k3.4 .. would do it but I can't get it working in any of the various permutations I've tried.
-- Mark Rogers // More Solutions Ltd (Peterborough Office) // 0844 251 1450 Registered in England (0456 0902) 21 Drakes Mews, Milton Keynes, MK8 0ER
sort -n -k 3.4,3.7
does it -- see man sort and the definition of -k POS1[,POS2] and the explanation of POS at the bottom of the man page.
I think your "sort -n -k3.4" only sorts on the first digit!
Ted.
E-Mail: (Ted Harding) ted.harding@wlandres.net Fax-to-email: +44 (0)870 094 0861 Date: 06-Sep-11 Time: 13:58:37 ------------------------------ XFMail ------------------------------
main@lists.alug.org.uk http://www.alug.org.uk/ http://lists.alug.org.uk/mailman/listinfo/main Unsubscribe? See message headers or the web site above!
-------------------------------------------------------------------- E-Mail: (Ted Harding) ted.harding@wlandres.net Fax-to-email: +44 (0)870 094 0861 Date: 06-Sep-11 Time: 14:14:28 ------------------------------ XFMail ------------------------------