Please explain this grep command


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Please explain this grep command
# 1  
Old 04-22-2013
Please explain this grep command

Please explain grep -A 999999. I've seen this before, it always seems to be with six 9's as well. See an example below.

Code:
grep 'regexp' -A 999999 server.log | egrep -c  'Option=\[Yes\]'


Last edited by scj2012; 04-22-2013 at 04:53 PM..
# 2  
Old 04-22-2013
Looks like a hack to print all lines after and including the first to match 'server.log' and then count those with Option=[Yes]. It assumes that the number of lines left in the file will be less than or equal to 999999.

I don't see the point of it. The following does the same job without the line count limitation:
Code:
sed -n '/server\.log/,$ { /Option=\[Yes]/p; }' | wc -l

Note that the 'server.log' in that grep command is almost certainly incorrect. The dot is behaving as a wildcard when what's most likely intended is a literal dot.

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 3  
Old 04-22-2013
Quote:
Originally Posted by alister
Note that the 'server.log' in that grep command is almost certainly incorrect. The dot is behaving as a wildcard when what's most likely intended is a literal dot.
True. The other plausible explanation is that the writer of this intended to search a file named "server.log" for a string/regexp he just forgot to write. This would mean the statement is equally incorrect, but so for a different reason.

Code:
grep '<missing regexp>' -A 999999 server.log

In any way, your solution is preferable to this, albeit in case the intended purpose was like i said it would have to be modified slightly:

Code:
sed -n '/<missing regexp>/,$ { /Option=\[Yes]/p; }' server.log | wc -l

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 4  
Old 04-22-2013
Yes. I forget the regexp. But my question was more along the lines why do we even need the A option in the command to begin with.
# 5  
Old 04-22-2013
Quote:
why do we even need the A option
grep -A N prints N lines of "after-context". That means it prints the matching line, and then the next N lines. So grep regexp -A 999999 prints the matching line and the next 999999 lines. The reason they chose six nines is they figured that was such as big number that it would print the rest of the log file, is my guess. 999999 is a very big number, in terms of number of lines for a log file. So the overall intention is to count the number of lines with 'Option=\[Yes\]' that occur AFTER the line with regexp.
# 6  
Old 04-22-2013
Quote:
Originally Posted by hanson44
So the overall intention is to count the number of lines with 'Option=\[Yes\]' that occur AFTER the line with regexp.
Not just after, but after and including the line that matched. The rest of your post made that distinction correctly, but I thought I'd correct it since you emphasized "after" with caps.

Regards,
Alister
# 7  
Old 04-22-2013
Quote:
Not just after, but after and including the line
For some reason, my intuition I guess, I was assuming the matched line did not have 'Option=\[Yes\]' which was maybe a bad assumption. Thank you for correcting me.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Explain iconv command

I have a requirement to remove all non-ascii characters from a fixed length file. I used the below command which is removing special characters but somehow the total record length is being truncated to one space less. If it is a multi-byte string then many characters at the end are being truncated.... (8 Replies)
Discussion started by: eskay
8 Replies

2. Shell Programming and Scripting

Can someone explain the following shell command?

Hi Forum. I have the following script /home/user/EDW_ENV.sh to setup some environment variables as: ##### section 1 PM_HOME ##### export PC_DIR_BASE=/data/informatica/ming export DIR_ORACLE=/data/sw/apps/oracle/Oracle_scripts export... (4 Replies)
Discussion started by: pchang
4 Replies

3. Red Hat

Please help to explain the command

su - keibatch -c ""date ; /usr/local/kei/batch/apb/bin/JKEIKYK4140.sh -run "&$C$6&" WSUKE100201"" Not clear about : date ; /usr/local/kei/batch/apb/bin/JKEIKYK4140.sh -run "&$C$6&" WSUKE100201 Please help (2 Replies)
Discussion started by: honda_city
2 Replies

4. Shell Programming and Scripting

Can any one explain this sqlplus command?

Hi , i am new to unix i need a small clarification regarding this sqlplus -s $USER_NAME/$PASSWD@$ORA_SID<< EOF >> SQL_CONN_LOG.log In the above command what is the meaning of <<EOF>> Thanks, krishna. (2 Replies)
Discussion started by: rams_krishna
2 Replies

5. Shell Programming and Scripting

Explain sed command

sed -e 's,*$,,' Can someone explain the options in this command? (2 Replies)
Discussion started by: scj2012
2 Replies

6. UNIX for Dummies Questions & Answers

Please explain this command?

Hi, I saw this. But I don't know why we need this? ls mydir > foo.txt ## I know what this will do, it will take the results and write to the file called foo.txt ls mydir > foo.txt 2>&1 ## Don't know why we need 2>&1 Thanks. (2 Replies)
Discussion started by: samnyc
2 Replies

7. Shell Programming and Scripting

Please Explain me this command

find . -type f -ctime +3 -exec mv {} /somedirectory/ \; in particular "-ctime v/s -mtime" and "difference between +3 and -3" (5 Replies)
Discussion started by: Rambo
5 Replies

8. UNIX for Dummies Questions & Answers

Can anyone explain what this command is doing?

Specifically what is the purpose of sed? What is f? Why is the 'cp f $phonefile' line needed when the script ‘goes live'? Why might that two commands following sed be commented out at the present time ( i.e., during development)? Thanks in... (2 Replies)
Discussion started by: knp808
2 Replies

9. Shell Programming and Scripting

please explain the command

Hi all , please explain the following command : perl -e 'select(undef,undef,undef,.15)' Thanks and Regards Navatha (2 Replies)
Discussion started by: Navatha
2 Replies

10. UNIX for Dummies Questions & Answers

Explain the output of the command....

Explain the output of the command “sort -rfn file1 | more” (1 Reply)
Discussion started by: wickbc
1 Replies
Login or Register to Ask a Question