The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How count number of fields in a record sureshg_sampat Shell Programming and Scripting 5 01-07-2008 02:30 AM
parse files and delete pimentelgg Shell Programming and Scripting 0 12-12-2007 02:06 PM
How to parse 2 files simultaneously Awanka Shell Programming and Scripting 8 04-12-2007 05:00 PM
shell scripts that parse log files trueman82 Shell Programming and Scripting 4 11-22-2006 02:09 AM
awk print fields to multiple files? TheCrunge Shell Programming and Scripting 1 06-08-2006 04:05 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-20-2008
Registered User
 

Join Date: Feb 2008
Posts: 2
Re-usable function to parse csv files with different number of fields

Hi there, been pondering how to deal with this and hoping someone would give me an insight on this.

I need help on creating a reusable bash funtion to parse csv files containing different number of fields (comma-seperated).

My initial thought is to create function for each input csv file (20+ of them, which means I have to create 20+ functions).

Appreciate any help.

Thanks.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 03-21-2008
Registered User
 

Join Date: Mar 2008
Posts: 34
I store my ksh script config settings externally and separate them by colons; you can change the cut delimiter to a comma. When needed in the process I call the following parse_parms function and set the requisite number of parms. Might not be optimal, but it's quick like a bunny and it keeps my logging:

Code:
###########################
#  function Parse_Parms ...
#     - Parse arguments based on ${my_mthd} arg...
function parse_parms
{

   $_dbg_mode

   print "\n\tGreetings from parse_parms ($@) "              >>${MY_LOG}
2>&1

   print "\n\tParsing parms per config file: ${config_file}" >>${MY_LOG}

   export args_rcd=$(grep -i "^${my_mthd}:" ${config_file} |grep -v "#" |tr
" " "_" )

   typeset -i parms_count=$(print $(print ${args_rcd} |tr ":" "\n" |wc -l )
+ 0 |bc )

   typeset -i  arg_num="1"

   while (( ${arg_num} <= ${parms_count} ))
   do
      print "Setting arg_num${arg_num}: "
      export param_num${arg_num}=$(print "$(echo "${args_rcd}" |cut -d":"
-f${arg_num} )")
      arg_num=$(print "${arg_num} + 1" |bc )
   done

   print "\tFinished Parsing your args..."                   >>${MY_LOG}

#  If there are no explicitly set params, then there's nothing to
do...exit...
   if [[ $(set |grep param_num ) == "param_num1=''" ]]
   then
      print "\n    ***\n   ${my_name}: $(date) "             |tee -a
${MY_LOG}
      print "   Parameters not found for ${my_mthd}..."      |tee -a
${MY_LOG}
      print "                                         "      |tee -a
${MY_LOG}
      print "   Called as: $0 ${*} \n    ***\n"              |tee -a
${MY_LOG}
      error_flag='Y' ; logger_heads ; return 2
#  Print the list of params otherwise...
   else
#     If this is an FTP process, then hide the password string...
      if [[ ${my_mthd} == "ftp" ]]
      then
         typeset -l  passed_word='[hidden]'
      fi
         print "\n\tCalled as:  $0 ${@} \n" \
            "\tParameters: ${param_num1} \n " \
            "\t            ${param_num2:-"#"} \n " \
            "\t            ${param_num3:-"#"} \n " \
            "\t            ${passed_word:-${param_num4}} \n " \
            "\t            ${param_num5-"#"} \n " \
            "\t            ${param_num6-"#"} \n " \
            "\t            ${param_num7-"#"} \n " \
            "\t            ${param_num8-"#"} \n " \
            "\t            ${param_num9-"#"} \n " \
            "\t            ${param_num10:-"#"} \n " \
            "\t            ${param_num11:-"#"} \n " \
            "\t            ${param_num12:-"#"} \n " \
            "\t            ${param_num13:-"#"} \n " \
            "\t            ${param_num14:-"#"} \n " \
            "\t            ${param_num15:-"#"} \n " |grep -v "#" \
                                                             |tee -a
${MY_LOG}
   fi

   echo "nicely done"

   return

}
Reply With Quote
  #3 (permalink)  
Old 03-21-2008
Registered User
 

Join Date: Feb 2008
Posts: 2
Re-usable function to parse csv files with different number of fields

Thanks, curleb.
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 10:12 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0