[wplug] awk help

Kuzman Ganchev kuzman at sccs.swarthmore.edu
Fri Aug 27 20:09:12 EDT 2004


On Fri, Aug 27, 2004 at 07:01:47PM -0400, chris.romano at verizon.net wrote:
> So how would I pick out certain fields?  I only want about 8 out of 60 fields.  Or could I use that perl script to change the commas in quotes to say semi-colons?
> 

Count the fields...

> > I would just go ahead and learn a little Perl:
> > 
> > #!/usr/bin/perl
> > 
> > open(FILE,"$ARGV[0]") || die "Could not read $ARGV[0]";
	%fields_I_want;
	$fields_I_want{0}=1;
	$fields_I_want{5}=1; 
	$fields_I_want{32}=1; // and so on
> > 
> > while ( <FILE> ) {
> > 
> > 	@line = split(/\"/,$_);    # first split the line on quotes
> > 	$quote = 0;                # set a toggle for whether we're in a quote section or not
	$field = 0;
> > 
> > 	foreach $qpart ( @line ) {
> > 
> > 		if ( $quote ) {
> > 			# if we're in a quote section just print it
 			print "\"$qpart\"" if $fields_I_want{$field};
> > 			$quote = 0;
> > 		} else {
> > 			# if we're not, split on commas and print the parts
> > 			@parts = split(/\,/,$qpart);
> > 			foreach $part ( @parts ) {
				$field+=1;
 				print "$part\n" if $fields_I_want{$field};
> > 			}
> > 			$quote = 1;
> > 		}
> > 
> > 	}
> > 
> > }
> > 
> > close FILE;
> > 

I haven't tested this at all, but I think you get the point. 

Kuzman
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://penguin.wplug.org/pipermail/wplug/attachments/20040827/922346e5/attachment-0001.bin


More information about the wplug mailing list