2013-01-18 56 views
0

我嘗試從文件中提取特定模式。文件內容:從shell腳本中的文件中提取特定模式

--------some other command in here---------- 
<tr> 
<td>someWords</td> 
<td>anotherWords</td> 
</tr> 
--------some other command in here----------- 
the <tr>...</tr> patter occur again 

我嘗試從shell腳本文件

提取TR模式我寫:

讀取輸入

grep '<tr>\n<td>[A-Za-z0-9,<,>,/]</td>\n<td>[A-Za-z0-9,<,>,/]</td>\n</tr> 

但是,此命令犯規工作。任何想法? THX

回答

0
sed -n '/<tr>/,/<\/tr>/p' file 

見例如:

kent$ cat a      
--------some other command in here---------- 
<tr> 
<td>someWords</td> 
<td>anotherWords</td> 
</tr> 
--------some other command in here----------- 
the 
<tr> 
<td>someOtherWords</td> 
<td>anotherWords2</td> 
</tr> 

kent$ sed -n '/<tr>/,/<\/tr>/p' a 
<tr> 
<td>someWords</td> 
<td>anotherWords</td> 
</tr> 
<tr> 
<td>someOtherWords</td> 
<td>anotherWords2</td> 
</tr> 
+0

它因此未工作。我在我的外殼嘗試了它。因爲我使用的是標準輸入,所以我只寫sed -n'/ /,/ <\/tr>/p'然而,沒有任何變化。 – user1988385

+0

@ user1988385你是否喜歡'我的標準輸入命令| sed ...'它應該工作。 – Kent

+0

w/my shell T.T有問題,我在終端上試了一下(使用sed -n'/ /,/ <\/tr>/p'一個命令),它完美地工作。但是,由於某種原因,我無法在shell中執行它 – user1988385

相關問題