简体   繁体   English

在Linux bash脚本中的awk中执行日期实用程序

[英]Executing date utility in awk in linux bash script

I am trying to format some text from a file using awk. 我正在尝试使用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 要求是用表示人类可读时间的格式化字符串,用以下格式替换表示时期的冒号分隔文件(:)的第三个字段: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将第三个字段中的值之一复制并粘贴到a命令中,我已经能够创建所需的结果:

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. 这是我试图在脚本中执行的awk命令,我一直在这里和那里对脚本进行修改,希望能使其正常工作,但是没有运气。 Note that $userFound has already stored a single line from the file listed above: 请注意,$ userFound已经从上面列出的文件中存储了一行:

  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

Don't use date command like you are trying to use. 不要像尝试使用date命令那样使用date命令。

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

More info here 更多信息在这里

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. 如果您确实需要使用date (因为您没有GNU awk),则您的脚本可以进行一些更改,即print语句不应位于END块中。

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 如果将此脚本放在名为a.awk的文件中,则可以执行

awk -f a.awk foo.txt

And if foo.txt looks like this: 如果foo.txt看起来像这样:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM