shell script to read file line by line

Hi,

I need to read a text file from shell script line by line and copy the feilds of each line.
Below is the complete requirement.

I've text file which contains ...

pgm1 file11 file12 file13
pgm2 file21 file22
pgm3 file31 file32 file33

I'll give input as pgm1/pgm2/pgm3 etc.

the shell script has to read the text file and if the input given is pgm1 then the shell script shoud copy the file to some directory.
file directory is common for all the files.

when the input is pgm2 then the shell script should have statements for copying file21, file22 etc and so on.

I tried the file reading with WHILE DO and FOR LOOP and used CUT option to differentiate the feilds. But I'm getting all the records with the looping and I'm unable to cut exactly as the number of files for each program differs.

Can anybody suggest code which completes my requirement?
I'm using KSH.

Thanks,
Ani

cp $(grep pgm1 file | cut -d " " -f 2-) new_directory 2>/dev/null || echo Error

Or in a script:

[ $# -eq 0 ] && echo "Tell me what to copy..." && exit 1
 
for KEY in $@; do
  cp $(grep $KEY file | cut -d " " -f 2-) new_directory 2>/dev/null || echo Error
done

Where file is the file containing

pgm1 file11 file12 file13
pgm2 file21 file22
pgm3 file31 file32 file33

Try this:
set `egrep '^pgm2' temp.txt` | cp $2 $3 $4 $5 $5 $7 new_directory

Assumed "temp.txt" is the input files.
Assumed "new_directory" is the directory to copy to.
Assumed max. number of files to copy is 7.
If not increase/decrease $2......$7.
Do not use $0 or $1.

Take care of error trapping and consider writing into a script.
Ex: What if egrep finds nothing?
set `egrep '^pgm' temp.txt` && cp $2 $3 $4 $5 $5 $7 new_directory
(Note the && in the above line)

Thanks for the reply.

For the below query, I need to search for the file in 2 directories and if the file is available then copy the file from that directory.

for ex: if the pgm is pgm1 and first field is file11 then

if the file11 is in dir1 then copy /dir1/file11 to new directory else if the file11 is in dir2, then
copy /dir1/file11 to new directory else exit.

that means the text file contains only filename and not the full path. The shell script should search the files in 2 directories.

the code is :
for KEY in $@; do
cp $(grep $KEY file | cut -d " " -f 2-) new_directory 2>/dev/null || echo Error
done

Thanks,
Ani

Not sure if I got you right.
Presume this is what you are looking for.

set `egrep '^pgm2' temp.txt`
cd dir1
cp $2 $3 $4 $5 $5 $7 new_directory
cd dir2
cp $2 $3 $4 $5 $5 $7 other_directory

If it does not find the file(s) in dir1 or dir2 it might throw some error.
I think you can ignore them. Is that OK?