Printing only the first field in a string

user2099444 picture user2099444 · Feb 22, 2013 · Viewed 74.4k times · Source

I have a date as 12/12/2013 14:32 I want to convert it into only 12/12/2013. The string can be 1/1/2013 12:32 or 1/10/2013 23:41 I need only the date part.

Answer

Chris Seymour picture Chris Seymour · Feb 22, 2013

You can do this easily with a variety of Unix tools:

$ cut -d' ' -f1  <<< "12/12/2013 14:32"
12/12/2013

$ awk '{print $1}' <<< "12/12/2013 14:32"
12/12/2013

$ sed 's/ .*//' <<< "12/12/2013 14:32"
12/12/2013

$ grep -o "^\S\+"  <<< "12/12/2013 14:32"
12/12/2013

$ perl -lane 'print $F[0]' <<< "12/12/2013 14:32"
12/12/2013