Perl Grep Error - Possible Syntax?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl Grep Error - Possible Syntax?
# 1  
Old 03-15-2010
Perl Grep Error - Possible Syntax?

Alrighty, I'm trying to get a perl script going to search through a bunch of files for me and compile it to a single location. I am currently having troubles on just getting the grep to work.

Here is what I currently have:
Code:
#!/usr/bin/perl
open (LOG, "errors.txt") or die
 ("Unable to open error.txt");
@errors=<LOG>;
close(LOG);
$errors2 = grep Error,@errors;
foreach ($errors2)
{
chomp($_);
print "$_";
print "\n";
}

Here is the current output:
Code:
$ ./perl.sh
7
$

Here is the contents of errors.txt
Code:
$ more errors.txt
Error1
Error2
Error3
Makeshift Error
Makeshift
Pluto
jerror

For some reason it's doing a line count. Which is my next thing I wanted to do with it. I want to line count the results to see to even write them, or just skip over writing that section of my perl script.

I'm not too sure if I'm making much sense right now, but I'm just trying re-relearn perl. More of a brush up.

I've tried:
Code:
 
open (LOG, "errors.txt") or die
 ("Unable to open error.txt");
@errors=<LOG>;
close(LOG);
$errors2 = `grep Error errors.txt`;
foreach ($errors2)
{
chomp($_);
print "$_";
print "\n";
}

Which works fine as well. Then when I try to line count $errors2, it wants to line count the file Error1, Error2, etc.

But when I try to
Code:
$errors2 = `grep Error @errors`;

it tries to grep for the word Error in file Error1,Error2,etc. So that doesn't work either.


Any chance another set of eyes could see what I am doing wrong? Thank you for your time. I greatly appriciate it.
# 2  
Old 03-15-2010
MySQL

You made a minor mistake here ,

You tried to assign the result of the grep to the scalar ( $errors2) . Because of it it you get the count of the occurrence . Just change that as an array.

Code:
#!/usr/bin/perl

use strict;
use warnings;

open (LOG, "errors.txt") or die ("Unable to open error.txt");
my @errors=<LOG>;
close(LOG);
my @errors2 = grep /Error/,@errors;
foreach (@errors2)
{
chomp($_);
print "$_";
print "\n";
}

# 3  
Old 03-15-2010
Ahh...

Thank you so much!

Could you breifly explain the purpose of the forward slashes around the Error so I may better understand?

Code:
my @errors2 = grep /Error/,@errors;

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

FIND and GREP syntax

I have a question to this command find . -type f -name ".*txt" -exec grep "text" {}\. The find command will locate a file name with the extension of txt once per round and find the word "text" in the content of the file or the find command will locate all the file names with the extension of... (2 Replies)
Discussion started by: TestKing
2 Replies

2. Shell Programming and Scripting

Help on grep syntax in UNIX

Dear Team /app/Appln/logs/ echo Session used server are 'grep -i pid|grep -i session | cut -d'.' -f1 | awk '{print $9}' | sort | uniq' Output - lxserver01 lxserver02 lxserver03 When I grep session pid in logs server details I can see above distinct server details but I... (6 Replies)
Discussion started by: skp
6 Replies

3. Programming

Syntax error in perl program.

Hi, i am running this code but i am getting syntax error #!/usr/bin/perl use warnings; use strict; use XML::LibXML; use XML::LibXML::Reader; use Data::Dumper; my $file; open( $file, 'DTC_Specification_transformed.xml'); my $reader = XML::LibXML::Reader->new( IO => $file ) or die... (1 Reply)
Discussion started by: veerubiji
1 Replies

4. Shell Programming and Scripting

Grep syntax print after certain character

My current code is: user@ubuntu:~/Desktop$ grep -e "\(packaged by\)\|\(employee\)\|\(file name\)\|\(Total Data (MB) Read\)\|\(Begin Time\)" log.txt packaged by = Ron Mexico employee = Michael Vick file name = Mike_Vick_2011.bat Total Data (MB) Read: 11.82 Begin Time: 6/13/2011... (8 Replies)
Discussion started by: chipperuga
8 Replies

5. UNIX for Dummies Questions & Answers

Find/Grep Syntax Question

Hi Folks, I am trying to dig through about 100 directories that have 1 or 2 .jpg images stored in each. I want to copy the .jpg to another file in the root directory. Really my ultimate goal is not to have to dig down into each directory to copy the images individually. I thought I could use a... (2 Replies)
Discussion started by: alpinescott
2 Replies

6. UNIX for Dummies Questions & Answers

| help | unix | grep (GNU grep) 2.5.1 | advanced regex syntax

Hello, I'm working on unix with grep (GNU grep) 2.5.1. I'm going through some of the newer regex syntax using Regular Expression Reference - Advanced Syntax a guide. ls -aLl /bin | grep "\(x\)" Which works, just highlights 'x' where ever, when ever. I'm trying to to get (?:) to work but... (4 Replies)
Discussion started by: MykC
4 Replies

7. BSD

proper syntax of grep command

I'm learning UNIX on my mac (BSD), using a manual. I'm trying to figure out the grep command, and am getting something wrong. I've opened one of my files in NeoOffice and am looking for a string, the phrase 'I am writing.' I've been to some sites to get the proper syntax, and from what I can see... (5 Replies)
Discussion started by: Straitsfan
5 Replies

8. Shell Programming and Scripting

Perl Script Syntax error in version 4

Hi , I use the following simple perl script to find the yesterday time perl -e ' use POSIX(strftime); print POSIX::strftime("%a %b %e %H:%M:%S %Y", localtime(time-86400*$ARGV))' 1 However in the perl version 4 , it gives me the following error : Do the perl version 4 does not support... (4 Replies)
Discussion started by: youareapkman
4 Replies

9. Shell Programming and Scripting

grep syntax for this...

I wanna grep for a pattern logs 1 2 & 3 within a folder containing 100 logs grep "test" /folder/log1 /folder/log2 /folder/log3 The above command will work fine but is there any command like grep "test" /folder/log1, log2, log3 or something similar (4 Replies)
Discussion started by: roshanjain2
4 Replies

10. Shell Programming and Scripting

Need help with the syntax using awk+grep

Hi, I need to extract information from a 4 GB file based on the following conditions: 1) Check for the presence of a set of account numbers Each account number is present along with other information within a PAGESTART and PAGEEND. The file looks like this: PAGESTART ACCOUNT NO 123... (6 Replies)
Discussion started by: kthri
6 Replies
Login or Register to Ask a Question