count and content in the same command


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers count and content in the same command
# 1  
Old 02-25-2009
Lightbulb count and content in the same command

Hi,

I have small query and a understanding, could you please clarify the query and correct my understanding..?

I am very big log file If i need to take a count and contained lines for a particular pattern/word i am using the grep command twice like the below.

grep -c "OutOfMemoryException" sample.log
grep "OutOfMemoryException" > tmp.txt

This way i think i am using wrong logic i think i can use awk command like

awk '/OutOfMemoryException/' sample.log '{print $0, $NR}'

Please correct me if i am wrong.

Thanks
Senthilkumar
# 2  
Old 02-25-2009
Code:
awk ' /OutOfMemoryException/ { print; CNT++ } END { print "Record Count = " CNT } ' sample.log

# 3  
Old 02-26-2009
... or using a quick shell :

$ grep "OutOfMemoryException" sample.log > tmp.txt; wc -l tmp.txt

... will copy all relevant lines into tmp.txt AND give you the line count after Smilie
# 4  
Old 03-03-2009
Is it possible me to check the last 1000 line by using the awk command, I think the only option left for me is first i need to tail it out for 1000 line with grep -c then i need to do a normal grep to get the count and content for a particular pattern for last 1000 lines . Please correct if my understanding is wrong.

Thanks
GCS Support.
# 5  
Old 03-03-2009
Code:
tail -1000 sample.log| awk ' /OutOfMemoryException/ { print; CNT++ } END { print "Record Count = " CNT } '

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Count Command Help

Hi Everyone, I'm new to unix scripting. Hoping you guys can help me out. I am trying to create a script that will do a count command for each file (in this example *.dat) within a predefined directory and display only the filename without the full path. This is what I got so far, but it... (10 Replies)
Discussion started by: kadawtomtom978
10 Replies

2. Homework & Coursework Questions

Command combination for displaying header and content

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: You are given a ANSI text file (a.txt) in the following format and with the following contents-: NAME ROLL... (7 Replies)
Discussion started by: sreyan32
7 Replies

3. UNIX for Dummies Questions & Answers

Command combination for displaying header and content

Hi everyone, I have UNIX this semester and I am just getting started with the commands. An interesting question came up while discussing the head and tail commands. Suppose that I have text file with the following data in the following format-: NAME ROLL MARKS Sam 05 ... (2 Replies)
Discussion started by: sreyan32
2 Replies

4. Shell Programming and Scripting

compare 2 files and show count same content.

$ cat File1 Non HTTP response code:java.net.ConnectException225073X 000000005143329Load time: 402335410224 Non HTTP response code: ava.net.ConnectException206423X 000000005143330Load time: 402305687161 Non HTTP response code: ava.net.ConnectException290212X 000000005143331Load time:... (1 Reply)
Discussion started by: ooilinlove
1 Replies

5. Shell Programming and Scripting

get file content and produce command

hi buddies; ip.txt: 192.168.1.1 192.168.1.2 192.168.1.3 192.168.1.4 192.168.1.5 ... parameters.txt: portvalue username password session ... (2 Replies)
Discussion started by: gc_sw
2 Replies

6. UNIX for Dummies Questions & Answers

probloem in scp command 99% content only transfers

hi every one.. i am new to working in unix environment.. i have a problem in transferring files using scp command.. i am transferring files using scp command on one unix manchine to another unix machine.. the syntax is scp -r sourcedirectory destination directory The files in sourcedirectory... (1 Reply)
Discussion started by: GovindGembali
1 Replies

7. UNIX for Dummies Questions & Answers

Problem count with wc-l command

Hi All, I'm trying to count how many line in my *.txt file. My *.txt file has 1937 lines. The problem is : when I use wc -l command the result is 1936. Again, I did some test. I create a new file with 100 lines with text editor (Notepad ++, Ultra edit). And when I count it again with wc... (2 Replies)
Discussion started by: kunimi
2 Replies

8. Linux

Command that prints content of line5, or similar?

Hello all; I've been having trouble completing a script (bash) because I can't get past the issue of finding a line in a file. For example, I have a file like this: ddmmmyyyy Lon Lat 24may2003 -100.0 24.1 25may2003 -100.1 24.0 28may2003 -99.5 23.2 ....etc... (4 Replies)
Discussion started by: lunchtime
4 Replies

9. UNIX for Dummies Questions & Answers

Deleting all content in a file from command line

Hey guys, I'd like to delete all text within a text file without actually deleting the file. Is there a vi command I can do from my prompt without actually going into the file and doing a %d i think it is... to remove all the text inside? I want to add this to a cron job to erase all the data... (2 Replies)
Discussion started by: kingdbag
2 Replies

10. UNIX for Dummies Questions & Answers

is there a content type command?

Hi I run on a Unix webserver at my university. I have a couple of *.inc files which are include files used for php. They are all in one folder without an index file. If you go to the folder, you get to see a list of files in that dir. Some of the .inc have a question mark icon next to... (3 Replies)
Discussion started by: aqh2
3 Replies
Login or Register to Ask a Question