Link to home
Start Free TrialLog in
Avatar of fosiul01
fosiul01Flag for United Kingdom of Great Britain and Northern Ireland

asked on

perl script to spilit data

Hi
Please have a look at simple perl code

the output is like this

mysql     1041  0.3  4.3 141588 22604 ?        Sl   09:15   0:50 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --user=mysql --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/run/mysqld/mysqld.sock --port=3306
www-data   904  0.0  2.2  41124 11660 ?        S    09:15   0:01 /usr/sbin/apache2 -k start
www-data  2739  0.0  2.2  41008 11344 ?        S    10:14   0:00 /usr/sbin/apache2 -k start
www-data  2774  0.0  2.0  39972 10308 ?        S    10:15   0:00 /usr/sbin/apache2 -k start
www-data   911  0.0  1.9  39972 10044 ?        S    09:15   0:00 /usr/sbin/apache2 -k start
www-data   910  0.0  1.9  39972 10044 ?        S    09:15   0:00 /usr/sbin/apache2 -k start
www-data   909  0.0  1.9  39972 10044 ?        S    09:15   0:00 /usr/sbin/apache2 -k start
www-data   906  0.0  1.9  39712 10076 ?        S    09:15   0:00 /usr/sbin/apache2 -k start
www-data  2754  0.0  1.9  39972  9976 ?        S    10:15   0:00 /usr/sbin/apache2 -k start
www-data  2753  0.0  1.9  39716 10052 ?        S    10:15   0:00 /usr/sbin/apache2 -k start
www-data  2742  0.0  1.8  39716  9720 ?        S    10:14   0:00 /usr/sbin/apache2 -k start
root       827  0.0  1.7  37492  9128 ?        Ss   09:14   0:04 /usr/sbin/apache2 -k start
root@IT-wiki:~/perl#


(a) I need to get the 3rd,4th and 11th column in 3 variables

(b) then put those 3 variables into a database..

can any one please help me ...
thanks


#/usr/bin/perl



open (PS_O, "ps aux | sort -nr -k 4 | head -12 |");
#open (PS_O, "ps auxf | ");

#print STDOUT <PS_O>;
while ($line = <PS_O>){

print  "$line";


}

Open in new window

Avatar of farzanj
farzanj
Flag of Canada image

Since you are using shell and pipes, you could get the relevant columns by doing one more pipe like
 | awk '{print $3" "$4" "$11}'
Avatar of fosiul01

ASKER

hi thanks

but bellow lines does not work

open (PS_O, "ps aux | sort -nr -k 4 | head -12 | awk '{print "$3" "$4" "$11"} '");


its throws too much error
ok this line does not throuw any error

open (PS_O, "ps aux | sort -nr -k 4 | head -12 | awk '{print $3 $4 $11}'");


but it does not show any oput ..
Did you try this:
open (PS_O, "ps aux | sort -nr -k 4 | head -12 | awk '{print "$3\" \"$4\" \"$11} ' | ");
ok i did this

open (PS_O, "ps aux | sort -nr -k 4 | head -12 | awk '{print \"$3\" \"$4\" \"$11} ' | ");

but it saying

awk: line 1: runaway string constant "}  ...
ASKER CERTIFIED SOLUTION
Avatar of FishMonger
FishMonger
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
open (PS_O, "ps aux | sort -nr -k 4 | head -12 | awk '{print \"$3\" \"$4\" \"$11} ' | ");

is missing the \" after $11 and before the }