Formatting dates in a file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Formatting dates in a file
# 1  
Old 12-18-2001
Question Formatting dates in a file

Hi!

Sitting with a headache this morning and can't get the brain around it!

I have a file with 32000 lines that needs to be inserted onto a database.

My problem is with the following:
First part between bars is a date and second some identifiers

|19511108|0001417|
|19481024|0001439|
|19400507|0001461|
|19400918|0001464|
|19350619|0001468|
|19350818|0001486|

I need to get it into the following format
|08-Nov-1951|0001417|
|24-Oct-1948|0001439|

Any ideas please, I have tried sed and tr but somehow not getting it to do what I would like to due to my incompetence with some unix commands.

Thank you,

Mav
maverick
# 2  
Old 12-18-2001
Try something on the lines of the following ...
Code:
## Short Name Month Listing.
shtmth="Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec"

# Read file
xStart=`cut -c1 $INFILE`
xYr=`cut -c2-5 $INFILE`
xMth=`cut -c6-7 $INFILE`
xDay=`cut -c8-9 $INFILE`
xRemainder=`cut -c10-18 $INFILE`

# Output
echo $xStart$xDay`echo $shtmth | cut -f$xMth -d" "`$xYr$xRemainder >> $OUTFILE

Hope that helps.
# 3  
Old 12-18-2001
Thanks Cameron,

Will have a play with it, much appreciated!

Maverick
Smilie
maverick
# 4  
Old 12-18-2001
No problem, again hope it helps.

I had a similar problem just last week and solved it in a similar fashion. Smilie
# 5  
Old 12-18-2001
Cameron's solution works fine if your data file has only a single line, but for multiple lines, you would need to nest that code in a loop, such as
cat $INFILE | while read line

Here are a couple of awk solutions, with identical results. First solution is column specific from start of line, using the default white space as field separation:
Code:
awk '
BEGIN {split("Jan Feb Mar Apr May Jun Jul Aug \
Sep Oct Nov Dec",mon)}
  {yyyy=substr($0,2,4)
   mm=substr($0,6,2)
   dd=substr($0,8,2)
   rem=substr($0,10)
   print "|" dd "-" mon[mm+0] "-" yyyy rem
  }' mavdata > newdata

Second solution is column specific from start of each word, with word delimiter changed to the bar character. And since each line starts with a bar, word 1 is null:
Code:
awk -F\| '
BEGIN {split("Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,\
Sep,Oct,Nov,Dec",mon,",")}
  {yyyy=substr($2,1,4)
   mm=substr($2,5,2)
   dd=substr($2,7,2)
   print "|" dd "-" mon[mm+0] "-" yyyy "|" $3 "|"
  }' mavdata > newdata

added code tags for readability --oombera

Last edited by oombera; 02-19-2004 at 01:21 PM..
Jimbo
# 6  
Old 12-18-2001
Neat.

Am only just learning awk having gotten the grasp of basic scripting.

I must say, it's a language all in itself from what I've seen thus far.

BTW - Thanks for the awk examples. Smilie
# 7  
Old 12-18-2001
Yep, I agree with that. But well worth the effort to learn. Good luck with unix scripting - lot of fun stuff ahead!
Jimbo
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. What is on Your Mind?

Grep file containing dates

How to grep a file containing dates to only last 30 days then move to another folder (7 Replies)
Discussion started by: kmarcus
7 Replies

2. Shell Programming and Scripting

Replacing the Dates in a file

Hello Gurus, I'm beginner in Shell scripting. I got a requirement to write a script. I have a file with below (similar) content If you can observe above content, there are many date values existed (with different dates) in a format: ddMonyyyy I have to write replace all these... (7 Replies)
Discussion started by: raghu.iv85
7 Replies

3. UNIX for Advanced & Expert Users

How to get the Missing dates between two dates in the table?

Hi Am Using Unix Ksh ... I have a Table called date select * from date ; Date 01/02/2013 06/02/2013 I need the output as Missing Date 01/02/2013 02/02/2013 03/02/2013 04/02/2013 05/02/2013 06/02/2013 (2 Replies)
Discussion started by: Venkatesh1
2 Replies

4. UNIX for Dummies Questions & Answers

How to write the dates between 2 dates into a file

Hi All, I am trying to print the dates that falls between 2 date variables into a file. Here is the example. $BUS_DATE =20120616 $SUB_DATE=20120613 Output to file abc.txt should be : 20120613,20120614,120120615,20120616 Can you pls help me accomplish this in LINUX. Thanks... (5 Replies)
Discussion started by: dsfreddie
5 Replies

5. Shell Programming and Scripting

lines between two dates in a file

Hi Every body I had a data in file as shown.. Now i want to get the data and insert into other file but between the two date ranges or from a past date to current date..Please Guide me in doing that.. Below is the data that looks like... child process 2588 still did not exit, sending a... (2 Replies)
Discussion started by: Reddy482
2 Replies

6. Shell Programming and Scripting

How to processing the log file within certain dates based on the file name

Hi I am working on the script parsing specific message "TEST" from multiple file. The log file name looks like: N3.2009-11-26-03-05-02.console.log.tar.gz N4.2009-11-29-00-25-03.console.log.tar.gz N6.2009-12-01-10-05-02.console.log.tar.gz I am using the following command: zgrep -a --text... (1 Reply)
Discussion started by: shyork2001
1 Replies

7. UNIX for Dummies Questions & Answers

Extract dates from file name.

Hi All, I have files with names as us_Gec1_wk_01to01_2008.TXT ad_EngEnt_wk_01to10_2008.TXT br_EngMov_wk_01to10_2008.TXT Over here, I need to extract the dates and the year and store them in variables. How can I achieve the same in bash. In case of ad_EngEnt_wk_01to10_2008.TXT ... (3 Replies)
Discussion started by: Swapna173
3 Replies

8. Shell Programming and Scripting

Need script to generate all the dates in DDMMYY format between 2 dates

Hello friends, I am looking for a script or method that can display all the dates between any 2 given dates. Input: Date 1 290109 Date 2 010209 Output: 300109 310109 Please help me. Thanks. :):confused: (2 Replies)
Discussion started by: frozensmilz
2 Replies

9. Shell Programming and Scripting

Help to switch dates from a file

Hi. I need some assistance with a file who at the end i must import to EXCEL. The problem is that i have a file with this inside: Output: JOBID START TIME END TIME ELAPSED CPU ------------------------------------------------------------ AVERAGE: ... (2 Replies)
Discussion started by: osramos
2 Replies

10. Shell Programming and Scripting

formatting dates

How do I insert a string into a particular character position using SED,AWK or KSH? I have a text file with contents (dates) below and I want to change the format from YYYYMMDD to YYYY/MM/DD. $ more input_date 20060227 20051201 20040130 I created a shell below which does the conversion... (4 Replies)
Discussion started by: stevefox
4 Replies
Login or Register to Ask a Question