?
Solved

grep line too long

Posted on 2009-12-28
8
Medium Priority
?
2,918 Views
Last Modified: 2013-11-22
I have a log file of 1.8 GB and need to search the lines having "Size of downloaded file".
so i tried and got the below output..

[admin@test]$ grep "Size of downloaded file" nohup.out
2009-12-24 04:40:49 DEBUG - Size of downloaded file test1-deacts_20091224.csv.zip = 20
2009-12-24 04:40:50 DEBUG - Size of downloaded file test2-deacts_20091223.csv.zip = 138533
grep: line too long
0
Comment
Question by:sukhvinderkumar
  • 4
  • 2
  • 2
8 Comments
 
LVL 81

Expert Comment

by:arnold
ID: 26135707
perl might be a better choice

presumably you will either cat the file into a perl script or provide the filename on the command line as an argument to the script.
#!/usr/bin/perl

if ($#ARGV>0 and -f "ARGV[0]") { #an argument is being provided and it is a file
open (FILE, "<ARGV[0]) || die "Unable to open file $ARGV[0] for reading.$!\n";
while (<FILE>) {
chomp();
if ( /Size of downloaded file ([a-zA-ZZ0-9_\-\.]+\.zip) = (\d+)$/) {
     $filename=$1;
     $size=$2;
     #do what you want with the data
} #close IF match
} #close While
close (FILE);
} #close if 
else {#assuming the data is being pumped in via pipe
while (<STDIN>) {
chomp();
if ( /Size of downloaded file ([a-zA-Z0-9_\-\.]+\.zip) = (\d+)$/) {
     $filename=$1;
     $size=$2;
     #do what you want with the data
} #close IF match
} #close While
} #close else

Open in new window

0
 
LVL 81

Expert Comment

by:arnold
ID: 26135709
Teh $#ARGV test should be >=.
0
 

Author Comment

by:sukhvinderkumar
ID: 26135722
#!/usr/bin/perl

if ($#ARGV>=0 and -f "ARGV[0]") { #an argument is being provided and it is a file
open (FILE, "<ARGV[0]) || die "Unable to open file $ARGV[0] for reading.$!\n";
while (<FILE>) {
chomp();
if ( /Size of downloaded file ([a-zA-ZZ0-9_\-\.]+\.zip) = (\d+)$/) {
     $filename=$1;
     $size=$2;
     #do what you want with the data
} #close IF match
} #close While
close (FILE);
} #close if
else {#assuming the data is being pumped in via pipe
while (<STDIN>) {
chomp();
if ( /Size of downloaded file ([a-zA-Z0-9_\-\.]+\.zip) = (\d+)$/) {
     $filename=$1;
     $size=$2;
     #do what you want with the data
} #close IF match
} #close While
} #close else



I'm getting below error

[test@]$ perl size.pl nohup.out
Bareword found where operator expected at size.pl line 4, near ""<ARGV[0]) || die "Unable"
        (Missing operator before Unable?)
Backslash found where operator expected at size.pl line 4, near "$!\"
        (Missing operator before \?)
String found where operator expected at size.pl line 4, at end of line
        (Missing semicolon on previous line?)
syntax error at size.pl line 4, near ""<ARGV[0]) || die "Unable to "
Can't find string terminator '"' anywhere before EOF at size.pl line 4.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 85

Accepted Solution

by:
ozo earned 1000 total points
ID: 26136055
a perl equivalent of
[admin@test]$ grep "Size of downloaded file" nohup.out
would be
[admin@test]$ perl -ne 'print if /Size of downloaded file/' nohup.out
0
 
LVL 81

Expert Comment

by:arnold
ID: 26137794
<ARGV[0] should be <$ARGV[0]
0
 

Author Comment

by:sukhvinderkumar
ID: 26152129
Hi Arnold,

#!/usr/bin/perl

if ($#ARGV>=0 and -f "ARGV[0]") { #an argument is being provided and it is a file
open (FILE, "<$ARGV[0]") || die "Unable to open file $ARGV[0] for reading.$!\n";
while (<FILE>) {
chomp();
if ( /Size of downloaded file ([a-zA-ZZ0-9_\-\.]+\.zip) = (\d+)$/) {
     $filename=$1;
     $size=$2;
     #do what you want with the data
} #close IF match
} #close While
close (FILE);
} #close if
else {#assuming the data is being pumped in via pipe
while (<STDIN>) {
chomp();
if ( /Size of downloaded file ([a-zA-Z0-9_\-\.]+\.zip) = (\d+)$/) {
     $filename=$1;
     $size=$2;
     #do what you want with the data
} #close IF match
} #close While
} #close else


I tried but it's not showing any output but it keeps running and doesn't comes to the shell prompt.

perl size.pl nohup.out
0
 
LVL 85

Expert Comment

by:ozo
ID: 26152347
are you giving it a file argument, or are you feeding it through stdin?
0
 
LVL 81

Expert Comment

by:arnold
ID: 26153882
The problem is that the section:
"#do what you want with the data" was left to do with as you saw fit.
each iteration through the loop either a file or data fed through a PIPE will be assigned to $filename and $size do you want them just printed out?
All the script I posted did was go through the file and extract the data.
Below is a modified version that works
The output will be:
On <date and time from Log> Filename: <filename> Size: <size>
#!/usr/bin/perl

if ($#ARGV>=0 and -f "$ARGV[0]") { #an argument is being provided and it is a file
open (FILE, "<$ARGV[0]") || die "Unable to open file $ARGV[0] for reading.$!\n";
while (<FILE>) {
chomp();
if ( /^([0-9]+\-[0-9]+\-[0-9]+ [0-9:]+).*Size of downloaded file ([a-zA-ZZ0-9_\-\.]+\.zip) = (\d+)$/) {
     $date_time=$1;
     $filename=$2;
     $size=$3;
     #do what you want with the data
     print "On $date_time Filename: $filename\tSize: $size\n";
} #close IF match
} #close While
close (FILE);
} #close if
else {#assuming the data is being pumped in via pipe
while (<STDIN>) {
chomp();
if ( /^([0-9\-]+ [0-9\:]+).*Size of downloaded file ([a-zA-Z0-9_\-\.]+\.zip) = (\d+)$/) {
     $date_time=$1;
     $filename=$2;
     $size=$3;
     #do what you want with the data
     print "On $date_time Filename: $filename\tSize: $size\n";
} #close IF match
} #close While
} #close else

Open in new window

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses

616 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question