Sponsored Content
Top Forums Shell Programming and Scripting How to get value from a close and open parenthesis? Post 302998187 by durden_tyler on Friday 26th of May 2017 08:30:16 PM
Old 05-26-2017
Code:
$ 
$ cat -n statements.sql
     1    Insert into table_name (col1,col2,col3,col4,col5,DATE1,DATE2,col6,col7,col8,col9,col10,col11) values (6752,14932156,24,'ALL','Staff',to_date('04/17/2017 00:00:00','MM/DD/YYYY HH24:MI:SS'),to_date('05/24/2017 23:59:59','MM/DD/YYYY HH24:MI:SS'),'aso 4','device',148,null,null,null);
     2    Insert into emp (empno,ename,job,sal,hiredate,mgr,deptno) values (21,'Adrian','Salesman',1000,to_date('01/01/2017 00:00:00','MM/DD/YYYY HH24:MI:SS'),1,42);
     3    Insert into tname (col1,col2,col3,col4) values (to_date('01/01/2017 00:00:00','MM/DD/YYYY HH24:MI:SS'),to_date('02/01/2017 00:00:00','MM/DD/YYYY HH24:MI:SS'),to_date('03/01/2017 00:00:00','MM/DD/YYYY HH24:MI:SS'),to_date('04/01/2017 00:00:00','MM/DD/YYYY HH24:MI:SS'));
     4    Insert into temp (col1,col2,col3,col4) values (null,null,null,null);
$ 
$ 
$ cat -n generate_qry.pl 
     1    #!/usr/bin/perl -w
     2    use strict;
     3    # Accept the file name as first argument and open it
     4    my $file = $ARGV[0];
     5    
     6    open(FH, "<", $file) or die "Can't open $file: $!";
     7    while (<FH>) {
     8        # Parse the line and set the table name, column-list and value-list
     9        my ($tab, $cols, $vals) = m/into\s+(\S+)\s+\((.*)\)\s+values\s+\((.*)\)/i;
    10    
    11        # Split the column-list and value-list into arrays. The "to_date" token will
    12        # be split into two elements and will be joined during loop iteration.
    13        my @c = split(/,/,$cols);
    14        my @v = split(/,/,$vals);
    15        my $qry = sprintf("select * from %s", $tab);
    16        my ($left, $right, $j);
    17    
    18        foreach my $i (0..$#c) {
    19            # Each predicate is of the form "left-string right-string". Here,
    20            # "left-string" is "where column" and "right-string" is "= value".
    21            if ($i eq 0) {
    22                $left = sprintf("where %s", $c[$i]);
    23                $j = $i;
    24            } else {
    25                $left = sprintf("and %s", $c[$i]);
    26            }
    27            $right = $v[$j];
    28    
    29            # Check the right-string and adjust it according to its value. If
    30            # it has "to_date" function, then increment the right counter and append
    31            # the next element.
    32            if ($right =~ m/null/i) {
    33                $right = sprintf(" is %s", $right);
    34            } elsif ($right =~ m/to_date/i) {
    35                $j++;
    36                $right = sprintf(" = %s,%s", $right, $v[$j]);
    37            } else {
    38                $right = sprintf(" = %s", $right);
    39            }
    40            $j++;
    41            $qry .= sprintf("\n%s%s", $left, $right);
    42        }
    43    
    44        # We're done. Print the query and move on to the next line from file.
    45        $qry .= ";\n";
    46        printf("\n%s", $qry);
    47    }
    48    close(FH) or die "Can't close $file: $!";
    49    
$ 
$ perl generate_qry.pl statements.sql

select * from table_name
where col1 = 6752
and col2 = 14932156
and col3 = 24
and col4 = 'ALL'
and col5 = 'Staff'
and DATE1 = to_date('04/17/2017 00:00:00','MM/DD/YYYY HH24:MI:SS')
and DATE2 = to_date('05/24/2017 23:59:59','MM/DD/YYYY HH24:MI:SS')
and col6 = 'aso 4'
and col7 = 'device'
and col8 = 148
and col9 is null
and col10 is null
and col11 is null;

select * from emp
where empno = 21
and ename = 'Adrian'
and job = 'Salesman'
and sal = 1000
and hiredate = to_date('01/01/2017 00:00:00','MM/DD/YYYY HH24:MI:SS')
and mgr = 1
and deptno = 42;

select * from tname
where col1 = to_date('01/01/2017 00:00:00','MM/DD/YYYY HH24:MI:SS')
and col2 = to_date('02/01/2017 00:00:00','MM/DD/YYYY HH24:MI:SS')
and col3 = to_date('03/01/2017 00:00:00','MM/DD/YYYY HH24:MI:SS')
and col4 = to_date('04/01/2017 00:00:00','MM/DD/YYYY HH24:MI:SS');

select * from temp
where col1 is null
and col2 is null
and col3 is null
and col4 is null;
$ 
$

 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

File status - open/close ??

Is there any commands in UNIX, like isopen(),isclose() to know whether a file has been opened for read/write purpose.? Thanks (3 Replies)
Discussion started by: Pal
3 Replies

2. UNIX for Dummies Questions & Answers

Should a UNIX daemon process close open fds?

I have a UNIX daemon process that's been started by a parent process, an application server. The behavior of this daemon process is to inherit and use the app server's file descriptors (ports/sockets). When I shutdown the app server, the daemon continues to run, because there may be other... (1 Reply)
Discussion started by: kunalashar
1 Replies

3. Solaris

close open files before remove

Hi I have a script to remove log files when it reaches %70 usage in order to descending order of dates. But sometimes it happens to remove open - being processes files , so makes them unlinked from the directors and results them disappeared. How can I solve this problem , simple scenario I... (9 Replies)
Discussion started by: xramm
9 Replies

4. UNIX for Advanced & Expert Users

Number of open files (Dir's) how to close?

I am having a client/server application which will download files from server. If server has directories, it will create directories/sub directories and then download files. In this process, I observed that number of open files are more than 400 (which is approxmately same as number of dir/subdir... (1 Reply)
Discussion started by: satyam90
1 Replies

5. Windows & DOS: Issues & Discussions

Can DOS close an open file?

I'm trying to setup a cron job for my brother that goes out to the web and retrieves an excel file and overwrites the existing copy on his desktop. The problem I'm facing is I have to kill the process (excel.exe) if the file is open while the batch file runs, otherwise, it will create another copy... (2 Replies)
Discussion started by: quattro20v
2 Replies

6. Solaris

file open/read/write/close/access by process

Hi want to know what file (descriptor+filename+socket) is being accessed by particular process on solaris. Purpose : while running perf. test, needs to find where is the bottleneck. We are providing concurrnet load for around 1 hr and needs to capture data related to file usage pattern... (1 Reply)
Discussion started by: raxitsheth
1 Replies

7. Shell Programming and Scripting

Cleanup between parenthesis

Hi, I am trying to clean up data between parenthesis () in a file. See example below.... Input File : (New York) Chicago (London) New York (Chicago) London New York Chicago (London) (New York) (Chicago) (London) New York (Chicago) ... (3 Replies)
Discussion started by: msalam65
3 Replies

8. Programming

when parent process close, how to close the child?

can someone provide an example, where if the parent process quits for any reason, then the child process will also close? (3 Replies)
Discussion started by: omega666
3 Replies

9. IP Networking

Open/close of ports

Hi, I have read some forum theads about the open and close ports. some points are clear and it is not working on my machine or something am i missing? I have commented out a port /etc/services, one application uses then when i use the telnet <hostname> <port_blocked> it shows connected..... (1 Reply)
Discussion started by: balamv
1 Replies

10. Shell Programming and Scripting

Work with setsid to open a session and close it correctly

I would like to create the following script: run a python script with setsid python may or may not fail with exception check if all of the group processes were terminated correctly if not, kill the remaining processes How can I do that? Thanks a lot (3 Replies)
Discussion started by: ASF Studio
3 Replies
All times are GMT -4. The time now is 02:03 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy