Click here to get back home

Help with script

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Help with script Andy 02-28-2008
Get Chitika Premium
Posted by Andy on February 28, 2008, 1:54 pm
Please log in for more thread options
> Andy wrote:
>
> > I am pretty new here, tyring to learn as I go.
>
> > I have a lil project I am trying to setup
>
> > Basically : below is a piece of the file
>
> > Has three fields Seperated by ~
>
> > 6081703039~1~My Own Company
> > 3081709039~1~DeweyCheetham&Howe
> > 9081710039~1~One Bad Firm
> > 2081757039~1~IRSUSUCK
> > 1082445039~1~
>
> > Basically I need the file to only show the Field that has all data
>
> > I am new so not sure if my syntax is correct
>
> > If field 3 is blank then don't show?
>
> > Can anyone help me write a Perl script to acocmplish this ?
>
> while ( <FH> ) {
> chomp;
> my @fields = split /~/, $_, -1;
>
> if ( 3 != @fields ) {
> warn "Error: should be 3 fields, record contains " . @fields .
> " fields instead.\n";
> next;
> }
>
> if ( 3 != grep length, @fields ) {
> warn "Error: one of the fields is empty.\n";
> next;
> }
> # Or, just the third field:
> unless ( length $fields[ 2 ] ) {
> warn "Error: the third field is empty.\n";
> next;
> }
>
> # process the data
> }
>
> John
> --
> Perl isn't a toolbox, but a small machine shop where you
> can special-order certain sorts of tools at low cost and
> in short order. -- Larry Wall

John

Thank you Very Much

I take it I have to turn around and

Add

!#/usr/bin/perl -w


Question How does the script know that I want it to Scrub a particular
file.

In this case the Above snippet is from a file called

msus.dat

?

Posted by John W. Krahn on February 28, 2008, 2:26 pm
Please log in for more thread options
Andy wrote:
>> Andy wrote:
>>
>>> I am pretty new here, tyring to learn as I go.
>>> I have a lil project I am trying to setup
>>> Basically : below is a piece of the file
>>> Has three fields Seperated by ~
>>> 6081703039~1~My Own Company
>>> 3081709039~1~DeweyCheetham&Howe
>>> 9081710039~1~One Bad Firm
>>> 2081757039~1~IRSUSUCK
>>> 1082445039~1~
>>> Basically I need the file to only show the Field that has all data
>>> I am new so not sure if my syntax is correct
>>> If field 3 is blank then don't show?
>>> Can anyone help me write a Perl script to acocmplish this ?
>> while ( <FH> ) {
>> chomp;
>> my @fields = split /~/, $_, -1;
>>
>> if ( 3 != @fields ) {
>> warn "Error: should be 3 fields, record contains " . @fields .
>> " fields instead.\n";
>> next;
>> }
>>
>> if ( 3 != grep length, @fields ) {
>> warn "Error: one of the fields is empty.\n";
>> next;
>> }
>> # Or, just the third field:
>> unless ( length $fields[ 2 ] ) {
>> warn "Error: the third field is empty.\n";
>> next;
>> }
>>
>> # process the data
>> }
>
> John
>
> Thank you Very Much
>
> I take it I have to turn around and
>
> Add
>
> !#/usr/bin/perl -w

Better to add:

#!/usr/bin/perl
use warnings;
use strict;

> Question How does the script know that I want it to Scrub a particular
> file.

It doesn't. The example posted is incomplete.

> In this case the Above snippet is from a file called
>
> msus.dat

Open the file first:

open FH, '<', 'msus.dat' or die "Cannot open 'msus.dat' $!";



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall

Posted by John Bokma on February 28, 2008, 3:04 pm
Please log in for more thread options

> Basically : below is a piece of the file
>
> Has three fields Seperated by ~
>
> 6081703039~1~My Own Company
> 3081709039~1~DeweyCheetham&Howe
> 9081710039~1~One Bad Firm
> 2081757039~1~IRSUSUCK
> 1082445039~1~
>
>
> Basically I need the file to only show the Field that has all data
>
> I am new so not sure if my syntax is correct

what syntax?

> If field 3 is blank then don't show?

        perl -ne "/~$/ or print;" yf.txt

or

        perl -ne "print unless /~$/" yf.txt

(yf.txt is your file :-) )

(assuming Windows, otherwise you might want to use single quotes)

Note: this assumes that the 3rd field can never end in ~
and that the 3rd field blank means empty.

--
John

http://johnbokma.com/mexit/2008/02/

Posted by Andy on February 28, 2008, 3:41 pm
Please log in for more thread options
> > Basically : below is a piece of the file
>
> > Has three fields Seperated by ~
>
> > 6081703039~1~My Own Company
> > 3081709039~1~DeweyCheetham&Howe
> > 9081710039~1~One Bad Firm
> > 2081757039~1~IRSUSUCK
> > 1082445039~1~
>
> > Basically I need the file to only show the Field that has all data
>
> > I am new so not sure if my syntax is correct
>
> what syntax?
>
> > If field 3 is blank then don't show?
>
> perl -ne "/~$/ or print;" yf.txt
>
> or
>
> perl -ne "print unless /~$/" yf.txt
>
> (yf.txt is your file :-) )
>
> (assuming Windows, otherwise you might want to use single quotes)
>
> Note: this assumes that the 3rd field can never end in ~
> and that the 3rd field blank means empty.
>
> --
> John
>
> http://johnbokma.com/mexit/2008/02/

John

thank you that worked wonderfully

although I am trying to understand why.

But as I was playing around I removed some characters from the file

and it prints data from lines that I removed the ~1~ For instance the
1

Can u explain some of this ,

Pardon if I am asking to much, but I am curious.

I have been wanting to delve into perl for a long while.



Posted by Tad J McClellan on February 28, 2008, 7:21 pm
Please log in for more thread options

> Subject: Help with script


Please put the subject of your article in the Subject of your article.


> Morning Guys


This is an international forum, so it is simultaneously night and day...


> I am pretty new here,


Please see the Posting Guidelines that are posted here frequently.


> tyring to learn as I go.


The best way to learn Perl is to write Perl code.


> Has three fields Seperated by ~
>
> 6081703039~1~My Own Company
> 3081709039~1~DeweyCheetham&Howe
> 9081710039~1~One Bad Firm
> 2081757039~1~IRSUSUCK
> 1082445039~1~
>
>
> Basically I need the file to only show the Field that has all data


perl -ne 'print if /.~.~./' tilde_file


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher0cmdat/"

Similar ThreadsPosted
How to generate radio buttons in Perl/CGI script with call to shell script? November 23, 2007, 11:24 pm
FAQ: I {changed directory, modified my environment} in a perl script. How come the change disappeared when I exited the script? How do I get my changes to be visible? October 6, 2004, 4:54 pm
FAQ 8.34: I {changed directory, modified my environment} in a perl script. How come the change disappeared when I exited the script? How do I get my changes to be visible? November 3, 2004, 12:03 am
FAQ 8.34: I {changed directory, modified my environment} in a perl script. How come the change disappeared when I exited the script? How do I get my changes to be visible? November 9, 2004, 6:03 am
FAQ 8.34 I {changed directory, modified my environment} in a perl script. How come the change disappeared when I exited the script? How do I get my changes to be visible? March 26, 2005, 12:03 am
FAQ 8.34 I {changed directory, modified my environment} in a perl script. How come the change disappeared when I exited the script? How do I get my changes to be visible? June 1, 2005, 11:03 pm
FAQ 8.34 I {changed directory, modified my environment} in a perl script. How come the change disappeared when I exited the script? How do I get my changes to be visible? October 19, 2005, 4:03 am
FAQ 8.34 I {changed directory, modified my environment} in a perl script. How come the change disappeared when I exited the script? How do I get my changes to be visible? November 17, 2005, 5:03 am
FAQ 8.34 I {changed directory, modified my environment} in a perl script. How come the change disappeared when I exited the script? How do I get my changes to be visible? September 10, 2006, 3:03 pm
FAQ 8.34 I {changed directory, modified my environment} in a perl script. How come the change disappeared when I exited the script? How do I get my changes to be visible? March 27, 2007, 3:03 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap