[CentOS] how can I use awk to match? pls help

Mark Weaver mdw1982 at mdw1982.com
Mon Nov 19 23:24:10 UTC 2007


ann kok wrote:
> Hi all
> 
> I don't have idea how to write this script, please
> help
> 
> I have thousand records in this format. eg: file No.3
> is 
> 
> 
> File No:       003
> Customer:      Ann
> Email address: xxx at info
> Country:       England
> Created by:    20071102
> 
> 
> file No. 4 is:
> 
> File No:       004
> Customer:      James
> Email address: xxx at gov
> Country:       Australia
> Created by:    20071105
> 
> 
> 
> 
> I need to write a script to replace those Fields 
> eg: (Customer/Email/Country.... when matching the
> FileNo.001...002...) to get Data in this file
> and also put the date in "Created by:    20071105"
> when I run this script 
> 
> 
> 
> File NO  Customer     	      Email Address     
> Country  
> --------------------------------------------------------
> 001      John     	      xxx at net            USA
> 002      Peter                xxx at com  	        
> Canada
> 003      David                xxx at org           
> Mexico
> 
> 
> I know awk can do it. but don't know how to match the
> File No. in original file and the data file
> 
> 
> Thank you for your help
> 

I would think PERL would be nicely suited for this need.

================================
#!/usr/bin/perl

#######################################################
# Incoming file format
# ---------------------------
# File No:       003
# Customer:      Ann
# Email address: xxx at info
# Country:       England
# Created by:    20071102
#######################################################
# run this program in the same directory where
# files to be read are stored, or set path variable
# to path where files are located.
#######################################################
# this code assumes that "ALL" files being read are
# in exactly the format listed above
#######################################################

#
# path to files to be read
$infiles = "./*"; # default setting assumes this program
			   # is being run from the same directory
			   # where the files being read exist.

#
# create an array of file names to be read
@records = `ls -w 1 $infiles`;

#
# path to file we're creating to contain information
$outfile = "./records_outfile"; # change file name
								# to what ever you want

open (OF, ">$outfile");

#
# Output column headers to output file
print OF "File No.\t\tCustomer\t\tEmail Address\t\tCountry\t\tCreated By\n";

foreach ( @records ){
	chomp $_;
	open (F, "<$_");
	foreach $r (<F>){
		chomp $r;
		($label,$value) = split(/\:/, $r);
		$value =~ s/^\s+//g; # trim off any spaces before value
		if ($string eq "") { # first value added to string
			$string = $value;
		}else{
			$string .= "\t\t$value";
		}
	}
	close(F);
	print OF "$string\n";
}

close(OF);
exit;

================================

-- 
Mark

"Drunkenness is not an excuse for stupidity. If you're stupid when
you're sober then that's one thing, but if you're sober when you're
stupid, then you're just plain stupid!"
==============================================
Powered by CentOS5 (RHEL5)



More information about the CentOS mailing list