Remove new line and convert Month to Decimal

# Sample input

common-name www.test.com.au
expiration Dec 21 01:00:31 2017 GMT
common-name www.test1.com.au
expiration Jan 19 04:41:03 2018 GMT

# Desired Output
# Field 1: Domain name
# Field 2: Date/time converted to Austraian format DD/MM/YYYY and on the same line as Domain Name.
# These will be import to excel and sort by date later.
# Repeat

www.test.com.au		21/12/2017
www.test1.com.au	19/01/2018

# My attempt below

awk '/common-name/ {print $2}/expiration/{print $3,$2,$5}'

# Current output

www.test.com.au
21 Dec 2017
www.test1.com.au
19 Jan 2018

1) How to remove the new line?
2) If Month can not be converted, i will look at excel function.

Hello thangbom,

Could you please try following and let me know if this helps(haven't tested it though).

awk '/common-name/{VAL=$2} /expiration/{num=split("Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec", A,",");for(i=1;i<=num;i++){B[A]=i};print VAL FS $3"/"B[$2]"/"$(NF-1)}'  Input_file

Output will be as follows.

www.test.com.au 21/12/2017
www.test1.com.au 19/1/2018

Thanks,
R. Singh

1 Like

Try this:

awk '
/common-name/ {DN=$2}
/expiration/{
   print DN,$3 "/" 1 + int(\
       index("JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC",\
       toupper($2))/3) "/" $5
}' infile

Edit: RaviderSingh13 just beat me on this, but still worth keeping for alternate month conversion logic

1 Like

Thank you. Works fine.

---------- Post updated at 01:15 PM ---------- Previous update was at 01:14 PM ----------

Thank you. Your code works as well.

Hi R Singh,
I 'd like to make change to the script for some new requirements. Hope you can give me a quick explanation how it works so i can go away and study it. Thanks

---------- Post updated at 03:39 PM ---------- Previous update was at 03:35 PM ----------

Hi Chubber_XL, sorry for a simple question but is there a way to run this code on the 1 single line instead of multiple lines.

How exactly would your input and the desired output look like?

Both above solutions works fine. I am fairly new to this so just need a short explanation how the codes work. That's all. Thank you.

Hello thangbom,

Following may help you in same.

awk '/common-name/                                                                 ##### Searching for a string "common-name" here if any line contains this in it.
     {VAL=$2}                                                                      ##### setting variable named VAL's value to $2 here.
     /expiration/                                                                  ##### Searching for a string "expiration" here if any line contains this in it.
    {num=split("Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec", A,",");          ##### using split(built-in keyword of awk) to create an array named A, which has values of all the months Jan, Feb etc in it and mentioned separator as , in it.
    for(i=1;i<=num;i++){                                                           ##### starting a loop here which will start from i=1 to till the value of num(where num is the length of array A(which we created by split in above step)).
    B[A]=i};                                                                    ##### creating an array B whose index is value of array A whose index is variable i, so it will be like B[A[1]]=B["Jan"]=1 and so on like this..
    print VAL FS $3"/"B[$2]"/"$(NF-1)                                              ##### printing the variable VAL here then mentioning FS(field seprator) which is space by default in awk. then printing values of $3"/"B[$2](where B[$2]' will be month's number)"/"$(NF-1)(is the second last field of each line, which is a year), which is date, month and year respectively.
   }'  Input_file                                                                  ##### Mentioning the Input_file here.
 

Thanks,
R. Singh

1 Like