create diffrent files based on other file and parameters list

I would like ot create shell script/ bash to create diffrent files based on a file and parameters list.

Here is the detail example: I have a textfile and four static parameter files (having �?'). mainfile.txt has below records (this count may be more than 50)
A200001
A200101
B200001
B200002
C200001
D200101
D200102
D200201

And 4 static parameter files corresponding to records in mainfile.txt, started with A,B,C,D
1.audit_paramter.prm
[PQRS.temp_info]
$$Source_file_audit=?
$$Source_file_audit_dir = \\dal1mspcx55\cogent$\productionqc\?\?

2.borrower_paramter.prm
[PQRS.temp_info]
$$Source_file_borrower =?
$$Source_file_borrower_dir = \\dal1mspcx55\cogent$\productionqc\?\?

3.carriage_paramter.prm
[PQRS.temp_info]
$$Source_file_carriage =?
$$Source_file_carriage_dir = \\dal1mspcx55\cogent$\productionqc\?\?

4.document_paramter.prm
[PQRS.temp_info]
$$Source_file_document =?
$$Source_file_document_dir = \\dal1mspcx55\cogent$\productionqc\?\?

Now I would like to create different parameter files based on the records from mainfile.txt and using static parameter files

If records starts with A, it should create audit_paramter_*.prm such as
audit_paramter_A200001.prm
[PQRS.temp_info]
$$Source_file_audit=A200001
$$Source_file_audit_dir = \\dal1mspcx55\cogent$\productionqc\2000\A200001

audit_paramter_A200101.prm
[PQRS.temp_info]
$$Source_file_audit=A200101
$$Source_file_audit_dir = \\dal1mspcx55\cogent$\productionqc\2001\A200101

If records starts with b, it should create borrower_paramter_*.prm and so on.. like 8 different parameter files??? Would this possible do with sed / scripting??? Can someone please assist?

I can replace �?' with other symbols to differentiate two different values

Thanks in advance

while read record
do
 year=`expr substr ${record} 2 4`
 case $record in
  A*)
      PARM_FILE="audit_parameter_${record}.prm"
      echo "[PQRS.temp_info]" > $PARM_FILE
      echo "\$\$Source_file_audit=${record}" >> $PARM_FILE
      echo "\$\$Source_file_audit_dir = \\\\dal1mspcx55\\\cogent\$\\productionqc\\${year}\\${record}" >> $PARM_FILE
      ;;
  B*) 
      PARM_FILE="borrower_parameter_${record}.prm"
      echo "[PQRS.temp_info]" > $PARM_FILE
      echo "\$\$Source_file_borrower=${record}" >> $PARM_FILE
      echo "\$\$Source_file_borrower_dir = \\\\dal1mspcx55\\\cogent\$\\productionqc\\${year}\\${record}" >> $PARM_FILE
      ;;
  C*)
      PARM_FILE="carriage_parameter_${record}.prm"
      echo "[PQRS.temp_info]" > $PARM_FILE
      echo "\$\$Source_file_carriage=${record}" >> $PARM_FILE
      echo "\$\$Source_file_carriage_dir = \\\\dal1mspcx55\\\cogent\$\\productionqc\\${year}\\${record}" >> $PARM_FILE
      ;;
  D*)
      PARM_FILE="document_parameter_${record}.prm"
      echo "[PQRS.temp_info]" > $PARM_FILE
      echo "\$\$Source_file_document=${record}" >> $PARM_FILE
      echo "\$\$Source_file_document_dir = \\\\dal1mspcx55\\\cogent\$\\productionqc\\${year}\\${record}" >> $PARM_FILE
      ;;
 esac 
done<mainfile.txt

Hi Krishmath...

Thanks for the soultion. Result as expected except with two things...

  1. All parameter are creating with space (i can say "with new line") after the $record value such as borrower_parameter_B200103
    .prm

  2. It is not reading the last record of the file and not creating the parameter for that.

Content of the newly created files are really good. Can you please provide the solution for these two...

Thanks for your help!

I tested the code and I'm getting the correct parameter file name, such as audit_parameter_A200101.prm

Are you transferring the mainfile.txt from Windows to Unix? What do you see when you open the file in vi? Are there any special characters at the end?

Yes... This mainfile.txt is getting copied from windows server and i can see ctl chars when i open it on Unix. Any solution for this too:confused:? Thanks

Yes, use dos2unix or dos2ux (based on your OS) on the mainfile.txt to get rid of the ctl characters at the end.

Thanks much! It worked... I used the command
cat /u01/opt/incoming/mainfile.txt | tr -d '\015' > /u01/opt/incoming/mainfile.txt

Prgm is working as expected. Thanks lot... :slight_smile:

Last Question.. Do you have any idea on DOS commands?
I need a similar dos command for this? to select first file of similar pattern of files in one directory...
ls /u01/opt/incoming/abc_*.txt | head -1

Thanks

Sorry, no idea on dos commands!

Use the GNU utilities for Win32
You'll find head.exe, ls.exe, ... :eek:

use strict;
my %hash=('A','audit','B','borrower','C','carriage','D','document');
sub l_sub{
	my ($id,$year,$name)=(@_);
	my $in_file =$hash{$id}."_paramter.prm";
	my $out_file=$hash{$id}."_paramter_".$name.".prm";
	open my $ifh,"<", $in_file  or die "Can not open file";
	open my $ofh,">>", $out_file or die "Can not open file";
	while(<$ifh>){
		if($.==2){
			$_=~s/\?/$name/;
		}
		if($.==3){
			$_=~s/\?\\\?/$year\\$name/;
		}
		print $ofh $_;
	}
}
open my $fh,"<","a.txt";
while(<$fh>){
	/(([A-Z])(\d{4}).*)/;
	l_sub($2,$3,$1);
}