The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How do I extract text only from html file without HTML tag los111 UNIX for Dummies Questions & Answers 4 11-28-2007 03:40 AM
Searching for text in a Space delimited File andyblaylock UNIX for Dummies Questions & Answers 6 11-27-2007 06:33 PM
coverting html data to text in 'c' phani_sree High Level Programming 3 10-18-2007 10:06 AM
Parsing comma delimited text file chengwei Shell Programming and Scripting 5 02-23-2007 04:38 AM
Looping thru tab delimited data tipsy Shell Programming and Scripting 6 10-17-2006 05:44 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-21-2008
Registered User
 

Join Date: Nov 2008
Location: Canada
Posts: 4
Post Turn HTML data into delimited text

I have a file I've already partially pruned with grep that has data like:

<a href="MasterDetailResults.asp?textfield=a&Application=3D Home Architect 4">3D Home Architect 4</a> </td>
Approved </td>
--
<a href="MasterDetailResults.asp?textfield=a&Application=3d Home Architect 6">3d Home Architect 6</a> </td>
Not Approved </td>
--
<a href="MasterDetailResults.asp?textfield=a&Application=A to Zap">A to Zap</a> </td>
Approved </td>
--

except much, much more of it ;-)

I want to get the application name (i.e. 3D Home Architect 4) and the status (i.e. Approved or Not Approved) and turn it into this:

3D Home Architect 4|Approved
3d Home Architect 6|Not Approved
A to Zap|Approved
etc.

for use as a searchable database or import into Excel

I want to use bash scripting with sed or gawk to do this in the smallest number of lines (number of lines is not critical, of course ;-)

Thanks in advance for your help.
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-21-2008
Moderator
 

Join Date: Feb 2007
Posts: 3,549
Try this:

Code:
awk -F"\"" '/Application=/{sub(".*a&","",$2);s=$2;getline;FS=" ";$0=$0;print s"|"$1}' file
Reply With Quote
  #3 (permalink)  
Old 11-21-2008
Registered User
 

Join Date: Nov 2008
Location: Canada
Posts: 4
Thanks Franklin52, that's a start. I got:
Application=3D Home Architect 4|Approved
Application=3d|Not
Application=A|Approved
when I ran it. I'll keep on working on it.
Reply With Quote
  #4 (permalink)  
Old 11-21-2008
Registered User
 

Join Date: Sep 2008
Posts: 205
Hi,

try

Code:
sed -n '/Application/{N;s/.*Application=\([^"]*\).*\n\(.*\)<.*/\1 | \2/p}' file
If you sed doesn't support \n you have to write

Code:
sed -n '/Application/{N;s/.*Application=\([^"]*\).*\
\(.*\)<.*/\1 | \2/p}' file
instead.

HTH Chris
Reply With Quote
  #5 (permalink)  
Old 11-22-2008
Moderator
 

Join Date: Feb 2007
Posts: 3,549
Quote:
Originally Posted by macxcool View Post
Thanks Franklin52, that's a start. I got:
Application=3D Home Architect 4|Approved
Application=3d|Not
Application=A|Approved
when I ran it. I'll keep on working on it.
This should work:
Code:
awk -F"\"" '
/Application=/{
  sub(".*=","",$2); s=$2
  getline; sub(" <.*","")
  print s "|" $0
}' file
Reply With Quote
  #6 (permalink)  
Old 11-23-2008
Registered User
 

Join Date: Jun 2007
Location: Beijing China
Posts: 880
perl:

Code:
undef $/;
open FH,"<d:/a.txt";
$str=<FH>;
@arr=split("--",$str);
map {s/<a.*>(.*)<\/a>(.*)<\/td>\n(.*)<\/td>/$1|$3/} @arr;
print "@arr";
close FH;
Reply With Quote
  #7 (permalink)  
Old 11-24-2008
Registered User
 

Join Date: Nov 2008
Location: Canada
Posts: 4
Thank you all for your solutions. I'm going to use Christoph Spohr's because I'm more comfortable with sed than I am with awk (although I know it's very powerful). I get an output with spaces after the pipe because there are spaces at the beginning of the line. How can I modify
Code:
sed -n '/Application/{N;s/.*Application=\([^"]*\).*\n\(.*\)<.*/\1 | \2/p}' file
to get rid of those spaces.
Also, what if my input file has another line between the two lines in question:
Code:
    <tr> 
      <td height="23" align="default" valign="top"> 
        <a href="MasterDetailResults.asp?textfield=a&Application=3D Home Architect 4">3D Home Architect 4</a> </td>
      <td align="default" valign="top"> 
        Approved </td>
    </tr>
Once again, I need: Application Name|Status as my output. I've been removing the
<td align="default" valign="top">
line with sed before finishing things off with the sed code above.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Bookmarks

Tags
bash, csv, delimited, html, sed awk bash shell

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:




All times are GMT -4. The time now is 06:52 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66