469,640 Members | 1,563 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,640 developers. It's quick & easy.

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

Tarantulus
114 100+
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
Oct 6 '09 #1
3 4084
gpraghuram
1,275 Expert 1GB
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
Oct 7 '09 #2
Tarantulus
114 100+
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
??
Oct 7 '09 #3
gpraghuram
1,275 Expert 1GB
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
Oct 9 '09 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

8 posts views Thread by uc_sk | last post: by
2 posts views Thread by Jürgen Holly | last post: by
5 posts views Thread by Andrei Pociu | last post: by
16 posts views Thread by Martin Jørgensen | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.