change date stamp in a flat file

Posted on 2006-04-12
Medium Priority
Last Modified: 2010-04-21
I have a flat file which has columns to be exported to a oracle table, this flat file has inside date format in the form of 2006-02-01 and 2006-02-28, I wanted to change every line that has this date format (could be any day during the month of feb) to start date as 2006-02-01 and 2006-02-07 since I want to restrict data being copied to just 7 days.

This was a problem to do manually since the file is large and also the start and end dates can be any date between 01 to 28 for the month of february.

Is there any way to do this using command line commands. Unix on which I am trying is LINUX.
Question by:mahjag
LVL 85

Assisted Solution

ozo earned 195 total points
ID: 16439768
perl -i -pe 's/(200\d-02-)(\d\d)/sprintf"$1%02d",$2>7?7:$2/eg' file

Author Comment

ID: 16440435
do we need to have perl for this command, I dont think I have that?
LVL 85

Expert Comment

ID: 16440824
You could probably do it in awk, Linux usually has a perl

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.


Expert Comment

ID: 16440882
you can use the touch command
LVL 51

Accepted Solution

ahoffmann earned 180 total points
ID: 16443291
using awk, assuming that your date is the second column (columns are space seperated):

awk '($2!~/^2006-02-0[1-7]$/{$2="2006-02-07";}{print}' file

sed would be fine to, but it struggles with huge files, usually ...
LVL 85

Expert Comment

ID: 16443374
That would also change days in January or March to 2006-02-07
but perhaps that's what you want.
You don't really say what you would want to do if you see anything other than "2006-02-01 and 2006-02-28"
LVL 51

Expert Comment

ID: 16443727
> That would also change days in January or March to 2006-02-07
I assumed one world's standard format of writing dates, not the proprietary scary uncle Sam format ;-)
LVL 85

Expert Comment

ID: 16448638
That would also change 2006-01-07 or 2006-03-28 or 2007-02-28 to 2006-02-07
I interpreted the question to mean that the first two to be unchanged and the third to be changed to 2007-02-07
I'm still not sure what the intended interpretation was.

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tech tip describes how to install the Solaris Operating System from a tape backup that was created using the Solaris flash archive utility. I have used this procedure on the Solaris 8 and 9 OS, and it shoudl also work well on the Solaris 10 rel…
My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (http://www.experts-exchange.com/articles/OS/Unix/Solaris/Installing-the-Solaris-OS-From-the-Flash-Archive-on-a-Tape.html), discussed installing the Solaris Operating S…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Suggested Courses
Course of the Month15 days, 4 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question