使用脚本在文件中搜索整数(版本号)

时间:2013-01-21 07:27:22

标签: c linux shell scripting

我有一个下面给出的文件,我需要从下面给出的文本文件中访问一个特定的字符串。

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

如何使用shell脚本从上面的文本文件

获取数字2.8.12

1 个答案:

答案 0 :(得分: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