如何用 ||| 替换仅包含 -------- 的行

发布于 2024-10-12 19:11:57 字数 1206 浏览 1 评论 0原文

我有这样的事情:

------------------------------------------------------------------------
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。 我的想法是:

  1. 删除空格
  2. 将换行符更改为 |
  3. 摆脱 -------------- 并用换行符替换它们,

但问题是我无法替换 ------- 因为它们与其他字符混合。

----------------------------------------------------------------------
|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 谁能帮助我! 谢谢

I have something like:

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

My target is to identify the file added by the "username" in a specific date. Thus, I need to have the combination (username, 16 Jan 2011, A) to insure that it is the right file ands then print foo.
My idea is to:

  1. delete the white spaces
  2. change the newlines into |
  3. get rid of the --------------- and replace them with newlines

but the problem is that I couldn't replace the ------- since they are mixed with other characters.

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

So I thought it would be a good idea to start by replacing the --------------- by a special character like ||| and then change this character by a newline using awk FS=||| OFS=\n
Can anyone help me!
thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

空城之時有危險 2024-10-19 19:11:57
gawk 'BEGIN{FS="\n";RS="--+"} {$1=$1}RT' OFS="|"  file
gawk 'BEGIN{FS="\n";RS="--+"} {$1=$1}RT' OFS="|"  file
场罚期间 2024-10-19 19:11:57

下面怎么样,


#! /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

希望有帮助,
——卢克

How about the following,


#! /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;
    }
}
'

If the following is executed on the input data,

$ ./script data username '2011-01-16 16:51:03 +0100 (Sun, 16 Jan 2011)'

the output is

foo

Hope that helps,
- Luke

独木成林 2024-10-19 19:11:57
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
awk '/^-+$/{print a[i++];next}!/^-+$/{gsub(/ /,"");a[i]=a[i] "|" $0}' infile

Output

$ 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文