efficiency..


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting efficiency..
# 8  
Old 04-28-2004
Quote:
Originally posted by RTM
Must have been this:
$userinfo=`/usr/bin/grep $id /etc/passwd`;

So how do you do it in Perl ? I haven't done that much in Perl and found it rather weird that it always seemed that you had to use UNIX commands to get info - I have Perl scripts that I could have done in ksh and only kept in Perl for a bit of understanding of it.
this is how i would do it.
just so you know tho perl has a built in grep function.

Code:
#!/usr/bin/perl -w

while( <STDIN> ) {
chop ($_);
($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$dir,$shell) = getpwnam("$_");
print "uname:$name uid:$uid home:$dir\n";
}

you can prolly slim it down to (getpwnam("$_"))[0,2,7];


but generally:

You have a couple of ways of importing the data. (provided perl dosent have a built in function to read the file as in this example) One of the most popular is loading the data into a control structure (array/hash/combo) or working right off the file handle.

so let say you have x file and you need to work with it. you can do something like this (from the perl cookbook)

Code:
This is from the perl cookbook. ties a file to an array.

use Tie::File;
use Fcntl;

tie(@lines, Tie::File, $FILE, mode => O_RDWR) or die "Cannot tie file $FILE: $!\n";


OR you can do something like this. not from the cook book.
open(FILEHANDLE,"filename") or die;
while (<FILEHANDLE>) {do something};

RTM did that help at all? if not toss me some stuff and i will try to explane it better. i am rather fortunet and have a variety of perl books on hand along w/ the latest cd bookself so i should be able to answer a fair amount for ya.

Last edited by Optimus_P; 04-28-2004 at 12:58 PM..
# 9  
Old 04-28-2004
Optimus P - Cool. Thanks for the info. I think I have used something like it once, but didn't recall it. I'll try to remember for next time (actually doing any scripts has been so few and far between for the last 2 years it's hard to remember how to do much of anything any more).

Perderabo - fyi - the person was looking for a uid not account name (
see post )

Last edited by RTM; 04-28-2004 at 02:10 PM..
# 10  
Old 04-28-2004
Quote:
Originally posted by RTM
Perderabo - fyi - the person was looking for a uid not account name
Same deal applies to uid. I got 3 lines with:
grep 300 /etc/passwd
but only one line with
grep '^.*:.*:300:' /etc/passwd
# 11  
Old 04-28-2004
Quote:
Originally posted by Perderabo
Same deal applies to uid. I got 3 lines with:
grep 300 /etc/passwd
but only one line with
grep '^.*:.*:300:' /etc/passwd
but in the context of your search patter the user would not be entering in the first 3 fields of the password filed they woudl only be entering in a username i believe.

no matter tho we are getting way off the topic of efficiency
# 12  
Old 04-28-2004
Well, ok...

id=300
grep '^.*:.*:'${id}':' /etc/passwd

The point is that you need to search in such a way that you get what you're looking for.

And RTM says it is uid, not user name.
Login or Register to Ask a Question

Previous Thread | Next Thread

5 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

About efficiency of parallel memory allocation

Hello, there. I'm a new beginner to Linux kernel and curious about its memory management. When multiple applications apply for memory space at the same time, how Linux kernel solve the resource contending problem for high performance? I have known that there is a buddy system for allocating and... (4 Replies)
Discussion started by: blackwall
4 Replies

2. Shell Programming and Scripting

File or Folder Efficiency?

I've got this program set up so that it creates files whose unique names specify the jobs their contents describe. In order to retrieve the information inside those files, I have to do a "grep" and awk or sed to extract it. I've just assumed that making a directory with that unique name that... (1 Reply)
Discussion started by: gmark99
1 Replies

3. Shell Programming and Scripting

The efficiency between GREP and SED???

Hello Everyone! I am a newbie. I'd like to get key lines from a big txt file by Reg Exp, The file is nearly 22MB. GREP or SED?which may be the best choice,more efficient way? or any other best practise? Thank you in advance. Ever:) (5 Replies)
Discussion started by: ever
5 Replies

4. Shell Programming and Scripting

Improve program efficiency (awk)

Hi !! I've finished an awk exercise. Here it is: #!/bin/bash function calcula { # Imprimimos el mayor tamaño de fichero ls -l $1 | awk ' BEGIN { max = $5; # Inicializamos la variable que nos guardará el máximo con el tamaño del primer archivo } { if ($5 > max){ #... (8 Replies)
Discussion started by: Phass
8 Replies

5. Shell Programming and Scripting

Perl: code efficiency for gmtime

I have the following Perl snippet: # get datetime @dt = gmtime(); $strdate = 1900 + $dt . addleadingzero(++$dt) . addleadingzero($dt) . addleadingzero($dt) . addleadingzero($dt) . addleadingzero($dt); # write to file $outfile = $strdate . ".txt"; getstore($url, $outfile) or die "Error:... (3 Replies)
Discussion started by: figaro
3 Replies
Login or Register to Ask a Question