Matt
Matt

Reputation: 879

Executing date utility in awk in linux bash script

I am trying to format some text from a file using awk.

The requirement is to replace the 3rd field of a colon-seperated file (:) representing epoch time with a formatted string representing the human-readable time in the follow format: DD/MM/YYYY

Here is an example file:

abc:$3$wHe$JKAP1Ry.CAcEGhD0J7SGVl.AMg.0:1427135400:0:120:7:30::
rst:$6$3WWbfvblr6FF92R5/n3mLdlSkARgfRm1:1427293800:0:40:7:30::
xyz:$1$xuTkkle203F$df.ixcn/mcuFIO90lndn:1420478400:0:90:7:30::
def:$4$vid2003mDEOF$dc2.Rkdlkfdiw8/cib6:1389547200:0:120:7:30::
ab:*$5P1wHeEG$JKA2ya.ikol30.de/ldiv.230:1449771300:0:120:7:30::
xy:$1k3lc930vs.lskdie/sldiemDIIsdk193n:1429995693:0:50:7:30::
xyy:*$tkwsMt972w.Csrl5jr.23nsoijsleqJK:1429995889:0:120:7:30::

By copying and pasting a one of the values from the 3rd field into the a command using date I have been able to create the desired results:

date -d @1427135400 +"%d/%m/%Y"
23/03/2015

Here is the awk command that I am trying to execute in the script, I have been tweeking the script here and there in hopes to get it to work, but with no luck. Note that $userFound has already stored a single line from the file listed above:

  echo $userFound | awk -F':' '{  
                                                    if ( $2 ~ /^\*/ ) {$2="L"}              \
                                                    if ($2 ~ /^[^*]/) {$2="P"}              \
                                                    cmd="date -d @"$3" +\"%d/%m/%Y\""       \
                                                    cmd | getline time                      \
                                                    close(cmd)                              \
                                            }                                               \
                                            END {                                           \
                                            print $1":"$2":"time":"$4":"$5":"$6":"$7":"$8":"$9      \
                                            }'

Running the current script, I get the following output:

awk: cmd. line:5: (FILENAME=- FNR=1) fatal: expression for `|' redirection has null string value

Upvotes: 1

Views: 596

Answers (2)

bkmoney
bkmoney

Reputation: 1256

If you really need to use the date (because you don't have GNU awk), your script works with a few changes, namely the print statement shouldn't be in the END block.

BEGIN {
    FS = ":"
    OFS = ":"
}
{
    if ( $2 ~ /^\*/ ) {$2="L"}      
    if ($2 ~ /^[^*]/) {$2="P"}
    cmd="date -d @" $3 " +\"%d/%m/%Y\""
    cmd | getline time
    close(cmd)
    print $1, $2, time, $4, $5, $6, $7, $8, $9
}

If you put this script in a file called a.awk, then you can do

awk -f a.awk foo.txt

And if foo.txt looks like this:

abc:$3$wHe$JKAP1Ry.CAcEGhD0J7SGVl.AMg.0:1427135400:0:120:7:30::
rst:$6$3WWbfvblr6FF92R5/n3mLdlSkARgfRm1:1427293800:0:40:7:30::
xyz:$1$xuTkkle203F$df.ixcn/mcuFIO90lndn:1420478400:0:90:7:30::
def:$4$vid2003mDEOF$dc2.Rkdlkfdiw8/cib6:1389547200:0:120:7:30::
ab:*$5P1wHeEG$JKA2ya.ikol30.de/ldiv.230:1449771300:0:120:7:30::
xy:$1k3lc930vs.lskdie/sldiemDIIsdk193n:1429995693:0:50:7:30::
xyy:*$tkwsMt972w.Csrl5jr.23nsoijsleqJK:1429995889:0:120:7:30::

Then this is the output:

abc:P:23/03/2015:0:120:7:30::
rst:P:25/03/2015:0:40:7:30::
xyz:P:05/01/2015:0:90:7:30::
def:P:12/01/2014:0:120:7:30::
ab:P:10/12/2015:0:120:7:30::
xy:P:25/04/2015:0:50:7:30::
xyy:P:25/04/2015:0:120:7:30::

Upvotes: 2

anishsane
anishsane

Reputation: 20980

Don't use date command like you are trying to use.

Use awk function: strftime("%d/%m/%Y", $3).

More info here

Upvotes: 5

Related Questions