Connecting Tech Pros Worldwide Forums | Help | Site Map

Using awk/sed to find text between delimiters and outputting each result to a file

Tarantulus's Avatar
Member
 
Join Date: May 2007
Posts: 103
#1: Oct 6 '09
Hi Guys,

working on a solaris 9 box and I have an a problem I am not educated enough to solve :P

bit of a boring one, essentially I have a monthly HTML report and I want to extract the tables from it into separate files each month.

for example

Expand|Select|Wrap|Line Numbers
  1. <table>
  2. <tr>
  3. <td>
  4. data here
  5. </td>
  6. <td> more data </td>
  7. </tr>
  8. </table>
  9. <table>
  10. <tr>
  11. <td>
  12. even more data
  13. </td>
  14. </tr>
  15. </table>
  16.  
should be extracted to 2 files, one containing
Expand|Select|Wrap|Line Numbers
  1. <table>
  2. <tr>
  3. <td>
  4. data here
  5. </td>
  6. <td> more data </td>
  7. </tr>
  8. </table>
  9.  
the other containing
Expand|Select|Wrap|Line Numbers
  1. <table>
  2. <tr>
  3. <td>
  4. even more data
  5. </td>
  6. </tr>
  7. </table>
  8.  
(assuming the number of tables is variable)

I've been struggling with this for a week, but I just can't get the output to multiple files.

am I asking the impossible?

thanks in advance

gpraghuram's Avatar
Expert
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,258
#2: Oct 7 '09

re: Using awk/sed to find text between delimiters and outputting each result to a file


You can do this using awk.
Open the file in awk
Start reading the file When you encounter the first <table> strta collecting or append that to a string.
Do this till you hit the </table>

Do the same for the next <table></table> operation.

Th catch is dont rely on awk to parse the file and u read the file in the awk code.That is dont rely on $1 $2 etc giiven by awk.


Raghu
Tarantulus's Avatar
Member
 
Join Date: May 2007
Posts: 103
#3: Oct 7 '09

re: Using awk/sed to find text between delimiters and outputting each result to a file


Thanks Raghu,

But I think you misunderstand me, I understand the logic of it, but not the practicality, my AWK knowledge is limited. are you suggesting some sort of loop?

like
Expand|Select|Wrap|Line Numbers
  1. for i in text 
  2. do 
  3. awk something
  4. done
??
gpraghuram's Avatar
Expert
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,258
#4: Oct 9 '09

re: Using awk/sed to find text between delimiters and outputting each result to a file


The code wuld be like this
Expand|Select|Wrap|Line Numbers
  1. BEGIN{
  2. }
  3. {
  4. while (getline < IP_FILE > 0)
  5. {
  6. #Your logic shuld be here.
  7. }
  8. }
  9. END{
  10. }
Raghu
Reply