简体   繁体   中英

parse text using grep regex pull out text from multiple lines of text in a file

I have a chunck of text in a file:

<tr bgcolor="#F9F9F9">
     <td align="left">8/7/2012 11:23:42 AM</td>
     <td align="left"><em>Here is the text I want to parse out</em></td>
     <td class="ra">9.00</td>
     <td class="ra">297.00</td>
     <td class="ra">0.00</td>
     <td class="ra">0.00</td>
     <td class="ra">$0.00</td>
     <td class="ra">$0.50</td>
     <td class="ra"></td>
 </tr>

using grep I would like to end up with the result being

Here is the text I want to parse out

Working on the code now I have

cat file.txt | grep -m 1 -oP '<em>[^</em>]*'

but that does not work... thanks for your help!

A correct regex would be (?<=<em>).*?(?=</em>) .

So, try:

grep -m 1 -oP '(?<=<em>).*?(?=</em>)' file.txt

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM