Using awk, how to 'properly' print the rest of the string?

Hi,

Script below is working. But I want to put a "=" after the new date format BUT excluding the extra space :frowning:

$ cat x.bash
#!/bin/bash
#

cat x.txt
echo

awk '
   BEGIN {
      split("JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC", month, " ")
      for (i=1; i<=12; i++) mdigit[month]=i
   }
   { m=substr($1,4,3)
     $1 = sprintf("%04d-%02d-%02d=",substr($1,8,4),mdigit[m],substr($1,1,2))
     print
   }' x.txt | sort | uniq > x.txt.00

cat x.txt.00
echo

Example run below:

$ ./x.bash
18-MAR-2020 04:28:13 * (CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=

2020-03-18= 04:28:13 * (CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=

Note the space after the day, trying to work out how to get rid of it :frowning: The preferred output is

2020-03-18=04:28:13 * (CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=

Tried this one below and still the same :(, still have the space

$1 = sprintf("%04d-%02d-%02d=",substr($1,8,4),mdigit[m],substr($1,1,2),substr($0,13))

That space is the OFS between $1 and $2 . Try like

awk '
BEGIN   {for (i=split("JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC", month); i; i--) mdigit[month] = i
        }
        {$1 = sprintf("%04d-%02d-%02d=",substr($1,8,4),mdigit[substr($1,4,3)],substr($1,1,2)) $2
         $2 = ""         
         print
        }
  ' file
2020-03-18=04:28:13  * (CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=

Note the two spaces after the date/time string, the two OFS around the empty $2 . If that hurts, put in an e.g. sub (FS FS, FS)

EDIT: or, try

    {sub ($1 FS $2, sprintf("%04d-%02d-%02d=",substr($1,8,4),mdigit[substr($1,4,3)],substr($1,1,2)) $2)
     print
    }
1 Like

Thanks as usual RudiC. I used the sub option instead

   BEGIN {
         for ( i = split( "JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC", month ) ; i ; i-- )
         mdigit[month] = i
      }
      {
         sub ( $1 FS $2, sprintf( "%04d-%02d-%02d=",substr($1,8,4),mdigit[substr($1,4,3)],substr($1,1,2) ) $2 )
         print
      }