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

Try something on the lines of the following ...

## 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.

Thanks Cameron,

Will have a play with it, much appreciated!

Maverick
:slight_smile:

No problem, again hope it helps.

I had a similar problem just last week and solved it in a similar fashion. :wink:

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:

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:

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

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. :wink:

Yep, I agree with that. But well worth the effort to learn. Good luck with unix scripting - lot of fun stuff ahead!