Hi,
I have a file where there is a date field (single line variable length file)
how to extract just the date portion from it
the position of date field may vary anywhere in the line
but will always have the format mm-dd-yyyy
for eg .
xxxxxxxxxxxxxxx09-10-2006xxxxxxxxxxxxxxxxxxxx
mona
2
Try this..
$ echo "xxxxxxxxxxxxxxx09-10-2006xxxxxxxxxxxxxxxxxxxx" | sed 's/^.*\([0-9]\{2\}-[0-9]\{2\}-[0-9]\{4\}\).*$/\1/'
09-10-2006
Hi Mona
The date which i gave was just for example
it may be any date in general so i cannot hardcode; all i know is the format will be the same
Python alternative:
import time
mm,yy = time.strftime("%m,%Y",time.localtime()).split(",")
s[ s.find(mm) : s.find(yy) + len(yy) ]
as i mentioned the date given was just an example
It will be any date in general; so cannot hardcode
mona
6
No where i have hard coded the date in my solution. If the format MM-DD-YYYY occurs anywhere in the line then it will output.
$ cat samp
xxxxxxxxxxxxxxx09-10-2006xxxxxxxxxxxxxxxxxxxx
sfsd12-12-2007dfdfd
08-30-2003dfdfdfdf
fgfgfgf01-01-2001
$ sed 's/^.*\([0-9]\{2\}-[0-9]\{2\}-[0-9]\{4\}\).*$/\1/' samp
09-10-2006
12-12-2007
08-30-2003
01-01-2001
$
HTH,
Mona