Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have lots of HTML files contains date with format July 29th, 2011

I want to change date format July 29th, 2011 to 20110729, December 9th, 2010 to 20101209, etc.

I think sed may works but still can't find the solution because of its indirect sequence.

share|improve this question
    
You'd be better off with perl which can read one date format and output another, but my perl-foo isn't good enough, hopefully someone else will chip in. –  EightBitTony Jul 30 '11 at 9:32

1 Answer 1

up vote 7 down vote accepted

This sort of thing is much more difficult than it should be with most tools. GNU awk and Perl's POSIX module both give you a strftime(), but not a strptime(), which is what you want.

In any case, it is still pretty easy with Perl...

$ perl -MDate::Manip -lpe '$_=UnixDate(ParseDate($_), "%Y%m%d")' <<END
> July 29th, 2011
> December 9th, 2010
> END
20110729
20101209

Obviously there's more to it, since you actually want to convert the HTML. If you can figure out a regex with sed that will work to find the date strings, you should be able to do something very similar with Perl.

$ perl -MDate::Manip -lpe 's/(")([^"]+)(")/$1 . UnixDate(ParseDate($2), "%Y%m%d") . $3/ge' <<END
dates enclosed "July 29th, 2011" in quotes
"December 9th, 2010"
END
dates enclosed "20110729" in quotes
"20101209"
share|improve this answer
    
Where/How do I get the Date::Manip module? ... My perl gives me an error, "Can't locate Date/Manip.pm in @INC ...". Is it an extra module I need to install somehow? ... I'm using perl, v5.10.1 –  Peter.O Jul 30 '11 at 16:20
    
@fred: Most distro package managers will have this, try searching there. On my system the package name is perl-Date-Manip but yours will vary. You could also get it using cpan. –  Caleb Jul 30 '11 at 16:59
    
@StevenPritchard: You write good and useful answers, I'm glad to see you around this community. I would just like to note that this community depends on votes in order to keep functioning. Not only do votes help sort answers but they shape the site by changing what is visible where, search results, and most importantly help keep good users around to police the site. Without votes it dies. You have only cast 3 votes total. You are given 30 to use each day! Please consider using your knowledge to improve this site by voting on on what you think is valuable or not. –  Caleb Jul 30 '11 at 17:05
    
@Caleb: Thanks. It's in Ubuntu's repository as libdate-manip-perl .... @Steven Pritchard: It is a nice solution... Thanks. –  Peter.O Jul 30 '11 at 18:19
1  
GNU date does have both parsing and printing, but July 29th, 2011 isn't a recognized format (July 29, 2011 works), and you'd need to put quite a bit of shell and sed/awk infrastructure around it. –  Gilles Jul 31 '11 at 22:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.