我有类似的东西:
------------------------------------------------------------------------
r2 | username | 2011-01-16 16:52:23 +0100 (Sun, 16 Jan 2011) | 1 line
Changed paths:
D /foo
Removed foo
------------------------------------------------------------------------
r1 | username | 2011-01-16 16:51:03 +0100 (Sun, 16 Jan 2011) | 1 line
Changed paths:
A /foo
created foo
------------------------------------------------------------------------
我的目标是识别特定日期“用户名”添加的文件。因此,我需要组合(用户名,2011年1月16日,A)以确保它是正确的文件然后打印foo。 我的想法是:
但问题是我无法取代-------因为它们与其他角色混合在一起。
----------------------------------------------------------------------
|r2|username|2011-01-1616:52:23+0100(Sun,16Jan2011)|1line|Changedpaths:|D/foo|Removedfoo|
------------------------------------------------------------------------
|r1|username|2011-01-1616:51:03+0100(Sun,16Jan2011)|1line|Changedpaths:|A/foo|createdfoo|
------------------------------------------------------------------------
所以我认为从一个像|||这样的特殊字符替换---------------开始是个好主意然后使用awk FS = |||通过换行符更改此字符OFS = \ n 谁能帮我! 感谢
答案 0 :(得分:1)
gawk 'BEGIN{FS="\n";RS="--+"} {$1=$1}RT' OFS="|" file
答案 1 :(得分:0)
awk '/^-+$/{print a[i++];next}!/^-+$/{gsub(/ /,"");a[i]=a[i] "|" $0}' infile
$ awk '/^-+$/{print a[i++];next}!/^-+$/{gsub(/ /,"");a[i]=a[i] "|" $0}' ./infile
|r2|username|2011-01-1616:52:23+0100(Sun,16Jan2011)|1line|Changedpaths:|D/foo|Removedfoo
|r1|username|2011-01-1616:51:03+0100(Sun,16Jan2011)|1line|Changedpaths:|A/foo|createdfoo
答案 2 :(得分:0)
以下内容,
#! /bin/sh
if [ "$#" != '3' ] ; then
echo "usage $0 logfile username date"
exit 1
fi
cat "$1" | awk '
BEGIN{
FS="|";
}
/------------------------------------------------------------------------/{
username="";
date="";
next;
}
/^r[0-9]+/{
username = gensub(/^ *(.*[^ ]) *$/, "\\1", "", $2);
date = gensub(/^ *(.*[^ ]) *$/, "\\1", "", $3);
next;
}
/^created /{
filename = gensub(/^created /, "", "", $0);
if ( username == "'"$2"'" && date == "'"$3"'" ) {
print filename;
}
}
'
如果对输入数据执行以下操作,
$ ./script data username '2011-01-16 16:51:03 +0100 (Sun, 16 Jan 2011)'
输出
foo
希望有所帮助, - 卢克