2013-01-05 39 views
1

使用svn diff--summarize標誌返回類似於下面的內容。我們如何將它傳遞給sed的或grep來做到以下幾點:如何解析svn diff結果?

  1. 刪除所有以「d」(刪除文件)開始的任何行
  2. 刪除的「M」前綴,「A」或「 MM「(或任何其他情況)以及隨後的標籤。
  3. 刪除URL路徑只留下文件名/文件夾。
  4. 存儲在文件

例子:

D https://localhost/example/test1.php 
D https://localhost/example/test2.php 
M https://localhost/example/test3.php 
M https://localhost/example/test4.php 
A https://localhost/example/test5.php 
M https://localhost/example/test6.php 
A https://localhost/example/test7.php 
M https://localhost/example/test8.php 
M https://localhost/example/test9.php 
M https://localhost/example/test10.php 
A https://localhost/example/test11.php 
M https://localhost/example/test12.php 
M https://localhost/example/test13.php 
MM https://localhost/example/test.php 
M https://localhost/test0.php 

然後會變成:

/example/test3.php 
/example/test4.php 
/example/test5.php 
/example/test6.php 
/example/test7.php 
/example/test8.php 
/example/test9.php 
/example/test10.php 
/example/test11.php 
/example/test12.php 
/example/test13.php 
/example/test.php 
/test0.php 
+2

您的輸出結果與您的規格不符,它們不應包含'test1.php'或'test2.php',因爲它們以'D'開頭。 –

+0

謝謝,更新了我的示例輸出以糾正錯誤。 – atdev

回答

1

篩選與sed

$ svn diff --summarize | sed -e '/^D/d' -e 's/.*host//' 
/example/test3.php 
/example/test4.php 
/example/test5.php 
/example/test6.php 
/example/test7.php 
/example/test8.php 
/example/test9.php 
/example/test10.php 
/example/test11.php 
/example/test12.php 
/example/test13.php 
/example/test.php 
/test0.php 

# Redirect output to file 
$ svn diff --summarize | sed -e '/^D/d' -e 's/.*host//' > file 

你的東東d至pipe|svnsed的輸出。第一部分'/^D/d'刪除所有以D開頭的行,第二個s/.*host//將全部內容替換爲host而沒有任何內容,以存儲到文件使用redirect> file

類似的邏輯與grep

$ svn diff --summarize | grep '^[^D]' file | grep -Po '(?<=host).*' > file 

第一grep篩選出與D開始的行和第二個使用與positive lookahead-Po只顯示host後的線的一部分。