2013-01-21 44 views
-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. 

如何使用shell腳本,以便從上面的文本文件中獲得數2.8.12,

+0

一個簡單的正則表達式應該工作。 –

+3

[**您嘗試過什麼?**](http://whathaveyoutried.com/) –

回答

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