Find the answer to your Linux question:
Results 1 to 3 of 3
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 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jul 2010
    Posts
    16

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


    Thanks
    Maya

  2. #2
    Linux Newbie theNbomr's Avatar
    Join Date
    May 2007
    Location
    BC Canada
    Posts
    155
    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).

    Code:
    #
    #  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
            filename=${OUT}/ODS.POS.${pharmacyid}.${tablename}.${CURRENT_DATE}.dat
            echo $header > $filename
            grep -w $pharmacyid $pharmacyf >> $filename
            # If grep failed to find something, delete the file
            if [ $? ]; then
                rm $filename
            fi        
        # Is "inputfile" a constant, or an as-yet-undefined variable?
        done< inputfile
    done
    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 05:40 PM.
    Stuff happens. Then stays happened.

  3. #3
    Just Joined!
    Join Date
    Jul 2010
    Posts
    16
    Thank you so much! You are great!

    Maya

  4. $spacer_open
    $spacer_close

Posting Permissions

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