How do I get the same results in Perl as in csh


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How do I get the same results in Perl as in csh
# 1  
Old 06-06-2007
How do I get the same results in Perl as in csh

In csh I can do this

echo `ps -ef | grep root | awk '{print $2}'`

how would I get the same results using perl.

Thanks
# 2  
Old 06-06-2007
Code:
ps -ef | perl -e 'while ( <> ) { @tmp=split; print "$tmp[1]\n" if /.*root.*/;} '

# 3  
Old 06-06-2007
Thanks John,

But I didn't clarify that I want this in a perl script not command line.
This would include the ps -ef in the perl script.

Brad
# 4  
Old 06-06-2007
This sounds like homework... that is not allowed on the forums
Code:
#!/usr/bin/perl
      @tmp=`ps -ef`; 
      foreach $val ( @tmp ){ 
      	 $_ = $val;
      	 if (/.*root.*/)
      	 {@ps=split; 
      	  print "$ps[1]\n"; 
      	 } 
      }

It should not matter where "ps -ef" comes from.
# 5  
Old 06-06-2007
It's not homework Jim.
It's just me trying to understand Perl.

Thanks for your help.

Brad
# 6  
Old 06-06-2007
To me it seems Perl is alot more convoluted than it is help
if your script is heavily entrenched with unix commands.

Whats the advantage of all the extra code when you can do the samething
with one line of code?
Is the perl code really that great of an advantage in
this case?

Thanks
# 7  
Old 06-06-2007
Perl is a programming language. If you do heavy programming, Perl is more suited than shell scripting. But if all you want to do is parse and/or filter system command output, I will likely do shell scripting alone. For other cases, I will use Perl.

Perl is not convoluted. It is just your exact case that looks convoluted. There are many Perl one-liners floating all around on the Web to tell you it is not necessarily convoluted.

A one-liner that gives the same result as your shell command:
Code:
print map { [split(/\s+/, $_)]->[1] . "\n" } grep { /root/ } split(/\n/, `ps -ef`);

Is it convoluted? I don't think so. Think about doing that in C or Java.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl Csh - setenv ENV change environment variable

I have 3 programs, 1 in perl, 2 in csh: call them perl1, csh1 and run.ol I need perl1 to set csh1 variable NOLOG_qsub = "" I need perl1 to run, run.ol run.ol takes the executable and input and outputs to output run.ol#!/bin/csh -f # run.ol executable input output perl1 should... (1 Reply)
Discussion started by: austinj
1 Replies

2. Shell Programming and Scripting

pass parameters from perl to csh scripts

I use csh a lot but I don't really write csh scripts. Now I have a need to implement a security check (written in perl; verify an user input security code) into a csh script. Here is the senario: #csh 1. call the perl script 2. if the perl script returns 'true', pass on; if the perl... (1 Reply)
Discussion started by: Julian16
1 Replies

3. Shell Programming and Scripting

Perl - save results to output file.

Can any one please help, the code works...I want the output of $result to be saved in an output.txt file which is lcoated in c:\\temp\\output.txt. $filepath="C:\\temp\\ip.txt"; open (HOSTLIST,"$filepath"); @hosts=(<HOSTLIST>); foreach $host(@hosts) { $results = `nslookup... (1 Reply)
Discussion started by: sureshcisco
1 Replies

4. Shell Programming and Scripting

PERL - traverse sub directories and get test case results

Hello, I need help in creating a PERL script for parsing test result files to get the results (pass or fail). Each test case execution generates a directory with few files among which we are interested in .result file. Lets say Testing is home directory. If i executed 2 test cases. It will... (4 Replies)
Discussion started by: ravi.videla
4 Replies

5. Shell Programming and Scripting

Need to grep 2 words from Perl Script results in Terminal....

Hey guys. I have a .pl script that scans my hosts to see if they are down or up. I can run it anytime I want. The script uses a conf file that contains text lines of the IP addresses of the servers. I run the script from the command line of my terminal (MAC OS) I run: sudo ./scanner.pl brings... (3 Replies)
Discussion started by: yoyoyo777
3 Replies

6. Shell Programming and Scripting

Convert from Csh to Perl

Hi , I am trying to convert the below code embedded in a csh script to Perl. Can any expert give me some advice on this ? sed -n ''"$start_line"',$ p' $tester_dir/nfiles_extracted.txt|cut -c1-4,6-|/bin/perl $test_summary/compare_for_all _Duts.pl|sort > $tester_dir/nfiles_extracted1.txt (0 Replies)
Discussion started by: Raynon
0 Replies

7. Shell Programming and Scripting

How to do a formatted substittion in perl with a csh variable?

Hmm quite difficult for me to express my problem in one sentence.Introduction {i am a newbee to csh perl, awk and stuff, and by surfing these forums i managed to build quite a big code using all three of them in a script. Have to admitt, it was not a good idea, and its the perfect example of how... (3 Replies)
Discussion started by: Radamez
3 Replies

8. UNIX for Dummies Questions & Answers

Perl search and replace not working in csh script

I am using perl to perform a search and replace. It works at the command line, but not in the csh shell script perl -pi -e 's@/Pattern@@g' $path/$file I used the @ as my delimiter because the pattern contains "/" (3 Replies)
Discussion started by: NobluesFDT
3 Replies

9. Shell Programming and Scripting

Pass csh variable to Perl

Hi All, I am trying to put the csh variable into a perl. In the below case, i am trying to put the csh variable "var" into my perl code. I tried to use '"$var"' but i don;t think it works. Can anybody help me pls? #!/bin/csh set var = `echo "xxx"` perl myperlcode.pl file ... (9 Replies)
Discussion started by: Raynon
9 Replies

10. Shell Programming and Scripting

Is there any thing like this perl command is csh?

I mean this : perl -pi -e 's/OS/blah/g' *.c* The Great thing in such thing i dont need to rename orig then rename back when i do it with sed for instance inside csh shell , is there any way to avoid this with sed/awk/what ever? Thanks (4 Replies)
Discussion started by: umen
4 Replies
Login or Register to Ask a Question