display result from user input

how can i only display the result according to user input?

For example:
i have a data file(in the attachement), when the user enter "1" it will only display whole records with pid from the data file matches to the users entered.

Try the below code.

echo "Enter the PID :"
read pid
linenum=`cat tes.txt | tr -s " " "|" | cut -d"|" -f2 |grep -n $pid`
cat tes.txt | head -$linenum | tail -1

Its giving the expected results.

==========
Thanks,
Karthikeyan.

hi Karthikeyan_113! i have error when i run the code.

error shown:
head: unrecongonized option `-:'

Does it have to be awk/ksh? Can you use perl?

awk 'BEGIN{
 printf "%s : " ,"What Pid"
 getline pid <"-" 
}
$1==pid'  "file"

you could just post your Perl solution. If OP wants a Perl solution, he will use it.

Edit: Do not try this code, it's got a couple of errors. See below for corrected version.

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:

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

# 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


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.

Thanks ghostdog74, I owe you one. Corrected 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;

have you even tried the awk script ? ( or the Perl one?)

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

echo "enter pid"
read code
grep "$code" | awk '{print $0}'

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.

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"

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:$