The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
display the result of wc -l with words before and after the result melanie_pfefer UNIX for Dummies Questions & Answers 3 04-30-2008 04:33 AM
AWK set FILENAME via user input timj123 Shell Programming and Scripting 2 02-24-2008 12:05 PM
awk user input gefa Shell Programming and Scripting 17 10-30-2007 02:01 AM
Getting user input stevefox Shell Programming and Scripting 3 02-15-2007 11:09 PM
Display result one page at a time nickaren UNIX for Dummies Questions & Answers 4 09-09-2003 10:15 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #8  
Old 01-31-2008
Registered User
 

Join Date: Sep 2006
Posts: 1,580
Quote:
Originally Posted by KevinADC View Post
You're right mate. I am not sure what he wants printed out, the whole line or just part of the data, but this prints the whole line associated with the PID number:

Code:
use strict;
use warnings;
print "Enter the PID :" 
my $pid = <STDIN>;
chomp $pid;
open (IN , "tes.txt");
while (<IN>) {
   print if (/^\s*$pid\s/); 
   last;
}
close IN;
exit;
my result, after putting a semicolon at prompt for entering PID
Code:
# more tes.txt
  PID  PPID USER     CMD                         %CPU START STAT
    1     0 root     /sbin/init                   0.0 02:25 Ss
    2     0 root     [kthreadd]
# more test.pl
#!/usr/bin/perl

use strict;
use warnings;
print "Enter the PID :";
my $pid = <STDIN>;
chomp $pid;
open (IN , "tes.txt");
while (<IN>) {
   print if (/^\s*$pid\s/);
   last;
}
close IN;
exit;
# ./test.pl
Enter the PID :1
#
# perl -v

This is perl, v5.8.8 built for i586-linux-thread-multi
Reply With Quote
Forum Sponsor
  #9  
Old 01-31-2008
Registered User
 

Join Date: Jan 2008
Posts: 13
sorry that i ddint specify clearly... is there any way of either using awk or grep to retrive only the result from the user input?

eg. the user enter "1" and it matches from the data file pid column. It will display only the whole line of the matches.
Reply With Quote
  #10  
Old 01-31-2008
Registered User
 

Join Date: Jan 2008
Posts: 328
Thanks ghostdog74, I owe you one. Corrected code:

Code:
use strict;
use warnings;
print "Enter the PID :";
my $pid = <STDIN>;
chomp $pid;
open (IN , "tes.txt") or die "$!";
while (<IN>) {
   if (/^\s*$pid\s/) {
      print;
      last;
   }
}
close IN;
exit;
Reply With Quote
  #11  
Old 01-31-2008
Registered User
 

Join Date: Sep 2006
Posts: 1,580
Quote:
Originally Posted by thms_sum View Post
sorry that i ddint specify clearly... is there any way of either using awk or grep to retrive only the result from the user input?

eg. the user enter "1" and it matches from the data file pid column. It will display only the whole line of the matches.
have you even tried the awk script ? ( or the Perl one?)
Reply With Quote
  #12  
Old 01-31-2008
Registered User
 

Join Date: Jan 2008
Posts: 13
yup!!! i try use awk when i enter "1" or "2" it will display the whole data. Is there any way that i can limit my search to only the entered digit and display only the row

Code:
echo "enter pid"
read code
grep "$code" | awk '{print $0}'
Reply With Quote
  #13  
Old 01-31-2008
Registered User
 

Join Date: Jan 2007
Location: Boston, USA
Posts: 16
Thumbs up Added the exception handling part, try this....

When executing the script for a wrong PID, the error " -: ".

When the varaible linenum is having a null value, we get the above error.

To overcome this, we need put a exception. I have corrected the code.
Please check this.

Code:
echo "Enter the PID :"
read pid
linenum=`cat tes.txt | tr -s " " "|" | cut -d"|" -f2 |grep -n $pid`
if [[ $linenum != '' ]]
then
cat tes.txt | head -$linenum | tail -1
else
echo "Incorrect option... Please try again."
fi
If you get any errors, please send the input file & execution steps.

For your reference, u have attached the my exeution steps.

==========
Thanks,
Karthikeyan.
==========
Attached Files
File Type: txt test.txt (1.9 KB, 8 views)
Reply With Quote
  #14  
Old 02-01-2008
Registered User
 

Join Date: Jan 2008
Posts: 13
Quote:
Originally Posted by Karthikeyan_113 View Post
When executing the script for a wrong PID, the error " -: ".

When the varaible linenum is having a null value, we get the above error.

To overcome this, we need put a exception. I have corrected the code.
Please check this.

Code:
echo "Enter the PID :"
read pid
linenum=`cat tes.txt | tr -s " " "|" | cut -d"|" -f2 |grep -n $pid`
if [[ $linenum != '' ]]
then
cat tes.txt | head -$linenum | tail -1
else
echo "Incorrect option... Please try again."
fi
If you get any errors, please send the input file & execution steps.

For your reference, u have attached the my exeution steps.

==========
Thanks,
Karthikeyan.
==========
Karthikeyan_113, below is wat i get the error when i run the script "head: 2:6563: invalid number of lines"

Code:
ubuntu@ubuntu:$ bash test.sh


  PID  PPID USER     CMD                         %CPU START STAT
 6563  6560 ubuntu   -bash                        0.0 Jan28 S+
 6567  6564 ubuntu   -bash                        0.0 Jan28 S+
 6574  6570 ubuntu   -bash                        0.0 Jan28 S+
 6578  6575 ubuntu   -bash                        0.0 Jan28 S+
 6599  6579 ubuntu   -bash                        0.0 Jan28 S+
 6605  6580 ubuntu   -bash                        0.0 Jan28 S+
 7920  7695 ubuntu   x-session-manager            0.0 Jan28 Ssl
 7987  7920 ubuntu   /usr/bin/ssh-agent x-sessio  0.0 Jan28 Ss
 7994     1 ubuntu   /usr/lib/libgconf2-4/gconfd  0.0 Jan28 S
 7997     1 ubuntu   /usr/bin/gnome-keyring-daem  0.0 Jan28 S
 7999     1 ubuntu   dbus-daemon --fork --print-  0.0 Jan28 Ss
 8001     1 ubuntu   /usr/lib/gnome-control-cent  0.0 Jan28 Sl
 8007  7920 ubuntu   /usr/bin/metacity --replace  0.0 Jan28 S
 8009  7920 ubuntu   gnome-panel --sm-client-id   0.0 Jan28 S
 8012  7920 ubuntu   nautilus --no-default-windo  0.0 Jan28 S
 8016     1 ubuntu   /usr/lib/gamin/gam_server    0.0 Jan28 S
 8019     1 ubuntu   /usr/lib/bonobo-activation/  0.0 Jan28 Ssl
 8020     1 ubuntu   gnome-volume-manager --sm-c  0.0 Jan28 Ss
 8023     1 ubuntu   /usr/lib/gnome-vfs-2.0/gnom  0.0 Jan28 S
 8046  7920 ubuntu   vino-session --sm-client-id  0.0 Jan28 S
 8047  7920 ubuntu   bluetooth-applet             0.0 Jan28 S
 8051  7920 ubuntu   update-notifier              0.0 Jan28 S
 8056     1 ubuntu   /usr/lib/nautilus-cd-burner  0.0 Jan28 S
 8057  7920 ubuntu   /usr/lib/evolution/2.12/evo  0.0 Jan28 Sl
 8061  7920 ubuntu   python /usr/share/system-co  0.0 Jan28 S
 8063  7920 ubuntu   nm-applet --sm-disable       0.0 Jan28 S
 8072     1 ubuntu   gnome-power-manager          0.0 Jan28 Ss
 8088     1 ubuntu   gnome-screensaver            0.0 Jan28 Ss
 8090     1 ubuntu   /usr/lib/gnome-applets/tras  0.0 Jan28 S
 8104     1 ubuntu   /usr/lib/gnome-applets/mixe  0.0 Jan28 Sl
 8108     1 ubuntu   /usr/lib/fast-user-switch-a  0.0 Jan28 S
 8110     1 ubuntu   /usr/bin/python /usr/lib/de  0.0 Jan28 S
 9708     1 ubuntu   /usr/lib/notification-daemo  0.0 Jan28 S
20905     1 ubuntu   gedit file:///home/ubuntu/D  0.1 Jan28 S
24892     1 ubuntu   gnome-terminal               0.0 01:44 Sl
24895 24892 ubuntu   gnome-pty-helper             0.0 01:44 S
24896 24892 ubuntu   bash                         0.0 01:44 Ss
24937 24896 ubuntu   bash myPs.sh                 0.0 01:48 T
26658 24896 ubuntu   bash myPs.sh                 0.0 09:59 S+
26659 26658 ubuntu   bash myPs.sh                 0.0 09:59 S+
26660 26659 ubuntu   ps U ubuntu -o pid,ppid,use  0.0 09:59 R+


Enter the PID :
6563
head: 2:6563: invalid number of lines
ubuntu@ubuntu:$
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
linux, ubuntu

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 06:34 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0