ITEM: N0869L

How to read in colon delimited file in Korn Shell


Question:

Env:    AIX 3.2.5, Korn Shell

Desc:   Customer also has a specific question on how to read in 
        a file that has colon delimited fields into a Korn Shell 
        script.  The customer has a file that contains lines that
        consist of "username:menu_name".  The customer would like
        to assign each field to a variable and then do processing
        with those variables.

Action: In order to read in colon delimited fields you will want
        to set the IFS (Internal File Separator) environment variable
        to include colon (:) and then use a while loop to read in each 
        line and process it accordingly.  Here is a sample program 
        that uses this logic:

        \#!/bin/ksh
        IFS=IFS:,
        while read i
        do
          echo $i
        done \< ./efiles

        If rmfiles_file contains the following:

        file1,file2:file3
        file4:file5,file6

        this script will perform the following commands:

        echo file1 file2 file3
        echo file4 file5 file6

        If you were going to be reading in two fields into two
        separate variables, you would want to use a while loop
        similar to the following:

        while read i j
        do
        ...\...
        done \< filename_of_file_that_contains_data

        There are the two Korn Shell Programming books that prove
        useful to you in writing Korn Shell scripts:

        Bolsky, Morris I. and David G. Korn. "The KORNSHELL Command and 
                Programming Language", Prentice Hall, Englewood Cliffs, 
                NJ. 1989.

        Rosenblatt, Bill.  "Learning the Korn Shell", O'Reilly & 
                Associates, Inc., Sebastopol, CA. 1993.

        You can generally find these books in Technical Book Stores.  
        Also, the second book can be ordered from O'Reilly & Associates.
        To get their catalog of books, call 1-800-998-9938 or e-mail to 
        order@ora.com.


Support Line: How to read in colon delimited file in Korn Shell ITEM: N0869L
Dated: October 1994 Category: N/A
This HTML file was generated 99/06/24~13:30:39
Comments or suggestions? Contact us