给出以下记录
01-01-2012 18:02 some data 01-11-2014 20:22 some other data 10-02-2014 14:00 more data still
我正在尝试对日期,时间和数据进行分组,并将它们打印在不同的行上,如下所示:
01-01-2012 18:02 some data
01-11-2014 20:22 some other data
10-02-2014 14:00 more data still
然而,到目前为止我所拥有的:
echo '01-01-2012 18:02 some data 01-11-2014 20:22 some other data 10-02-2014 14:00 more data still' | awk -F '[0-9]*-[0-9]*-[0-9]* [0-9]*:[0-9]*' '{ for ( n=1; n<=NF; n++ ) print $n }
结果如下:
some data
some other data
more data still
缺少日期和时间。它们是字段分隔符,因此它们不会打印。
如何修改我的awk脚本以打印与正则表达式匹配的每个字段分隔符?
答案 0 :(得分:1)
使用gnu awk:
awk -v RS='[0-9]+-[0-9]+-[0-9]+ [0-9]+:[0-9]+' '!NF{s=RT;next} {print s $0}' file
01-01-2012 18:02 some data
01-01-2012 18:02 some other data
01-01-2012 18:02 more data still
编辑:您可以使用非gnu awk:
awk '{gsub(/[[:blank:]]+[0-9]+-[0-9]+-[0-9]+ [0-9]+:[0-9]+/, "\n&");
gsub(/\n[[:blank:]]+/, "\n")} 1' file
01-01-2012 18:02 some data
01-11-2014 20:22 some other data
10-02-2014 14:00 more data still
还可以使用grep -P
:
grep -oP '[0-9]+-[0-9]+-[0-9]+ [0-9]+:[0-9]+.+?(?=[0-9]+-[0-9]+-[0-9]+|$)' file
01-01-2012 18:02 some data
01-11-2014 20:22 some other data
10-02-2014 14:00 more data still
答案 1 :(得分:1)
awk way
awk '{for(i=2;i<=NF;i++)if($i~/[0-9]+-[0-9]+-[0-9]+/)$i="\n"$i}1' file
答案 2 :(得分:1)
by awk
awk '{for (i=1;i<=NF;i++) printf ($i~/-..-/)?RS $i:FS $i}' infile
for loop
:逐个读取元素,元素按空格分割。printf
:打印元素而不返回printf ($i~/-..-/)?RS $i:FS $i
- 可以用于if-else语句:
if ($i~/-..-/) {print RS $i) else (print FS $i)