【问题标题】:search for integer (version number) in file using script [closed]使用脚本在文件中搜索整数(版本号)[关闭]
【发布时间】:2013-01-21 07:27:22
【问题描述】:

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

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

【问题讨论】:

标签: c linux shell scripting


【解决方案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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-02
    • 2015-10-13
    • 1970-01-01
    • 2014-03-18
    • 1970-01-01
    • 2022-08-11
    • 1970-01-01
    • 2012-08-31
    相关资源
    最近更新 更多