ararghat has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to write a script for getting parameter & variable inside case block appended by nested case block. Can we write a subroutine for this using simple regular expressions. Nested case block is creating problem for me.

I was trying to match the content between "case" and "esac" ,but main case is counter with "esac" of nested case block.

A piece of the file is given below.

file.txt case `pattern` in ARAR) old_file="ak.csv" datefile="ak_nt_$curr_date.csv" case `kumar` in dbk*) newwack="rac_client_$curr_date.ack" IN_DIR="class/5060" ;; src) src_folder="class/5061" ;; *) echo "invalid parameter" ;; esac ;; SINGH) old_file="abh_cash.csv" date_file="ods_nt_cash_acc_$curr_date.csv" newwack="rac_cash_acc_$curr_date.ack" IN_DIR="xyx\5060" ;; GHAT) old_file="ods_ven.csv" newfile="ods_nt_ven_$curr_date.csv" newwack="rac_vendor_$curr_date.ack" IN_DIR="ofc\5060" case `para` in kol*) newreck="client_$curr_simul.ack" IN_DIR="vid/234" ;; src1*) src_folder="class/5061" ;; esac ;; esac

Output should be:

ARAR:old_file="ak.csv" ARAR:datefile="ak_nt_$curr_date.csv" ARAR:dbk*:newwack="rac_client_$curr_date.ack" ARAR:dbk*:IN_DIR="class/5060" ARAR:src:src_folder="class/5061" SINGH:old_file="abh_cash.csv" SINGH:date_file="ods_nt_cash_acc_$curr_date.csv" SINGH:newwack="rac_cash_acc_$curr_date.ack" SINGH:IN_DIR="xyx\5060" GHAT:old_file="ods_ven.csv" GHAT:newfile="ods_nt_ven_$curr_date.csv" GHAT:newwack="rac_vendor_$curr_date.ack" GHAT:IN_DIR="gunda\5060" GHAT:kol*:newreck="client_$curr_simul.ack" GHAT:kol*:IN_DIR="vid/234" GHAT:src1*:src_folder="class/5061"

Replies are listed 'Best First'.
Re: Getting content of case statement appended by inner case statement
by hippo (Archbishop) on Oct 12, 2016 at 11:31 UTC
Re: Getting content of case statement appended by inner case statement
by Corion (Patriarch) on Oct 12, 2016 at 11:45 UTC

    Consider the approach of simply running the case...esac expressions through your shell and then printing all the relevant variables.

    Usually, it's easier to run a script through the existing shell instead of rebuilding what the shell does in Perl.

Re: Getting content of case statement appended by inner case statement
by dsheroh (Monsignor) on Oct 12, 2016 at 11:38 UTC
    Tips for asking a question that will receive useful answers here:

    1. Provide runnable sample code demonstrating your problem, including the appropriate input data as well as the desired output.
    2. Tell us what the problem is.
    3. Ask a Perl-related question.

    "This fragment of bash script should provide this output" is not really something we can help with, unless someone spots a blatant syntax error, because we don't know what it's doing instead of producing the desired output and we can't run it ourselves to find out.

      my code is

      open (data, "<input.txt"); while (<data>) { s/^\s+//g; #LTRIM if (/"(.*)"\)(.*)$/) { #pattern match $parameter = $1; $var = $2 . "\n"; } else { $var = $_; } if (/case/../esac/){ if (/"(.*)"\)(.*)$/) { $parameter ="$parameter:$1"; $var = $2 . "\n"; } } if (/;;/) { print "\n"; next; } if($var=~/^\s*$/) {next;} #if $var is empty or 1 or more than 1 + whitespaces. elsif ($var!~/=/) {next;} #ignore if $var does not consist "=" +sign else{ print "$parameter: $var"; } } close data;