0

I have a file with lines that all begin with a date, followed by a tab, followed by a random number of words and spaces—some of which include numbers. For example:

    20140217    iPhone Upgrade Available
    20131101    Job Application Due
    20131219    Renew or return all library books
    20131114    Pay cell phone bill

I'm trying to sort this file by the date string and only the date string.

As per this thread, I've tried all kinds of combinations of sort -t$'\t' and -k1, but I keep getting garbled results.

Any help would be much appreciated. Also, it IS possible for me to replace that tab with a space or another character, if that would help for any reason.

Community
  • 1
  • 1
craigeley
  • 352
  • 2
  • 12

2 Answers2

1

You can use it like this:

sort -k1,1 file
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • I intentionally didn't use `-n` since OP wanted to use only date part to sort and date part is always 8 digit. – anubhava Oct 08 '13 at 21:07
1

you may want to try

sort -n -k1,1 file

output is

20131101    Job Application Due
20131114    Pay cell phone bill
20131219    Renew or return all library books
20140217    iPhone Upgrade Available
CS Pei
  • 10,869
  • 1
  • 27
  • 46
  • For the actual example I recently provided above, this is what that yields, on a single line: 20131114 Pay cell phone bill library books – craigeley Oct 08 '13 at 20:49
  • The output you gave is exactly what I want, but for whatever reason that is still not working for me in OSX Terminal while outputting the results to a new file. I'm getting a file that is the same as my input, using this command `sort -n -k1,1 /Users/Username/Desktop/input.txt > /Users/Username/Desktop/output.txt` – craigeley Oct 08 '13 at 21:12
  • so is your terminal `xterm`? I think you can run `xterm` on `OS X` right? – CS Pei Oct 08 '13 at 21:18
  • Okay—the problem had something do to with the way the lines were broken in my files. This is working for me now. Thanks! – craigeley Oct 08 '13 at 21:22
  • 1
    For anyone else having this problem or wondering: Because the file was being generated with Applescript, it was creating Classic Mac (CR) linebreaks instead of Unix (LF) ones. – craigeley Oct 08 '13 at 21:27