Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help required............. 1

Status
Not open for further replies.

jpor

Technical User
Nov 29, 2000
212
GB
I have been given the task to e-mail 480 stores with SQL outputs. Currently I have set-up an INFORMIX 7 SE SQL to pick up the stores and pipe the output to a file.
So far so good. But the output from the file is wrong and I need to re-arrange the text into some sort of E-mail name and seperate each one into a file of it's own.
I have had a look at the cut and paste commands from a book somone has lent me. And have started from scratch trying to piece together a KSH script to do the job.
The data from the database in the file that I created from the SQL looks like this:

location lname - (001) store sname
location lname - (002) store sname
etc... 480 times.

The e-mail address is in this format:
locationlname001.sname@storesname.co.uk

Any pointers on how I can achieve this ?

Thanks in advance.



 
while read line
do
set $line
num=${4#"("}
num=${num%")"}
echo $1$2$num.$6@$5$6.co.uk >outfile.$num
done < infile

Tested ...

==> outfile.001 <==
locationlname001.sname@storesname.co.uk

==> outfile.002 <==
locationlname002.sname@storesname.co.uk

 
Ygor. Thanks for that.

I have had a go at running it. But I have hit a snag. Some store locations are split in to two for example one of these is: NEWTON ABBEY

When I run the script It only brings back one of the words I.e ABBEY. Not all places are split.
Have tried incurring $7 into the script but this doesn't work. Any ideas ?

Thanks again.
 
Ygor. Forget the last message. I have worked it out.
I was using $6 $7 instead of using $5 $6. D'oh!

Thanks again for your help.

 
Okay. Now I need to know how I can capture the output into multiple variables instead of files.

I take it I can still use the script but how do you output to multiple to seperate variables ?

Thanks in advance.


 
#----I would recommend using an array instead of multiple variables...
while read line
do
set $line
num=${4#&quot;(&quot;}
num=${num%&quot;)&quot;}
eval array[$num]=$1$2$num.$6@$5$6.co.uk #----have to use eval
done < infile

#----example usage...
echo array[001] = ${array[001]}
echo array[002] = ${array[002]}
echo size of array = ${#array[*]} #----returns number of array elements

#----another benefit of using arrays is the simple loop construct...
for address in ${array[*]}
do
echo address = $address
done

Tested...
[navy]
array[001] = locationlname001.sname@storesname.co.uk
array[002] = locationlname002.sname@storesname.co.uk
size of array = 2
address = locationlname001.sname@storesname.co.uk
address = locationlname002.sname@storesname.co.uk
[/navy]


#----but if you really want multiple variables...
while read line
do
set $line
num=${4#&quot;(&quot;}
num=${num%&quot;)&quot;}
eval var_$num=$1$2$num.$6@$5$6.co.uk
done < infile

#----example usage...
echo var_001 = $var_001
echo var_002 = $var_002


Tested...
[navy]
var_001 = locationlname001.sname@storesname.co.uk
var_002 = locationlname002.sname@storesname.co.uk
[/navy]


 
Thanks again ygor. Your masterclass in scripting is very good.

Okay. What I'm attempting to do now is try to list each file in turn from 2 seperate areas i.e /email_list/ & /reportout/

I.e I need to &quot;ls -l&quot; from one directory (email_list) and cut out all but the file name. Do the same from another directory (reportout) and do the same but output to 1 single file an adjoining list which will be like this:

Report_name e-mail address

I know that the files will marry together as both come from a SQL sorted by name.

From what I can gather you can use this command after creating the 1 single parameter file.

grep $report_name $parameter_file |awk '{print $2}'

In the parameter file i want to put in:

report_name e-mail address

The i can use this command to e-mail:

/usr/local/bin/mpack -s &quot;report for `date`&quot; file_name e-mail address 480 times.

Any pointers ?

Thanks yet again in advance.
 
Amendment:

I.e I need to &quot;ls -l&quot; from one directory (email_list) and cut out all but the file name. Do the same from another directory (reportout) and do the same but output to 1 single file an adjoining list which will be like this:

Report_name e-mail address

Instead I need to grep the contents from ls -l on all files from (email_list) and then ls -l and cut all but the file name and extention from report_out) and paste them into 1 file.

Sorry. I working with this on the fly due to time constraints and other duties.

Thanks again.
 
Amendment:

I.e I need to &quot;ls -l&quot; from one directory (email_list) and cut out all but the file name. Do the same from another directory (reportout) and do the same but output to 1 single file an adjoining list which will be like this:

Report_name e-mail address

Instead I need to grep the contents from ls -l on all files from (email_list) and then ls -l and cut all but the file name and extention from report_out) and paste them into 1 file.

Sorry. I working with this on the fly due to time constraints and other duties.

Thanks again.
 
Okay.

I have worked out my last cry for help.

Currently I am working on another project where I can use the 'while read list' script YGor has shown me to do.

THe problem I have is with the parameter file I will be using. At the moment I have 261 addresses that I need to add to a database table. But as you can imagine I have spaces, which will be interpereted by the 'while read' as seprate variables. What I am planning on doing is seprating each line with a ','. For example:

2111101,wizbit ltd, 1 wizbit road, wizbitshire, wizbitland, w12 34a

This shows job number, company name, street name, county, country, zip/post code.

How do I tell the while read that the words between the commas means 1 variable ?

Tanks again in advance.
 
# assuming there are no 'embedded' commas
while IFS=',' read line

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
IFS=&quot;,&quot;
while read job company street county country zip
do
....
done < file1
unset IFS

From man ksh: IFS Internal field separators, normally space, tab, and new-line that are used to separate command words resulting from command or parameter substitution, and for separating words with the special command read.
 
I'd be saving the old IFS before setting it to ',' in the way Ygor is doing it. And setting it to the saved value AFTER the 'while loop'. Unsetting the IFS might have some determental effects to the rest of the script logic.

savedIFS=&quot;${IFS}&quot;
IFS=','
while
do
....
done
IFS=&quot;${savedIFS}&quot;

If you set the IFS 'inside' the 'while loop, its scope is whithin the loop and doesn't affect any other logic outside the loop.

It's your call.

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Thanks for the help guys.

I have tried the IFS=&quot;,&quot; and the savedIFS=&quot;${IFS}&quot;

But. When I echo back the fields I am getting commas as well. How do I not output the commas in my variables ?


Help is much appreciated as always.

Thnaks again.
 
Wjat does the following jpor.sh script outputs given a sample jpor.txt text file?

#------------------ jpor.txt
2111101,wizbit ltd, 1 wizbit road, wizbitshire, wizbitland, w12 34a


#------------------ jpor.sh
#!/bin/ksh

file='jpor.txt'

while IFS=',' read job company street county country zip
do
echo &quot;job->[${job}] county->[${county}]&quot;
done < ${file}


vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Thanks vgersh99.

That works a treat.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top