在 <other-keyword> 之后找到以 <keyword> 开头的第一行

Phi*_*ath 3 grep cygwin search

让我们提出我的问题以更好地解释。我正在使用 cygwin,安装基于 setup.ini,格式如下:

    @ package-name
    sdesc: "short description, on one line"
    ldesc: "long description of arbitrary length, commonly multiple lines"
    category: categories in which the packege belonges, one line
    requires: packages (libs etc) required by this package, one line
Run Code Online (Sandbox Code Playgroud)

然后是以下包,等等。

我需要的是,给定一个包名输出该包所需的所有包(如果可能,不带“requires”前缀)。

我确定这是基本的 grep,但我是新手。谢谢。

Ham*_*han 5

我不确定您将如何使用 grep 来执行此操作,但是对于此类任务,我更喜欢 awk。它可以更好地控制我想做的事情。虽然我不是 awk 的专家并且仍在学习,但这是我实现这一目标的方法。

PKGNAM="package-name"; awk "/$PKGNAM\$/,/requires:/ { if ( \$0 ~ /requires:/ ) { sub( /^requires:.?/, \"\"  ); print } }"
Run Code Online (Sandbox Code Playgroud)

更新:更新了示例 awk 命令,现在它使用 PKGNAM 变量来匹配 pacakge 名称。

在此处输入图片说明

哈。