Grepping in Perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grepping in Perl
# 1  
Old 11-04-2014
Bug Grepping in Perl

Hello Friends,



I have a password file at /etc/password.bak in which the fields are present in the below format i.e. each field is seperated by ":"

Code:
vbjr3:x:1007:1007:student users:/home/vbjr3:/bin/bash
dbhyt2:x:1008:1008:student users:/home/dbhyt2:/bin/bash
bbwe3:x:1009:1009:student users:/home/bbwe3:/bin/bash
abadt7:x:1010:1010:student users:/home/abadt7:/bin/bash
hascr3:x:1011:1011:student users:/home/hascr3:/bin/bash
pcgre2:x:1012:1012:student users:/home/pcgre2:/bin/bash
scscv21:x:1013:1013:student users:/home/scscv21:/bin/bash
sdasdm3:x:1014:1014:student users:/home/sdasdm3:/bin/bash

the first field here is the username.

Now we are at another location and i need to run a perl script which could take the user as argument and print the fifth and sixth fields from the password.bak file from /etc directory.

My Code:
Code:
#!/usr/bin/perl

use strict;
use warnings;

my $user = shift or die "Usage: $0 <username>\n";

my $db = "/etc/passwd.bak";
my ($home, $shell);

open $fh, "<", "$db" or die "Could not open $db: $!\n";

while ( $account = <$fh> ) {
   ($home, $shell) = $account =~ m/$user:x:\d+:\d+:(.*)$/ and last;
}

close $fh;


if ( $home and $shell ) {
    print "Home Directory: $home\n";
    print "Shell Used: $shell\n";
}
else {
    print "Account $user not found\n";
}

But this code exits with errors.

Could anyone please help me out here Smilie

Last edited by Ravi Tej; 12-01-2014 at 05:36 AM..
# 2  
Old 11-04-2014
Just a guide

Code:
#!/usr/bin/perl

use strict;
use warnings;

# fail if not argument is given
my $user = shift or die "Usage: $0 <username>\n";

# file path to use
my $db = "/etc/passwd.bak";
my ($home, $shell);

# open the db file for reading or end the program
open my $fh, "<", "$db" or die "Could not open $db: $!\n";

# read a line by line until it finds the user
while ( my $account = <$fh> ) {
   # extract account information if found and end loop
   ($home, $shell) = $account =~ m/$user:x:\d+:\d+:.*:(.*):(.*)$/ and last;
}

# dismiss the open file 
close $fh;

# condition to display results
if ( $home and $shell ) {
    print "Home Directory: $home\n";
    print "Shell Used: $shell\n";
}
else {
    print "Account $user not found\n";
}


Last edited by Aia; 11-04-2014 at 10:38 PM.. Reason: Adding missing "
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grepping more than one word

Dear Experts, Need your help. Typically we use "grep" to search and display a pattern in a txt file. However, here what we want is, we want to grep a line which contains 4 words any where in a line. For example. File has 10,000,000 lines in it out of which there is a particular line which... (1 Reply)
Discussion started by: anushree.a
1 Replies

2. UNIX for Dummies Questions & Answers

Grepping using -w and dashes (-)

I have a script to sort a list of arbitrary hosts and determine if they are supported by grepping them into a master supported list. I cut all the suffixes of the hosts in the arbitrary list, leaving the "short" hostname if you will, then grep -w them into the master list. For example: ... (1 Reply)
Discussion started by: MaindotC
1 Replies

3. Shell Programming and Scripting

grepping by digit

Hi all, Need your help here. I have a file with thousand of lines, as shown in example below KDKJAA 98324 OIDSAJ 324 KJAJAK 100 KJKAJK 89 JOIJOI 21 JDKDJL 12 UOIUOD 10 UDUYDS 8 UIUHKK 6 I would like to grep using... (5 Replies)
Discussion started by: masterpiece
5 Replies

4. Shell Programming and Scripting

Please help on grepping

Hi All, I have a log file and I want to parse the logfile with a script.A sample text is shown below: I would grep on "return code " on this file. Any idea how the lines above and below the grep patterns could also be extracted. Thanks! nua7 The runLoggingInstall return code is 0... (3 Replies)
Discussion started by: nua7
3 Replies

5. UNIX for Dummies Questions & Answers

grepping columns

Hi All, I was recently helped out 'big time' with my last post on changing multiple file formats (thx, scott1256ca and bakunin)! My new question is about selecting and displaying columns in a file using (possibly) grep. Several of my data files are spreadsheet format (columns separated by... (8 Replies)
Discussion started by: ScKaSx
8 Replies

6. Shell Programming and Scripting

Grepping issue..

I found another problem with my disk-adding script today. When looking for disks, I use grep. When I grep for the following disk sizes: 5242880 I also pick up these as well: 524288000 How do I specifically pick out one or the other, using grep, without resorting to the -v option? ... (9 Replies)
Discussion started by: LinuxRacr
9 Replies

7. Shell Programming and Scripting

grepping around

Using shell scripts, I use grep to find the word “error” in a log file: grep error this.log. How can I print or get the line 3 lines below the line that word “error” is located? Thanks in advance for your response. (9 Replies)
Discussion started by: cbeauty
9 Replies

8. UNIX for Dummies Questions & Answers

Grepping for strings

Hello. I have a dir of 1500+ dir. In these dirs is a file host, with a tag <x_tag>. I need to : 1. grep for all dir that contain this host file that contain <x_tag> 2. print a list of these host files containing <x_tag> is this better to egrep this? (5 Replies)
Discussion started by: t4st33@mac.com
5 Replies

9. UNIX for Dummies Questions & Answers

grepping for a sentence

Can you grep for a sentence. I have to search logs everyday at work and I was wondering if I could search for a string of words instead of just one. for example, if I had to find this sentence: "Received HTTP message type" How would I grep it (2 Replies)
Discussion started by: eloquent99
2 Replies

10. UNIX for Dummies Questions & Answers

grepping

Is there a way to grep for something and then print out 10 lines after it. for example if I want to grep for a word, then output the following 10 or whatever number of lines after the word. (5 Replies)
Discussion started by: eloquent99
5 Replies
Login or Register to Ask a Question