Results 1 to 5 of 5
  1. #1
    Join Date
    Nov 2007
    Posts
    21

    Unanswered: 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,

  2. #2
    Join Date
    Jun 2007
    Location
    London
    Posts
    2,527
    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

  3. #3
    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

  4. #4
    Join Date
    Jun 2007
    Location
    London
    Posts
    2,527
    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?

  5. #5
    Join Date
    Jun 2003
    Location
    Toronto, Canada
    Posts
    5,516
    Provided Answers: 1
    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.
    ---
    "It does not work" is not a valid problem statement.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •