display result from user input


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting display result from user input
# 8  
Old 01-31-2008
Quote:
Originally Posted by KevinADC
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

# 9  
Old 01-31-2008
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  
Old 01-31-2008
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  
Old 01-31-2008
Quote:
Originally Posted by thms_sum
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?)
# 12  
Old 01-31-2008
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  
Old 01-31-2008
MySQL 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.
==========
# 14  
Old 02-01-2008
Quote:
Originally Posted by Karthikeyan_113
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:$

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to parse file and display result based on text

I am trying using awk to open an input file and check a column 2/field $2 and if there is a warning then that is displayed (variantchecker): G not found at position 459, found A instead. The attached Sample1.txt is that file. If in that column/field there is a black space, then the text after... (6 Replies)
Discussion started by: cmccabe
6 Replies

2. UNIX for Dummies Questions & Answers

Japanese layout input. Help to get result with uim? (X)

It's easy but I'm searching how to work with fonts (besides) emerging them. /etc/conf.d/consolefont (consolefont=" ") - what to write after install, m.b. paths are wrong (emerged /usr/share/fonts/efont-unicode/*.gz). Has it any relation to X11 hieroglyph input?:wall: (1 Reply)
Discussion started by: Xcislav
1 Replies

3. Shell Programming and Scripting

Bash script to display result in table

My script gives the following result. Is it possible to display the same in table format ? 1. rex_best Latest feeds are not avaialable. The last feed was generated on 2012-05-17 File Name = ekb_best_20120517_010949_665.tar.gz The Number of entry elements = 4209539 2. rex_genre Latest... (2 Replies)
Discussion started by: kishorekumar87
2 Replies

4. Shell Programming and Scripting

display Range of date depend on user input php

Hi, i am very new to php Is it possible to display Range of date depend on user input day example: user input 2 day start from 28/4/12 it will add 2 day from date of input so display should look like this 28/4/12 to 30/4/12 then from 30/412 user add another 4 date so will... (0 Replies)
Discussion started by: guidely
0 Replies

5. Shell Programming and Scripting

How to get the user input recursively until the user provides valid input

Hi, echo "Enter file name of input file list along with absolute path : " read inputFileList if then for string in `cat inputFileList` do echo $string done else echo " file does not exist" fi From the above code, if the user enters a invalid file... (1 Reply)
Discussion started by: i.srini89
1 Replies

6. UNIX for Dummies Questions & Answers

How to display values from user input array?

Hi all, I wrote a script that reads inputs from user and store in array named "input". The number of elements in the array is not fixed - determined only after user exit the while loop that reads the array values : x=1 echo "Enter first value" read input while } != "exit" ] do ... (1 Reply)
Discussion started by: luna_soleil
1 Replies

7. Shell Programming and Scripting

csh script to search for files and display result

Hello all. I'm a long time browser, first time poster...Be gentle :-) I don't use csh much, but have been asked to make a small script for our HP-UX systems to search for a report file(s) or summary file and display the result (I was using vuepad, but this probably is just complicating... (4 Replies)
Discussion started by: neillsm
4 Replies

8. Shell Programming and Scripting

Pick up the return code for every iteration and display the result only once in loop.

Hi All, I amlearning UNIX scripting. I have a small query. I would be thankful if any one helps me out. I have a below piece of code which delets the files. If file dosent have the permissions to delete a particular file I have used 2>>operator to track the error code. But my objective is... (1 Reply)
Discussion started by: manas6
1 Replies

9. UNIX for Dummies Questions & Answers

display the result of wc -l with words before and after the result

hello showrev -p | wc -l returns: 381 What to do in case I want to have this output: number of lines returned by showrev -p is: 381 thx (3 Replies)
Discussion started by: melanie_pfefer
3 Replies

10. UNIX for Dummies Questions & Answers

Display result one page at a time

hi how can i display the result on the screen one page at a time? say i search for .txt files and return the result on the screen one page at a time. (4 Replies)
Discussion started by: nickaren
4 Replies
Login or Register to Ask a Question