Using shell to generate case statement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using shell to generate case statement
# 1  
Old 07-08-2017
Using shell to generate case statement

Hi Gurus,

I have a very weird requirement and have no clue to resolve the issue. please help me get out this difficulty
below two tables, table1 contains the column name. D means this column used for the rule. for example: rule 0 is all columns have value, rule1 is col3 and col7 have no value. the case statement is based on table1 key order. 0 first, then 1...

table2 contains the column values.
the requirement is: based on table2 listed value write a case statement
below data the case statement is

Code:
case when COL1 = 'V' AND COL2 = 'B' AND COL3 = 'C' AND P_CODE = 0 AND M_SRC = 5 AND M_FEC = 0 AND SEC_TYP = 1 THEN 1
WHEN COL1 = 'V' AND COL2 = 'B' AND P_CODE = 0 AND M_SRC = 5 AND M_FEC = 0  THEN 2
WHEN COL1 = 'V' AND COL2 = 'I' AND  P_CODE = 0 AND M_FEC = 0 THEN 3
WHEN COL1 = 'F' AND COL2 = 'B' AND P_CODE = 0 AND M_SRC = 5 AND M_FEC = 0 THEN 4
END


table1
Code:
key	SRC1	SRC2	SRC3	SRC4	SRC5	SRC6	SRC7
0	COL1    COL2    COL3	P_CODE	M_SRC	M_FEC	SEC_TYP
1	COL1	COL2	D	P_CODE	M_SRC	M_FEC	D
2	COL1	COL2	COL3	D	M_SRC	D	D
3	COL1	COL2	D	D	M_SRC	M_FEC	SEC_TYP
4	COL1	COL2	D	D	M_SRC	D	SEC_TYP

table2
Code:
COL1	 COL2	COL3	P_CODE	M_SRC	M_FEC	SEC_TYP	RESULT
V	B	C	0	5	0	1	1
V	B		0	6	0		2
V	I		0		0		3
F	B		0	5	0		4

thanks in advance
# 2  
Old 07-08-2017
Not clear.

Is the requirement "For every rule in table 1 scour table 2 for one (or more?) matching records and print out one case in the case statement"? Match means identical yes / no (value / empty) patterns between the two tables. As line 2 and 4 in table 2 have the same yes / no pattern, you want two cases printed?

The third case statement cannot be produced with any of the keys in table 1.
Why do you need table 1 at all? The case statement shown can be produced from table 2 alone.
Is it correct that keys 2,3, and 4 are not applicable to table 2 shown as P_CODE is not empty in any record?
Why, in the second case, is M_SRC = 5 and not 6 as in table 2?
Do you really mean "D means this column used for the rule", or the opposite?
# 3  
Old 07-08-2017
Based on just table 2, you can have
Code:
awk '
BEGIN   {print "case"
        }
NR == 1 {for (i=1;i<=NF; i++) COL[i] = $i
         next
        }
        {for (i=1; i<=NF; i++)  {if ($i != "") printf "%s %s = %s", i==1?"WHEN ":i==NF?" THEN ":" AND", COL[i], $i
                                }
         printf ORS
        }
' FS="\t" file
case
WHEN  COL1 = V AND COL2 = B AND COL3 = C AND P_CODE = 0 AND M_SRC = 5 AND M_FEC = 0 AND SEC_TYP = 1 THEN  RESULT = 1
WHEN  COL1 = V AND COL2 = B AND P_CODE = 0 AND M_SRC = 6 AND M_FEC = 0 THEN  RESULT = 2
WHEN  COL1 = V AND COL2 = I AND P_CODE = 0 AND M_FEC = 0 THEN  RESULT = 3
WHEN  COL1 = F AND COL2 = B AND P_CODE = 0 AND M_SRC = 5 AND M_FEC = 0 THEN  RESULT = 4

You need to make very sure that empty fields are recognized by e.g. using an explicit field separator <TAB>.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script run in a case statement call to run a php file, also Perl

Linux System having all Perl, Python, PHP (and Ruby) installed From a Shell script, can call a Perl, Python, PHP (or Ruby ?) file eg eg a Shell script run in a case statement call to run a php file, also Perl or/and Python file??? Like #!/usr/bin/bash .... .... case $INPUT_STRING... (1 Reply)
Discussion started by: hoyanet
1 Replies

2. Shell Programming and Scripting

Shell scripting with case statement

Foe example we have three environments int,qa and prod.Each environment has some number of servers. int=Server1,Server2,Server3 qa=Server4,Server5,Server6 prod=Server7,Server8,Server9 echo "Enter the Environment i.e int,qa,prod" read env case $env in int) ## Need command where all the... (9 Replies)
Discussion started by: nareshreddy443
9 Replies

3. Shell Programming and Scripting

Case statement in UNIX shell script

have written the below code to check whether the string received from user is a file name or dir using case statement, but its going into default case*). #!/bin/sh #Get a string from user and check whether its a existing filename or not rm str2 rm str3 echo "enter a file \c" read fil... (8 Replies)
Discussion started by: Mohan0509
8 Replies

4. Shell Programming and Scripting

Generate sql statement using shell scripting

Can anyone please assist me? I have attached 2 input files and one output file. I need to generate the sql update statements using the above 2 input files. if inputfile2 has 5 rows, then we should generate 5 update statements because column1 is unique. inputfile1 and inputfile2 may contain more... (10 Replies)
Discussion started by: vinus
10 Replies

5. Shell Programming and Scripting

Pass values to case statement in a function korn shell

I'm in the process of writng a function that consists of a case statement is there a way of calling the function and passing a value to it? ie function1 () { case opt1 do ..... opt2 do..... esac } function opt1 I'm aware the syntax is not correct, but you get the general idea. (1 Reply)
Discussion started by: squrcles
1 Replies

6. Shell Programming and Scripting

Case Statement

Hey, guys I really need some help with a project. "Write a shell program that examines the command line arguments, counts and collects the number of options. Basically it has to collect and count the arguments that start with a "-" and the one's that don't start with a - I know I have to use... (2 Replies)
Discussion started by: sk192010`
2 Replies

7. Shell Programming and Scripting

Shell case statement

echo -e "Select: \c" read IN pattern="1-20" case $IN in ) echo "Selected: $IN" ;; *) echo "Invalid selection: $IN" ;; esac # sh test Select: 10 Invalid selection: 10 # sh test Select: 2 (6 Replies)
Discussion started by: Ikon
6 Replies

8. Shell Programming and Scripting

shell script case statement

In a case statement like below : case $rental in "car") echo "For $rental Rs.20 per k/m";; "van") echo "For $rental Rs.10 per k/m";; "jeep") echo "For $rental Rs.5 per k/m";; "bicycle") echo "For $rental 20 paisa per k/m";; *) echo "Sorry, I can not gat a $rental for you";;... (4 Replies)
Discussion started by: sriram003
4 Replies

9. Shell Programming and Scripting

what is problem with this small shell script.. case statement related

Hi All, this small script is written to recognize user input character.. it is in small case .. upeer case or is a number... but when i input first capital letter say A.. it always gives small character.... what is the problem. #!/bin/bash echo "Enter the character" read a case $a in )... (2 Replies)
Discussion started by: johnray31
2 Replies

10. Shell Programming and Scripting

Shell script automation using case statement

Hi, I'm trying to write a shell script that has a menu and then dependant on the selection, will automate some samba file transfer. The problem is when I run the code without the case statement it runs fine. but when I put the case statement in the only way I can get the code to run is to... (6 Replies)
Discussion started by: ianf
6 Replies
Login or Register to Ask a Question