1. 程式人生 > >只輸出匹配的模式--正則表示式的一個應用

只輸出匹配的模式--正則表示式的一個應用

先看需要匹配的檔案需求:

如下為防火牆日誌,其中有欄位service=http proto=6,如何輸出service=http這個欄位呢,service欄位有可能包含好幾個空格,並且不確定究竟會有幾個,但是後面的欄位肯定是proto,如何用awk模式匹配輸出service這個欄位呢?

[[email protected] test]$ more sedonly.txt 
2011-09-30 00:00:20     Local0.Notice   10.2.0.254      ns50: NetScreen device_id=0019022004000299  [Root]system-notification-00257(traffic): start_time="2011-09-30 00:01:05" duration=15 polic
y_id=103 <span style="font-size:18px;">service=http proto=6</span> src zone=Trust dst zone=Untrust action=Permit sent=2683 rcvd=766 src=10.100.1.43 dst=119.188.11.3 src_port=4048 dst_port=80 src-xlated ip=218.206.244.202 port=467
9 dst-xlated ip=119.188.11.3 port=80 session_id=61727 reason=Close - AGE OUT<000>
 2011-09-30 00:00:20     Local0.Notice   10.2.0.254      ns50: NetScreen device_id=0019022004000299  [Root]system-notification-00257(traffic): start_time="2011-09-30 00:01:05" duration=15 poli
cy_id=103 <span style="font-size:18px;">service=NETBIOS (NS) proto=17 </span>src zone=Trust dst zone=Untrust action=Permit sent=2674 rcvd=766 src=10.100.1.43 dst=119.188.11.3 src_port=4045 dst_port=137 src-xlated ip=218.206.244.2
02 port=15311 dst-xlated ip=119.188.11.3 port=137 session_id=62271 reason=Close - AGE OUT<000>
 2011-09-30 00:00:20     Local0.Notice   10.2.0.254      ns50: NetScreen device_id=0019022004000299  [Root]system-notification-00257(traffic): start_time="2011-09-30 00:01:05" duration=15 poli
cy_id=103 <span style="font-size:18px;">service=VDO Live (tcp) proto=6</span> src zone=Trust dst zone=Untrust action=Permit sent=2645 rcvd=766 src=10.100.1.43 dst=119.188.11.3 src_port=4044 dst_port=7001 src-xlated ip=218.206.244
.202 port=14295 dst-xlated ip=119.188.11.3 port=7001 session_id=59240 reason=Close - AGE OUT<000>
[
[email protected]
test]$

--解決方法

[[email protected] test]$ grep -Po 'service=.*(?= proto=)' sedonly.txt 
service=http
service=NETBIOS (NS)
service=VDO Live (tcp)
[[email protected] test]$ sed -s 's/^.*\(service=.*\) proto=.*$/\1/' sedonly.txt 
service=http
service=NETBIOS (NS)
service=VDO Live (tcp)
[
[email protected]
test]$ awk -F 'proto|service' '{print "service"$2}' sedonly.txt service=http service=NETBIOS (NS) service=VDO Live (tcp) [[email protected] test]$

一下是我的一個需求

檔案類似於下面這樣,只取了前面一點

[[email protected] findjob]$ more alljob.xml 
<?xml version="1.0" encoding="utf-8"?><FindQuerySessionAsyncStateSerialiser xmlns:ibm="http://www.ibm.com/" clientInstallPath_="D:\IBM_IIS\Clients\Classic" generatedDate_="2014年5月29日" gener
atedTime_="11:06:48" serverName_="DATASTAGE" serverVersion_="8.7"><criteria_><caseInsensitive_>1</caseInsensitive_><createdAfter_ /><createdBefore_ /><createdByUser_ /><DependsOnObjects /><des
cription_ /><findWithinLastResultSet_>0</findWithinLastResultSet_><lastModifiedAfter_ /><lastModifiedBefore_ /><lastModifiedByUser_ /><name_>*</name_><nameDescriptionMatchMode_>NameOrDescripti
on</nameDescriptionMatchMode_><repositoryName_>lscrm</repositoryName_><folder_>\</folder_><Types><string>Parallel Jobs</string></Types><WhereUsedObjects /></criteria_><Results><ReposObjectSeri
aliser><className_>CJobDefn</className_><displayName_>CT_ENT_DIST_MAXLNBAL</displayName_><folderPath_>\Jobs\CRM_03_ENT\CRM_0303_ENT_CT\CRM_030303_ENT_CT_DIST</folderPath_><isTopLevel_>1</isTop
Level_><id_>CT_ENT_DIST_MAXLNBAL</id_><platformType_ /><reposID_>c2e76d84.43058877.2174cfdoj.l4f87r0.76hjj8.unm720lidv156as11jdb5</reposID_><reposManagerID_>DATASTAGE:lscrm</reposManagerID_><s
ubType_>3</subType_><typeDefinitionDisplayName_>Parallel Job</typeDefinitionDisplayName_></ReposObjectSerialiser><ReposObjectSerialiser><className_>CJobDefn</className_><displayName_>CopyOfIFS
I_CURTRAN</displayName_><folderPath_>\作業\0001_ODS\00011_ODS_賬戶資訊\00012_ODS_賬戶交易資訊</folderPath_><isTopLevel_>1</isTopLevel_><id_>CopyOfIFSI_CURTRAN</id_><platformType_ /><reposID_>c
2e76d84.43058877.2174ce5cg.e9a93n8.dq7mt3.rilur196dttfpvk1ipaj6</reposID_><reposManagerID_>DATASTAGE:lscrm</reposManagerID_><subType_>3</subType_><typeDefinitionDisplayName_>Parallel Job</type
DefinitionDisplayName_></ReposObjectSerialiser><ReposObjectSerialiser><className_>CJobDefn</className_><displayName_>CopyOfIFSI_DEPTRAN</displayName_><folderPath_>\作業\0001_ODS\00011_ODS_賬戶
資訊\00012_ODS_賬戶交易資訊</folderPath_><isTopLevel_>1</isTopLevel_><id_>CopyOfIFSI_DEPTRAN</id_><platformType_ /><reposID_>c2e76d84.43058877.2174cesld.fcdckp0.c4dm26.ogq04coo9cs4681ed5me0</r
eposID_><reposManagerID_>DATASTAGE:lscrm</reposManagerID_><subType_>3</subType_><typeDefinitionDisplayName_>Parallel Job</typeDefinitionDisplayName_></ReposObjectSerialiser><ReposObjectSeriali
ser><className_>CJobDefn</className_><displayName_>IFSI_CARDTRAN</displayName_><folderPath_>\作業\0001_ODS\00011_ODS_賬戶資訊\00012_ODS_賬戶交易資訊</folderPath_><isTopLevel_>1</isTopLevel_><i
d_>IFSI_CARDTRAN</id_><platformType_ /><reposID_>c2e76d84.43058877.2174b296p.aipqg68.3gs1oe.6id3oi6ifaunehhjd59tl</reposID_><reposManagerID_>DATASTAGE:lscrm</reposManagerID_><subType_>3</subTy
pe_><typeDefinitionDisplayName_>Parallel Job</typeDefinitionDisplayName_></ReposObjectSerialiser><ReposObjectSerialiser><className_>CJobDefn</className_><displayName_>IFSI_CURTRAN</displayName
_><folderPath_>\作業\0001_ODS\00011_ODS_賬戶資訊\00012_ODS_賬戶交易資訊</folderPath_><isTopLevel_>1</isTopLevel_><id_>IFSI_CURTRAN</id_><platformType_ /><reposID_>c2e76d84.43058877.2174b2970.2
r9jn6g.cqvmdf.r4521aevg2eh084hd8pgv</reposID_><reposManagerID_>DATASTAGE:lscrm</reposManagerID_><subType_>3</subType_><typeDefinitionDisplayName_>Parallel Job</typeDefinitionDisplayName_></Rep
osObjectSerialiser><ReposObjectSerialiser><className_>CJobDefn</className_><displayName_>IFSI_DEPTRAN</displayName_><folderPath_>\作業\0001_ODS\00011_ODS_賬戶資訊\00012_ODS_賬戶交易資訊</folde
rPath_><isTopLevel_>1</isTopLevel_><id_>IFSI_DEPTRAN</id_><platformType_ /><reposID_>c2e76d84.43058877.2174b2975.fj9jtmg.e8e747.3j81nbfj2eob0vlonomg5</reposID_><reposManagerID_>DATASTAGE:lscrm
</reposManagerID_><subType_>3</subType_><typeDefinitionDisplayName_>Parallel Job</typeDefinitionDisplayName_></ReposObjectSerialiser><ReposObjectSerialiser><className_>CJobDefn</className_><di
splayName_>IFSI_INTBANKTRAN</displayName_><folderPath_>\作業\0001_ODS\00011_ODS_賬戶資訊\00012_ODS_賬戶交易資訊</folderPath_><isTopLevel_>1</isTopLevel_><id_>IFSI_INTBANKTRAN</id_><platformTyp
e_ /><reposID_>c2e76d84.43058877.2174b2979.5tf23gg.4f527i.niesna07c63s112uhkt15</reposID_><reposManagerID_>DATASTAGE:lscrm</reposManagerID_><subType_>3</subType_><typeDefinitionDisplayName_>Pa
rallel Job</typeDefinitionDisplayName_></ReposObjectSerialiser><ReposObjectSerialiser><className_>CJobDefn</className_><displayName_>IFSI_INTBANKTRAN_PAYFEE</displayName_><folderPath_>\作業\00
01_ODS\00011_ODS_賬戶資訊\00012_ODS_賬戶交易資訊</folderPath_><isTopLevel_>1</isTopLevel_><id_>IFSI_INTBANKTRAN_PAYFEE</id_><platformType_ /><reposID_>c2e76d84.43058877.2174b2979.sng5q9g.67533

我要取<id_>IFSI_INTBANKTRAN_PAYFEE</id_>裡面的資訊,在檔案中大概有兩百個

我的處理方法

[[email protected] findjob]$ sed -s 's/^.*\(<id_>.*<\/id_>\).*$/\1/g' alljob.xml
<id_>REPORT51_SCORE_CONVERGIFT</id_>
[[email protected] findjob]$

只取到一個

--

[[email protected] findjob]$ awk -F '<id_>|<\/id_>' '{print $2}' alljob.xml
awk: 警告: 轉義序列“\/”被當作單純的“/”
CT_ENT_DIST_MAXLNBAL
[[email protected] findjob]$ awk -F '<id_>|</id_>' '{print $2}' alljob.xml
CT_ENT_DIST_MAXLNBAL
[[email protected] findjob]$

----

[[email protected] findjob]$ sed -s 's/^.*<id_>\(.*\)<\/id_>.*$/\1/g' alljob.xml
REPORT51_SCORE_CONVERGIFT
[[email protected] findjob]$

還是值取到一個

why??????????????

--我現在改一下檔案的樣式成標準XML

[[email protected] findjob]$ more all.xml 
<?xml version="1.0" encoding="utf-8"?>
<FindQuerySessionAsyncStateSerialiser xmlns:ibm="http://www.ibm.com/" clientInstallPath_="D:\IBM_IIS\Clients\Classic" generatedDate_="2014年5月29日" generatedTime_="11:06:48" serverName_="DATA
STAGE" serverVersion_="8.7">
<criteria_>
<caseInsensitive_>1</caseInsensitive_>
<createdAfter_ />
<createdBefore_ />
<createdByUser_ />
<DependsOnObjects />
<description_ />
<findWithinLastResultSet_>0</findWithinLastResultSet_>
<lastModifiedAfter_ />
<lastModifiedBefore_ />
<lastModifiedByUser_ />
<name_>*</name_>
<nameDescriptionMatchMode_>NameOrDescription</nameDescriptionMatchMode_>
<repositoryName_>lscrm</repositoryName_>
<folder_>\</folder_>
<Types>
<string>Parallel Jobs</string>
</Types>
<WhereUsedObjects />
</criteria_>
<Results>
<ReposObjectSerialiser>
<className_>CJobDefn</className_>
<displayName_>CT_ENT_DIST_MAXLNBAL</displayName_>
<folderPath_>\Jobs\CRM_03_ENT\CRM_0303_ENT_CT\CRM_030303_ENT_CT_DIST</folderPath_>
<isTopLevel_>1</isTopLevel_>
<id_>CT_ENT_DIST_MAXLNBAL</id_>
<platformType_ />
<reposID_>c2e76d84.43058877.2174cfdoj.l4f87r0.76hjj8.unm720lidv156as11jdb5</reposID_>
<reposManagerID_>DATASTAGE:lscrm</reposManagerID_>
<subType_>3</subType_>
<typeDefinitionDisplayName_>Parallel Job</typeDefinitionDisplayName_>
</ReposObjectSerialiser>
<ReposObjectSerialiser>

使用命令

awk -F '<id_>|<\/id_>' '{print $2}' all.xml

每個隔了很多空格,把空格去掉

awk -F '<id_>|</id_>' '{print $2}' all.xml |sed  '/^$/d'

OK

--

[[email protected] findjob]$ sed -n 's/<id_>\(.*\)<\/id_>/\1/p' all.xml |wc -l
290
[[email protected] findjob]$

注意:

不加-n 和 p的話,每行朝陽輸出,匹配的行被替換

只加-n的話,無輸出

只有加上-n和p,才打印了我想要的!!

--

grep -Po '<id_>.*<\/id_>' all.xml

列印如下

<id_>SCORE_PLAN_ZB</id_>
<id_>SPECIAL_SHOP</id_>
<id_>REPORT01_SCORE_MSOURCE</id_>
<id_>REPORT02_SCORE_QSOURCE</id_>
<id_>REPORT03_SCORE_YSOURCE</id_>
<id_>REPORT11_SCORE_MCARDORG</id_>
<id_>REPORT12_SCORE_QCARDORG</id_>
<id_>REPORT13_SCORE_YCARDORG</id_>
<id_>REPORT21_SCORE_MCUSTORG</id_>
<id_>REPORT22_SCORE_QCUSTORG</id_>
<id_>REPORT23_SCORE_YCUSTORG</id_>
<id_>REPORT41_SCORE_PART</id_>
<id_>REPORT51_SCORE_CONVERGIFT</id_>

修改如下

[[email protected] findjob]$ grep -Po '<id_>.*<\/id_>' all.xml |sed 's/<id_>//'|sed 's/</id_>//'
sed:-e 表示式 #1,字元 10:“s”的未知選項
[[email protected] findjob]$ grep -Po '<id_>.*<\/id_>' all.xml |sed 's/<id_>//'|sed 's/<\/id_>//'
CT_ENT_DIST_MAXLNBAL
CopyOfIFSI_CURTRAN
CopyOfIFSI_DEPTRAN
IFSI_CARDTRAN
IFSI_CURTRAN
IFSI_DEPTRAN



相關推薦

輸出匹配模式--表示式一個應用

先看需要匹配的檔案需求: 如下為防火牆日誌,其中有欄位service=http proto=6,如何輸出service=http這個欄位呢,service欄位有可能包含好幾個空格,並且不確定究竟會有幾個,但是後面的欄位肯定是proto,如何用awk模式匹配輸出service

Python學習筆記模式匹配表示式之使用和不使用表示式

 隨筆記錄方便自己和同路人查閱。 #------------------------------------------------我是可恥的分割線-------------------------------------------   假設你希望在字串中查詢電話號碼。你知道模式:3個數字,一

模式匹配表示式

一、正則表示式基礎 1、regex:是一種文字模式的描述方法 2、re.compile()向它傳入一個字串值,表示正則表示式,它將返回一個Regex模式物件(簡稱為Regex物件:正則物件) 3、Regex物件的search()方法查詢傳入的字串,尋找正則表示式的所有匹

第7章 模式匹配表示式

python 中所有的正則表示式函式都在re模組中。 向re.complie()傳入一個字串值,來表示正則表示式,它將返回一個Regex模式物件。 Regex 物件的serch()方法查詢傳入的字串,尋找該正則表示式的所有匹配。如果字串中沒有找到該正則表示

Scala基礎教程(八):模式匹配表示式

匹配使用case 類: case classes是用於模式匹配與case 表示式指定類。這些都是標準類具有特殊修飾:case。下面是一個簡單的模式使用case class匹配示例: object Test {    def main(args: Array[String])

mysql jdbc url模式 表示式

https://blog.csdn.net/youcijibi/article/details/77965487?locationNum=7&fps=1 jdbc:mysql://[host][,failoverhost...][:port]/[database] » [?propertyName1

表示式--相關應用

最近有很多朋友在問關於正則表示式的一些知識,所以在這裡統一給大家回覆一下,為了直接方便就忍了,沒有截圖,視覺效果不完美的話還請大家海涵! 正則表示式 (1)使用者名稱正則 //使用者名稱正則,4到16位(字母,數字,下劃線,減號) var uPattern = /^[a

iOS表示式簡單應用

想要了解基本語法的可以進入下面網站自行學習 自學地址Runoob 正則表示式在iOS的簡單應用 // 匹配小寫a開頭的字串 NSString * regex = @"^a.*"; NSPredicate *pred = [NSPredicate predicateWithForm

表示式基礎應用(替換replaceAll(regex,str))

1. 基礎字元替換 要求: 將字串中的數字串替換成* String e = "asdlfj328238asldfjlsfd32323sdfl";String reg5 ="\\d+";String newStr = e.replaceAll(reg5, "*");Syst

Split表示式應用

今天,別人提問日期格式不規範,格式各樣,中間的內容都不同,怎麼處理方便。 想了想還是用Split的正則表示式處理方便且易於擴充,只要正則表示式不斷擴充就行了。 程式碼如下: public static void main(String[] args) { String[

Android 表示式應用

一、 正則表示式 (一)、概念:         正則表示式(regular expression)就是由普通字元(例如a到z)以及特殊字元(稱為元字元)組成的一種字串匹配的模式,可以用來檢查一個串是否含有某種子

JAVA中(分組)表示式應用

分組表示式的應用 分組語法 (?\exp) 匹配exp,並捕獲文字到名稱為name的組裡,也可以寫成(?’name’exp) demo public static void main(String[] args){

python 3.6 表示式應用(附例項程式碼)

python 3.6 正則表示式的應用 正則表示式用於搜尋、替換和解析字串。正則表示式遵循一定的語法規則,使用非常靈活,功能強大。使用正則表示式編寫一些邏輯驗證非常方便,例如電子郵件地址格式的驗證。Python提供了re模組實現正則表示式的驗證。 正則表

Python 表示式替換應用 (用文字中一部分替換另一部分)

import re fobj = open('bws.html', 'r') subject = fobj.readlines() fobj.close() all = [] fobj = open('bws.html.new', 'w') for eachLine i

請實現一個函式用來匹配包括'.'和'*'的正則表示式模式中的字元'.'表示任意一個字元,而'*'表示它前面的字元可以出現任意次(包含0次)。 在本題中,匹配是指字串的所有字元匹配整個模式

題目描述 請實現一個函式用來匹配包括'.'和'*'的正則表示式。模式中的字元'.'表示任意一個字元,而'*'表示它前面的字元可以出現任意次(包含0次)。 在本題中,匹配是指字串的所有字元匹配整個模式。例如,字串"aaa"與模式"a.a"和"ab*ac*a"匹配,但是與"aa.a"和"ab*a"均

LeeCode 10: 給定一個字串 (s) 和一個字元模式 (p)。實現支援 '.' 和 '*' 的表示式匹配

題目 給定一個字串 (s) 和一個字元模式 (p)。實現支援 '.' 和 '*' 的正則表示式匹配。 '.' 匹配任意單個字元。 '*' 匹配零個或多個前面的元素。 匹配應該覆蓋整個字串 (s)

請實現一個函式用來匹配包括'.'和'*'的正則表示式模式中的字元'.'表示任意一個字元,而'*'表示它前面的字元可以出現任意次(包含0次)。 在本題中,匹配是指字串的所有字元匹配整個模式。例如,字

本題源自劍指offer ----------------------------------------------------------------------------------- 如果模式串遇到*字元則有兩種狀態,第一匹配字元,則字串後移一位,模式串不變,第二,

PHP 表示式---匹配模式

1、PHP 正則表示式 <一>正則表示式貪婪匹配,非貪婪匹配。 預設是貪婪匹配, 例如 ①、貪婪匹配, $str = '香腸月餅'; preg_match('/(.)</td>/',$str,$rs); print_r($rs); ②、 $str = '香腸月餅'; preg_