6

I want to convert following string (20140805234656) into date time stamp (2014-08-05 23:46:56).I am new to gawk and I don't know the exact syntax,how can I put - at every 5,8 and : at every 14,17 and put " " at 11 index. Is there any efficient way to achieve this in awk?

EDIT

Please note that I have string as variable in awk.I generated it during some processing of records.

14

One way of doing it using GNU awk is this:

echo 20140805234656 | awk 'BEGIN { FIELDWIDTHS = "4 2 2 2 2 2" } { printf "%s-%s-%s %s:%s:%s\n", $1, $2, $3, $4, $5, $6 }'
  • Ah. This is a much efficienter way! Nicely done. – Valentin Bajrami Aug 14 '14 at 14:22
  • @Subbeh thanks for your answer,can u suggest how can i use FIELDWIDTH with variable. – user2801682 Aug 14 '14 at 14:27
  • @user2801682, $0 = your_variable – Stéphane Chazelas Aug 14 '14 at 14:52
10

You can use substring as follow:

echo 20140805234656 | awk '{print substr($0,1,4)"-"substr($0,5,2)"-"substr($0,7,2)" "substr($0,9,2)":"substr($0,11,2)":"substr($0,13,2) }'

Probably there are easier ways too.

  • 4
    This one has the benefit of being standard/portable. – Stéphane Chazelas Aug 14 '14 at 14:26
  • Ah, cool. Thanks for reviewing and fixing the offset! – Valentin Bajrami Aug 14 '14 at 14:28
6

Another GNU awk answer

gawk -v timestamp=20140805234656 '
    BEGIN {
        if (match(timestamp, /(....)(..)(..)(..)(..)(..)/, m)) {
            t = mktime(m[1] " " m[2] " " m[3] " " m[4] " " m[5] " " m[6])
            print strftime("%F %T", t)
            print strftime("%c", t) 
        }
    }
'
2014-08-05 23:46:56
Tue Aug  5 23:46:56 2014
5

Another GNU awk approach:

result = gensub("(....)(..)(..)(..)(..)", "\\1-\\2-\\3 \\4:\\5:", 1, your_variable)
3

Try:

echo 20140805234656 | awk ' { printf "%s-%s-%s %s:%s:%s", 
                                     substr($0,1,4),
                                     substr($0,5,2),
                                     substr($0,7,2),
                                     substr($0,9,2),
                                     substr($0,11,2),
                                     substr($0,13,2)
                             } '

Or, if you want to asign it to a variable first:

echo 20140805234656 | awk ' { d=sprintf ("%s-%s-%s %s:%s:%s", 
                                     substr($0,1,4),
                                     substr($0,5,2),
                                     substr($0,7,2),
                                     substr($0,9,2),
                                     substr($0,11,2),
                                     substr($0,13,2));
                              print "Date is: " d
                             } '
0

Another way:

$ echo 20140805234656 |
  awk '{split($0,a,"");printf("%s-%s-%s %s:%s:%s\n",a[1]a[2]a[3]a[4],a[5]a[6],a[7]a[8],a[9]a[10],a[11]a[12],a[13]a[14])}'
2014-08-05 23:46:56
  • 3
    It is not portable. The behaviour with an empty separator is unspecified by POSIX and in practice, it doesn't work with the original awk or nawk. – Stéphane Chazelas Aug 14 '14 at 20:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.