Sponsored Content
Top Forums Shell Programming and Scripting how to print out data from mysql table in perl script Post 302482466 by kpddong on Wednesday 22nd of December 2010 02:06:08 PM
Old 12-22-2010
how to print out data from mysql table in perl script

I'm having trouble with this code.

if i do

.\read.pl -u user

it prints out

2010-12-20 12:00:00 host1 <cmd>a
2010-12-20 12:00:01 host1 <cmd> <execute>
2010-12-20 12:00:02 host1 <cmd>b
2010-12-20 12:00:03 host1 <cmd>c

however, if i enter
.\read.pl -h host1

it should print out
2010-12-20 12:00:00 user <cmd>a
2010-12-20 12:00:01 user <cmd> <execute>
2010-12-20 12:00:02 user <cmd>b
2010-12-20 12:00:03 user <cmd>c

and when i use this option
.\read.pl -u user -h host1

it prints out
2010-12-20 12:00:00 host1 <cmd>a
2010-12-20 12:00:01 host1 <cmd> <execute>
2010-12-20 12:00:02 host1 <cmd>b
2010-12-20 12:00:03 host1 <cmd>c

when i want it to print out
2010-12-20 12:00:00 <cmd>a
2010-12-20 12:00:01 <cmd> <execute>
2010-12-20 12:00:02 <cmd>b
2010-12-20 12:00:03 <cmd>c

can someone help me? can i not use the !defined() method? Thanks.

Code:
 
# DBI connection statement
my $dbh = DBI->connect("DBI:mysql:$database:$host", $user, $pw, {RaiseError=>1}) or die $DBI::errstr;
# Command line options
our ($opt_u, $opt_h, $opt_s, $opt_e, $opt_c);
getopt('u:h:s:e:c');
# Different queries
my $query;
my $sth;
if (defined($opt_u) && !defined($opt_h && $opt_s)) {
$query = ('SELECT time, host, cmd FROM '.$table.' WHERE user = "'.$opt_u.'"');
$sth = $dbh->prepare($query);
$sth->execute();
while (@ary = $sth->fetchrow_array()) {
foreach(@ary) {
print "$_ ";
}
print "\n";
}
}
elsif (defined($opt_h) && !defined($opt_u && $opt_s)) {
$query = ('SELECT time, user, cmd FROM '.$table.' WHERE host = "'.$opt_h.'"');
$sth = $dbh->prepare($query);
$sth->execute();
while (@ary = $sth->fetchrow_array()) {
foreach(@ary) {
print "$_ ";
}
print "\n";
}
}
elsif (defined($opt_u && $opt_h) && !defined($opt_s)) {
$query = ('SELECT time, cmd FROM '.$table.' WHERE user = "'.$opt_u.'" AND host = "'.$opt_h.'"');
$sth = $dbh->prepare($query);
$sth->execute();
while (@ary = $sth->fetchrow_array()) {
foreach(@ary) {
print "$_ ";
}
print "\n";
}
}

---------- Post updated 12-22-10 at 02:06 PM ---------- Previous update was 12-21-10 at 02:44 PM ----------

can someone help please

Last edited by pludi; 12-23-2010 at 08:39 AM..
 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help Inserting data in mysql table

Cant understand the error #!/bin/bash temp="" A="" D=$(date +"%Y-%m-%d") H=$(date +"%R") temp=$(wget -q -O - website | grep -o "Temperature:]**" | grep \-E -o "+") mysql -D "weather_wise" -e "INSERT INTO weather (Date, Hour, Degrees) VALUES ($D,$H, $temp)"; my data types for... (11 Replies)
Discussion started by: vadharah
11 Replies

2. Shell Programming and Scripting

How to scan data directly from Table using a script

Hi, I have a new task where i have two tables Acct_ open and lookup table In the Acct_open table there is all the information about an account including account number and lookup table is having a country code and corresponding country name where that account has been opened. both... (2 Replies)
Discussion started by: manmeet
2 Replies

3. Shell Programming and Scripting

Perl Script for reading table format data from file.

Hi, i need a perl script which reads the file, content is given below. and output in new file. TARGET DRIVE IO1 IO2 IO3 IO4 IO5 ------------ --------- --------- --------- --------- --------- 0a.1.8 266 236 ... (3 Replies)
Discussion started by: asak
3 Replies

4. Shell Programming and Scripting

Help with perl script to output data in table format...

Hello, I need help with a perl script that will process a text file and match virtual server name to profile(s). the rest will be ignored. Virtual server name follows the word "virtual" in the begging of the line. There could be multiple profiles assigned to one virtual server. For example, ... (3 Replies)
Discussion started by: besogon
3 Replies

5. Shell Programming and Scripting

Shell script for adding a table in mysql with 10,000 lines ... pls help

Hi , I am new to shell scripting . I need to write a shell script in sql to add 10,000 lines of data in a table . Pls help guys :) ---------- Post updated at 07:08 PM ---------- Previous update was at 03:40 PM ---------- guys please help !!! (3 Replies)
Discussion started by: vinumahalingam
3 Replies

6. Shell Programming and Scripting

Input data of a file from perl into HTML table

Hi , I need an help in perl scripting. I have an perl script written and i have an for loop in that ,where as it writes some data to a file and it has details like below. cat out.txt This is the first line this is the second line. .....Now, this file needs to be send in mail in HTML... (2 Replies)
Discussion started by: scott_cog
2 Replies

7. Shell Programming and Scripting

Need to Print output in table using shell script

#! /bin/ksh #] && . ./.profile 2>/dev/null if test -f '.profile'; then . ./.profile; fi; #. .profile LOG_DIR=/app/rpx/jobs/scripts/just/logs sendEmail() { pzCType="$1"; pzTitle="$2"; pzMsg="$3"; pzFrom="$4"; pzTo="$5"; pzFiles="$6"; pzReplyTo="$7" ( ... (4 Replies)
Discussion started by: ankit.mca.aaidu
4 Replies

8. Programming

MYSQL merge csv data with exisiting table

I have a MYSQL table with demographic data. The addresses contained in the table were entered manually before the advent of online postcode lookup software and, hence, there are a lot of errors when using automated online mailing checking software. I plan to export the data as csv file for a 3rd... (1 Reply)
Discussion started by: barrydocks
1 Replies

9. Shell Programming and Scripting

Send Data to MySQL Table Columns

I have two scripts, each script reads an individual data file and copies specific lines of data and sends to MySQL table. Only difference is, each script sends data to a separate column on the same DB. I want to use one script to populate DB table and have data look horizontal, with no overlapping.... (3 Replies)
Discussion started by: SysAdminRialto
3 Replies
Statistics::Basic::Median(3pm)				User Contributed Perl Documentation			    Statistics::Basic::Median(3pm)

NAME
Statistics::Basic::Median - find the median of a list SYNOPSIS
Invoke it this way: my $median = median(1,2,3); Or this way: my $v1 = vector(1,2,3); my $med = median($v1); And then either query the values or print them like so: print "The median of $v1: $med "; my $mq = $med->query; my $m0 = 0+$med; Create a 20 point "moving" median like so: use Statistics::Basic qw(:all nofill); my $sth = $dbh->prepare("select col1 from data where something"); my $len = 20; my $med = median()->set_size($len); $sth->execute or die $dbh->errstr; $sth->bind_columns( my $val ) or die $dbh->errstr; while( $sth->fetch ) { $med->insert( $val ); if( defined( $m = $med->query ) ) { print "Median: $m "; } # This would also work: # print "Median: $med " if $med->query_filled; } METHODS
new() The constructor takes a single array ref or a single Statistics::Basic::Vector as arguments. It returns a Statistics::Basic::Median object. Note: normally you'd use the median() constructor, rather than building these by hand using "new()". _OVB::import() This module also inherits all the overloads and methods from Statistics::Basic::_OneVectorBase. OVERLOADS
This object is overloaded. It tries to return an appropriate string for the calculation or the value of the computation in numeric context. In boolean context, this object is always true (even when empty). AUTHOR
Paul Miller "<jettero@cpan.org>" COPYRIGHT
Copyright 2012 Paul Miller -- Licensed under the LGPL SEE ALSO
perl(1), Statistics::Basic, Statistics::Basic::_OneVectorBase, Statistics::Basic::Vector perl v5.14.2 2012-01-23 Statistics::Basic::Median(3pm)
All times are GMT -4. The time now is 03:07 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy