Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question

6 Answers 6

up vote 12 down vote accepted

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 }'
share|improve this answer
    
Ah. This is a much efficienter way! Nicely done. –  val0x00ff 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

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.

share|improve this answer
3  
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! –  val0x00ff Aug 14 '14 at 14:28

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
share|improve this answer

Another GNU awk approach:

result = gensub("(....)(..)(..)(..)(..)", "\\1-\\2-\\3 \\4:\\5:", "", your_variable)
share|improve this answer

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
                             } '
share|improve this answer

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
share|improve this answer
1  
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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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