1

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 
4
  • You're script doesn't have the (FILENAME=- FNR=1) part, please post the whole thing. Commented Jun 5, 2015 at 7:50
  • The script receives the input from the pipe, all the awk code is there. The post below (marked as answer) solved the issue. Thanks for the help though! Commented Jun 5, 2015 at 7:57
  • I've posted your script with a few changes and I get what I think is the correct output. Commented Jun 5, 2015 at 7:59
  • 1
    Get rid of all the backslashes from the end of lines. Half of them aren't necessary and the other half are breaking your code. Commented Jun 5, 2015 at 12:39

2 Answers 2

5

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

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

More info here

Sign up to request clarification or add additional context in comments.

2 Comments

Note specifically which awks support that extension - follow inshsane's link.
gawk does this. current mawk does this as well. It is not in POSIX.
2

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:: 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.