![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#8
|
|||
|
|||
|
Quote:
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
|
| Forum Sponsor | ||
|
|
|
#9
|
|||
|
|||
|
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. |
|
#10
|
|||
|
|||
|
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;
|
|
#11
|
|||
|
|||
|
have you even tried the awk script ? ( or the Perl one?)
|
|
#12
|
|||
|
|||
|
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}'
|
|
#13
|
|||
|
|||
|
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 For your reference, u have attached the my exeution steps. ========== Thanks, Karthikeyan. ========== |
|
#14
|
|||
|
|||
|
Quote:
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:$ |
|||
| Google The UNIX and Linux Forums |