bash 在 awk 字符串中调用“date”命令,格式为 +%a
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29203110/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Invoking 'date' command inside awk string, with +%a formatting
提问by Mercutio
Still newish to the site, but here goes... Basically I'm storing events in multiple files, with each event being a line and each line containing dates ($1), start($2) and stop($3) times and several other pieces of data. I use two double underscores ("__") as Field Separators. I've been using a variety of shell scripts to manage the data, and I was using awk to calculate stats and I'm having trouble invoking the date function so I can get a total by day of the week. After much tinkering and scanning of discussion boards I got to this:
该网站仍然是新的,但这里是......基本上我将事件存储在多个文件中,每个事件是一行,每行包含日期($ 1),开始($ 2)和停止($ 3)时间和其他几个数据。我使用两个双下划线(“__”)作为字段分隔符。我一直在使用各种 shell 脚本来管理数据,我使用 awk 来计算统计数据,但在调用日期函数时遇到问题,因此我可以按星期几获得总数。在对讨论板进行大量修补和扫描后,我得到了这个:
ls /home/specified/folder/MBRS.db/* |
xargs -n 1 -I % awk -F"__" ' == "CLOSED" && >= "'$backDATE'" { print ls /home/lingotech/Einstein/data/MBRS.db/* |
xargs -n 1 -I % awk -F"__" ' == "CLOSED" && >= "'$backDATE'" { print for y in Sun Mon Tue Wed Thu Fri Sat; do
for directory in $( ls /home/specified/directory/MBRS.db/* | xargs -n 1 ); do
printf "."
[[ $( cat $directory | awk -F"__" ' == "CLOSED" && >= "'$backDATE'" { print }' | xargs -n 1 -I z date +%a -d z ) == "$y" ]] && echo BLAH
done
done
}' % |
awk 'BEGIN{FS="__"}system("date --date="" +%a") == "Mon" {print " "}'`
}' % |
awk 'BEGIN{FS="__"}{specDATE=system("date --date="" +%a")} specDATE == "Tue" {print " "}'
or
或者
awk 'BEGIN{FS="__"} NF == 10 && >= "'$backDATE'" && == "'$x'" && == "CLOSED" {while ( "date +%a -d """ | getline newDAY){if (newDAY == "'$y'") print " "}}' /home/absolute/path/*
Instead of outputting the start and stop times, I'm getting a list of all the different days of the week for each entry.
我没有输出开始和停止时间,而是为每个条目获取一周中所有不同天数的列表。
I've tried more variations of the date usage than I care to admit, including:
我尝试了比我愿意承认的更多的日期用法变化,包括:
awk -v backDATE="$backDATE" -v x="$x" -v y="$y" '
BEGIN { FS="__" }
(NF == 10) && ( >= backDATE) && ( == x) && ( == "CLOSED") {
cmd = "date +%a -d \"" "\""
while ( (cmd | getline newDAY) > 0 ) {
if (newDAY == y) {
print ,
}
}
close(cmd)
}
' /home/absolute/path/*
Some helpful explanation of what I'm screwing up would be much appreciated. Thanks in advance. Oh and I'm storing the date in YYMMDD format but that doesn't seem to be an issue for ubuntu server's version of 'date'.
对我搞砸的事情的一些有用的解释将不胜感激。提前致谢。哦,我以 YYMMDD 格式存储日期,但这似乎不是 ubuntu 服务器版本的“日期”的问题。
采纳答案by Ed Morton
I don't know about all the rest of it (too much text for my reading tastes!) but wrt the answer you posted, this part of it:
我不知道其余的所有内容(对于我的阅读口味来说文字太多了!)但是写了您发布的答案,这部分内容:
$ x="hello world"
$ awk 'BEGIN{ print '$x' }'
awk: cmd. line:1: BEGIN{ print hello
awk: cmd. line:1: ^ unexpected newline or end of string
$ awk 'BEGIN{ print "'$x'" }'
awk: cmd. line:1: BEGIN{ print "hello
awk: cmd. line:1: ^ unterminated string
awk: cmd. line:1: BEGIN{ print "hello
awk: cmd. line:1: ^ syntax error
$ awk 'BEGIN{ print "'"$x"'" }'
hello world
$ x="hello
world"
$ awk 'BEGIN{ print "'"$x"'" }'
awk: cmd. line:1: BEGIN{ print "hello
awk: cmd. line:1: ^ unterminated string
awk: cmd. line:1: BEGIN{ print "hello
awk: cmd. line:1: ^ syntax error
assuming it does what you want would be written as:
假设它做你想要的会被写成:
$ x="hello world"
$ awk -v x="$x" 'BEGIN{ print x }'
hello world
$ x="hello
world"
$ awk -v x="$x" 'BEGIN{ print x }'
hello
world
wrt why use awk variables instead of letting shell variables expand to become part of the body of a shell script, the answer is robustness and simplicity.
关于为什么使用 awk 变量而不是让 shell 变量扩展成为 shell 脚本主体的一部分,答案是健壮性和简单性。
This is letting a shell variable expand to become part of the body of an awk script:
这是让 shell 变量扩展成为 awk 脚本主体的一部分:
cmd = "date +%a -d \"" "\""
cmd | getline
close(cmd)
and this is using an awk variable initialized with the value of a shell variable:
这是使用一个用 shell 变量的值初始化的 awk 变量:
"date +%a -d \"" "\"" | getline
close("date +%a -d \"" $l "\"")
See the difference?
看到不同?
As for why store the command in a variable - because you have to close it after you use it and it must be spelled exactly the same way in the close command as it was when you opened the pipe. Compare:
至于为什么将命令存储在变量中 - 因为您必须在使用它后关闭它,并且它必须在关闭命令中以与打开管道时完全相同的方式拼写。相比:
>backDATE=150000;
> for x in $listOFsites; do
> for y in Sun Mon Tue Wed Thu Fri Sat; do
> totalHOURS=$( awk 'BEGIN{FS="__"} NF == 10 && >= "'$backDATE'" && == "'$x'" && == "CLOSED" {while ( ( "date +%a -d \"""\"" | getline newDAY) > 0 ){if (newDAY == "'$y'") print " "}}' /home/absolute/path/* | xargs -I % /home/custom/duration/calc % | paste -sd+ | bc ); printf ".";
> done
> done
vs:
对比:
##代码##and take an extremely close second look to spot the bug in the 2nd version.
并采取极其密切的第二升OOK被发现的bug在第2个版本。
回答by Mercutio
Ok, so I ended up using this:
好的,所以我最终使用了这个:
##代码##I had to use date inside the single quotes (so that I could pass $1 to it) rather than outside (using -F"__" -v newDAY=...), but inside the single quotes getting the output of system() is problematic. After seeing:How can I pass variables from awk to a shell command?I finally saw the while (cmd | get line x) format which was the crux of my issue. Props to Ed Morton
我不得不在单引号内使用日期(以便我可以将 $1 传递给它)而不是在外(使用 -F"__" -v newDAY=...),但在单引号内获取 system() 的输出是有问题的。看到后:如何将变量从 awk 传递给 shell 命令?我终于看到了 while (cmd | get line x) 格式,这是我问题的症结所在。艾德·莫顿的道具