If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > shell script to read the fields of text file line by line

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-11-09, 13:55
anant123123 anant123123 is offline
Registered User
 
Join Date: Nov 2007
Posts: 21
shell script to read the fields of text 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,
Reply With Quote
  #2 (permalink)  
Old 07-12-09, 06:05
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
The following should work.
Just send all payments to the usual address.
Code:
#!/bin/sh

file=your_text_file.dat

cat $file | (
        read type files

        while test "$type" != ""
        do
                if test "$type" = "pgm1"
                then
                        echo cp $files directory_1
                fi

                if test "$type" = "pgm2"
                then
                        echo cp $files directory_2
                fi

                if test "$type" = "pgm3"
                then
                        echo cp $files directory_3
                fi

                read type files
        done
        )
exit 0
Mike
Reply With Quote
  #3 (permalink)  
Old 07-13-09, 08:34
anant123123 anant123123 is offline
Registered User
 
Join Date: Nov 2007
Posts: 21
Thanks Mike,

It's working for me.
in addition to that, I need to find the files in two directories. that means the text file contain only file name and not the full path of the file. the shell script should search the file in two directories and copy from the available directory.

And also the shell script should be dynamic to accept the program (first field of the text file) and then proceed.

something like below..

cat file <text file> |
for pgmname=input pgm parmater
do
if the file11 is in dir1 then
cp /dir1/file11 /new_dir1
else if file11 is in dir2 then
cp /dir2/file11 /new_dir1
else exit
.......
....
for all other fields also
.....
....
done
Reply With Quote
  #4 (permalink)  
Old 07-14-09, 04:34
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
Quote:
Originally Posted by anant123123
It's working for me.
in addition to that, I need to find the files in two directories. that means the text file contain only file name and not the full path of the file. the shell script should search the file in two directories and copy from the available directory.

And also the shell script should be dynamic to accept the program (first field of the text file) and then proceed.
Would I be wrong in assuming that you're the one being paid to do this?
Reply With Quote
  #5 (permalink)  
Old 07-14-09, 16:53
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,448
Quote:
Originally Posted by mike_bike_kite
Would I be wrong in assuming that you're the one being paid to do this?
Not only that, but your job will become subject to "global resource optimization" once you finish writing this script.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On