7

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.

7 Answers 7

15

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 }'
3
  • Ah. This is a much efficienter way! Nicely done. Aug 14, 2014 at 14:22
  • @Subbeh thanks for your answer,can u suggest how can i use FIELDWIDTH with variable. Aug 14, 2014 at 14:27
  • @user2801682, $0 = your_variable Aug 14, 2014 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.

2
  • 4
    This one has the benefit of being standard/portable. Aug 14, 2014 at 14:26
  • Ah, cool. Thanks for reviewing and fixing the offset! Aug 14, 2014 at 14:28
7

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
1
  • 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. Aug 14, 2014 at 20:02
0

I have a question I have a text file that I want to use awk to change the date part and put the current date Of course, I wrote this and it works, but I have to give it the date manually, and what I want is for it to automatically replace the day's date.

Code : awk '{gsub(/ISODate("[0-9][0-9][0-9][0-9][-][0-9][0-9][-][0-9][0-9]/,"ISODate("2022-10-02");print}' rasoul-02.json

2
  • If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. - From Review Oct 6, 2022 at 8:54
  • This is a question, please delete and ask in another thread.
    – number9
    Oct 13, 2022 at 12:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .