2013-03-06 62 views
1

輸入的基礎上,一個文件 - FILE1.TXT
awk |合併連續3行中字段匹配

abc message=sent session:111,x,y,z 
pqr message=receive session:111,4,5,7 
abc message=sent session:123,x,y,z 
pqr message=receive session:123,4,5,7 
pqr message=receive session:123,4x,5x,7x 
pqr message=receive session:123,4p,5q,7r 
abc message=sent session:342,x,y,z 
abc message=receive session:311,x,y,z 
abc message=sent session:589,x,y,z 
pqr message=receive session:589,4,5,7 

輸出

abc message=sent session:111,x,y,z,pqr message=receive session:111,4,5,7 
abc message=sent session:123,x,y,z,pqr message=receive session:123,4,5,7,pqr message=receive session:123,4x,5x,7x,pqr message=receive session:123,4p,5q,7r 
abc message=sent session:342,x,y,z,NOMATCH 
abc message=receive session:311,x,y,z,NOMATCH 
abc message=sent session:589,x,y,pqr message=receive session:589,4,5,7 

注:

  1. 查找已發送並立即收到 - 僅限匹配會話號碼
  2. 將所有會話匹配「receive」與相同的「sent」組合在一起。例如:會議:123
  3. 如果沒有 「接收」,然後打印NOMATCH
+0

這難道不是同樣的問題你之前問過的? http://stackoverflow.com/questions/14847251/awk-merge-line-on-the-basis-of-field-matching – Guru 2013-03-06 06:07:59

+0

沒有..這次我們試圖合併匹配標準的所有行..以前我們是僅合併2行。我知道它的相同,但我試圖修改以前的帖子,但沒有成功。 – 2013-03-06 06:09:24

+0

@VipinChoudhary,你爲什麼在第一次合併時刪除了'pqr',而不是其他人? – perreal 2013-03-06 06:15:03

回答

2

試試這個:

awk -F "[:,]" '{split($2,a,",");if($1~/sent/){if(prev!~/receive/&& NR!=1)print prev,"NOMATCH";else if (NR!=1)print prev;prev=$0;session=a[1];}if($1~/receive/&&session==a[1]){prev=prev","$0;}}'END'{if(prev~/receive/)print prev;}' file1.txt 
+0

謝謝Gopal ..但輸出不同..最後一個記錄有點錯過了 – 2013-03-06 05:52:29

1
awk '{x=substr($3,0,match($3,","));a[x]?a[x]=a[x]","$0:a[x]=$0}END{for(i in a){print a[i],a[i]~/sent/&&a[i]~/receive/?"":"NOMATCH"}}' your_file 

測試:

>cat temp 
abc message=sent session:111,x,y,z 
pqr message=receive session:111,4,5,7 
abc message=sent session:123,x,y,z 
pqr message=receive session:123,4,5,7 
pqr message=receive session:123,4x,5x,7x 
pqr message=receive session:123,4p,5q,7r 
abc message=sent session:342,x,y,z 
abc message=receive session:311,x,y,z 
abc message=sent session:589,x,y,z 
pqr message=receive session:589,4,5,7 
> 
> 
> 
> awk '{x=substr($3,0,match($3,","));a[x]?a[x]=a[x]","$0:a[x]=$0}END{for(i in a){print a[i],a[i]~/sent/&&a[i]~/receive/?"":"NOMATCH"}}' temp 
abc message=sent session:111,x,y,z,pqr message=receive session:111,4,5,7 
abc message=sent session:123,x,y,z,pqr message=receive session:123,4,5,7,pqr message=receive session:123,4x,5x,7x,pqr message=receive session:123,4p,5q,7r 
abc message=receive session:311,x,y,z NOMATCH 
abc message=sent session:589,x,y,z,pqr message=receive session:589,4,5,7 
abc message=sent session:342,x,y,z NOMATCH 
>