Printing only the first field in a string
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
If your date string is stored in a variable, then you don't need to run an external program like cut
, awk
or sed
, because modern shells like bash
can perform string manipulation directly which is more efficient.
For example, in bash:
$ s="1/10/2013 23:41"$ echo "${s% *}"1/10/2013