Shell Sript to load data from flat file - Page 2
DBAsupport.com Forums - Powered by vBulletin
Page 2 of 2 FirstFirst 12
Results 11 to 18 of 18

Thread: Shell Sript to load data from flat file

  1. #11
    Join Date
    Mar 2001
    Posts
    314
    Use a script like the following:

    {
    tempregion = substr($3,1,7)
    tempyear = substr($2,4)
    if ( tempregion == "REGROLL" && tempyear >= 1996)
    printf("%s %d %s %s %f %f\n", $1,tempyear,$3,$4,$5,$6)
    }


    $0 refers to the whole input string - use it only if you need to print the input as is, if you need modifications print individual fields (as shown above) instead.

    I have assumed that the individual fields are separated by a whitespace. Please modify appropriately if it not so.

    -amar

  2. #12
    Join Date
    Aug 2000
    Location
    Belgium
    Posts
    342
    some extra awk stuff

    use sub() or gsub() to replace a substring in a string.

    use a combination of index(),length(),substr() to replace whatever you want in the string.

    Regards
    Gert

  3. #13
    Join Date
    Sep 2000
    Posts
    362
    Originally posted by amar
    .

    I have assumed that the individual fields are separated by a whitespace. Please modify appropriately if it not so.

    -amar
    Thanks Amar and denevge,

    The fields are of fixed length format and are not seperated by anything.

    Thanks
    Anurag
    Appreciation is a wonderful thing;
    It makes what is excellent in others belong to us as well.


  4. #14
    Join Date
    Sep 2000
    Posts
    362
    Hi,
    The fields are fixed length and how do I make sure that the length remains the same even after substituting the string bevause I cannot use $1, $2 ... in this case.

    Please Suggest.

    Thanks
    Anurag
    Appreciation is a wonderful thing;
    It makes what is excellent in others belong to us as well.


  5. #15
    Join Date
    Mar 2001
    Posts
    314
    Modify the script as follows:

    {
    tmpregion = substr( $0,21,10)
    tmpyear = substr($0,14,4)
    if ( tmpregion == "REGROLL " && tmpyear > 1996)
    printf("%s%s%s\n",substr($0,1,10),tempyear,substr($0,21))
    }

    Please check the relative positions of the fields. I am assuming the first field to be 10 chars, and that you want to print the rest of the record starting with "REG....".

    -amar


  6. #16
    Join Date
    Sep 2000
    Posts
    362
    Originally posted by amar
    Modify the script as follows:

    {
    tmpregion = substr( $0,21,10)
    tmpyear = substr($0,14,4)
    if ( tmpregion == "REGROLL " && tmpyear > 1996)
    printf("%s%s%s\n",substr($0,1,10),tempyear,substr($0,21))
    }

    Thanks Amar,
    what this script will do is will make the second field (tempyear) of 4 characters (from the substring). I need to pad it with 6 blank spaces and then start the third field.

    Any suggestions.

    Thanks
    Anurag
    Appreciation is a wonderful thing;
    It makes what is excellent in others belong to us as well.


  7. #17
    Join Date
    Mar 2001
    Posts
    314
    {
    tmpregion = substr( $0,21,10)
    tmpyear = substr($0,14,4)
    blanks = " "
    if ( tmpregion == "REGROLL " && tmpyear > 1996)
    printf("%s%s%s%s\n",substr($0,1,10),blanks,tempyear,substr($0,21))
    }


    -amar

  8. #18
    Join Date
    Mar 2001
    Posts
    314
    Heck, make that
    blank = "6 blanks"


    -amar

Posting Permissions

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



Click Here to Expand Forum to Full Width