-3

以下のようなファイルがあります。以下のテキスト ファイルから特定の文字列にアクセスする必要があります。

**  The gSOAP code generator for C and C++, soapcpp2 release 2.8.12
**  Copyright (C) 2000-2012, Robert van Engelen, Genivia Inc.
**  All Rights Reserved. This product is provided "as is", without any warranty.
**  The soapcpp2 tool is released under one of the following two licenses:
**  GPL or the commercial license by Genivia Inc.

上記のテキスト ファイルから番号 2.8.12 を取得するためにシェル スクリプトを使用するにはどうすればよいですか。

4

1 に答える 1

1

これがAWKのプログラムです。これをテキストファイルに入れて、テキストファイルに実行権限を設定し、ファイルからの入力で実行します。

#!/usr/bin/awk -f

/gSOAP code generator/ {
    LAST = NF
    P1 = LAST - 1
    P2 = LAST - 2

    if ($P2 == "soapcpp2" && $P1 == "release")
        print $LAST
    }

最近は Python の方が好きなので、Python ソリューションもここにあります。

#!/usr/bin/python

import sys

for line in sys.stdin:
    if "gSOAP code generator" in line:
        lst = line.split()
        if lst[-3] == "soapcpp2" and lst[-2] == "release":
            print(lst[-1])
            break

いずれかのプログラムを「foo」というファイルに入れて保存すると、次のことができます。

# chmod +x ./foo
# ./foo < file_to_search
于 2013-01-21T09:23:22.017 に答える