dateawk2-digit-year

Number date to Alphabetic in awk


I have a file: ifile.txt

83080603   55  72
87090607   83  87
88010612   82  44
89080603   55  72
00110607   83  87
01030612   82  44
05120618   84  44

The 1st column shows the date and time with the format YYMMDDHH.

I would like to print the first column as hrHDDMMMYYYY ofile.txt

03H06Aug1983   55  72
07H06Sep1987   83  87
12H06Jan1988   82  44

and so on

I can't able to convert the digit month-year to text month. My script is

awk '{printf "%s%5s%5s\n",
substr($0,7,2)"H"substr($0,5,2)substr($0,3,2)substr($0,1,2), $2, $3}' ifile.txt

Solution

  • EDIT: Adding solution as per OP's comment for adding year as per condition here.

    awk -v curr_year=$(date +%y) '
    BEGIN{
      num=split("Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec",arr,",")
      for(j=1;j<=num;j++){
        key=sprintf("%02d",j)
        months[key]=arr[j]
      }
    }
    {
      year=substr($1,1,2)>=00 && substr($1,1,2)<=curr_year?"20":"19"
      $1=substr($1,length($1)-1)"H"substr($1,length($1)-3,2) months[substr($1,3,2)] year substr($1,1,2)
      print
      year=""
    }
    ' Input_file
    


    Could you please try following, written on mobile and successfully tested it on site https://ideone.com/4zYMu7 this also assume that since your Input_file first 2 letters denote year and you want to print only 19 in case there is another logic to get exact year then please do mention it

    awk '
    BEGIN{ 
      num=split("Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec",arr,",")
      for(j=1;j<=num;j++){
        key=sprintf("%02d",j)
        months[key]=arr[j]
      }
    }
    {
      $1=substr($1,length($1)-1)"H"substr($1,length($1)-3,2) months[substr($1,3,2)] "19" substr($1,1,2)
      print
    }
    ' Input_file