Find the answer to your Linux question:
Results 1 to 3 of 3
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    File splitting issues

    I am splitting a file based on the values read from an input file. The below one is the script.

    1)How do I add the header which is present in the original file to the new split files created?(For eg. pharmacyf conatins header as table column names. The new files created (ODS.POS.$pharmacyid.$tablename.$CURRENT_DATE.dat) are without the header).

    2) Also the script is creating 0 byte files for the pharmacyids which are not available in the intial file? Can this be avoided?

    for pharmacyf in *
    tablename=`echo $pharmacyf |cut -f4 -d'.' `
    while read pharmacyid
    grep -w $pharmacyid $pharmacyf >> $OUT/ODS.POS.$pharmacyid.$tablename.$CURRENT_DATE.dat
    done< inputfile


  2. #2
    Linux Newbie theNbomr's Avatar
    Join Date
    May 2007
    BC Canada
    It would have been helpful to post your code in [CODE ][/CODE] tags, and to have shown enough of the input file(s) data to know hw to parse it (also in [code ] tags; very important).

    #  Variables not defined here:  $OUT $pharmacyid $CURRENT_DATE
    #  Variables possibly undefined here: $inputfile
    for pharmacyf in *; do
       tablename=`echo $pharmacyf |cut -f4 -d'.' `
       # Read header into $header
       header = "grep/cut/etc header content from $pharmacyf here" 
        while read pharmacyid; do
            echo $header > $filename
            grep -w $pharmacyid $pharmacyf >> $filename
            # If grep failed to find something, delete the file
            if [ $? ]; then
                rm $filename
        # Is "inputfile" a constant, or an as-yet-undefined variable?
        done< inputfile
    If you have data which is constant through each iteration of a loop, then define it before entering the loop (the header), then use it on each iteration of the loop.
    You haven't show enough information to determine how the header is found/parsed.
    --- rod.
    Last edited by theNbomr; 07-31-2010 at 04:40 PM.
    Stuff happens. Then stays happened.

  3. #3
    Thank you so much! You are great!


  4. $spacer_open

Posting Permissions

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