[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Extractiing data with Perl



I Don't know perl, but if u want to get the job done awk emerges from the dust :) Try the following dirty awk line:

gawk 'BEGIN {RS="(\n)?<option"}; {tmp=tmp "<option" $0 "</option>" "\n"} END {print tmp}' countries.txt

BTW, save ur file in UNIX text format (b4 awk or perl), this may be what's messing up ur perl script.

Good luck!

At 08:49 AM 29-04-03, you wrote:

Salam,
I'm using Active State's Perl 5.8 on my Win2K AS machine.
I have a file that has a list of countries as an option list which I
want to extract, and put each option in a line and add a closing
</option> tag.
After trying for hours I got this script, which worked fine except that
it repeats any line that has an ending \n (newline).
---- Begin Perl script ----
#!/usr/bin/perl
while (<STDIN>) {
  while (m/(<option[\w\s=\"]*>[\w\s\d&#;]+)/gi){
    chomp();
    print "$1</option>\n";
  }
}---- End Perl script ----
I'm running the script as:
#perl extract.pl < countries.txt

Can any one please help, and also explain some things:
* How can I use \G (explained as: matches were the previous m//g left
off)?
* What is the difference between using chop() and chomp()?

I read through the manual but just couldn't figure it out.

Thanks in advance,
Ossama Khayat