grep return more than one line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep return more than one line
# 1  
Old 09-16-2010
grep return more than one line

Hi,

I have a lot of log files which contain lines in the format of (date info) or (info), and when I use grep to search for "date" I was able to get the (date info) line, but some info lines are a separate line after the (date info) line... for example like:

(date info info info)
(date info info)
(info info)
(date info) (or end of file)
I want grep to return the date info line and any further info lines containing the date parameter i enter.

any help would be greatly appreciated, thanks.
# 2  
Old 09-16-2010
Post real sample data.
# 3  
Old 09-16-2010
2010-07-19 10:29:00,813 ERROR [WebContainer : 10]...
2010-07-19 10:29:15,813 ERROR [WebContainer : 10].........
2010-07-19 10:29:30,813 ERROR [WebContainer : 10].......
at com..*****......
at com..*****.........

so if i look for the "2010-07-19 10:29:30"

I want the last 3 lines of this log.

thanks
# 4  
Old 09-16-2010
Try:
Code:
awk -vs="2010-07-19 10:29:30" '$0~"^"s{p=1}!($0~"^"s) && /^2010/{p=0}p' logfile

This User Gave Thanks to bartus11 For This Post:
# 5  
Old 09-16-2010
wow that worked very well!! thanks, just a little more to ask though: the returned format was more of a paragraph instead of lines, for example:
i have several lines with the same date and time, they got returned in a big chunk, is there anyway to separate them by lines like originally shown in the log files?
# 6  
Old 09-16-2010
Like this?
Code:
awk -vs="2010-07-19" '$0~"^"s{p=1}!($0~"^"s) && /^2010/{if(p==1){print "\n"}p=0}p' logfile

# 7  
Old 09-16-2010
I don't think it worked, it returned in the same format, thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Return line when line below has the string

I have a file with data like this. I want to look for MultiLoad and pull that line and the line above. I cant use the grep -B 1 I dont have that version and cant install on the server. is there a awk or perl that will do it. "CMPGN_RPT_DEV_TT"."text1" (TableId 0029H 5CEEH) bypassed due to... (4 Replies)
Discussion started by: wambli
4 Replies

2. Shell Programming and Scripting

search a string in a particular column of file and return the line number of the line

Hi All, Can you please guide me to search a string in a particular column of file and return the line number of the line where it was found using awk. As an example : abc.txt 7000,john,2,1,0,1,6 7001,elen,2,2,0,1,7 7002,sami,2,3,0,1,6 7003,mike,1,4,0,2,1 8001,nike,1,5,0,1,8... (3 Replies)
Discussion started by: arunshankar.c
3 Replies

3. UNIX for Dummies Questions & Answers

Grep to return lines not containing a character

Hello , this is my first topic cause I need your little help:( I got .txt file, and I want to find lines without letter 'a', so im writing: grep "" list.txt (list.txt is the file of course) and i have no idea why it's not working because it shows lines with a. (1 Reply)
Discussion started by: bbqtoss
1 Replies

4. Shell Programming and Scripting

Grep: Return a user message when no hits

I have ASCII files to parse that 48 hours old or more ; I can identify them like so find . -name "FILE*TXT" -mtime +1 -exec ls -ltas '{}' ';' Some files have a list of hardware errors (we test electronic components), some have none. If the file name has no errors, I still want to... (3 Replies)
Discussion started by: alan
3 Replies

5. UNIX for Advanced & Expert Users

return of grep

Hi, in a script I will do : grep ORA- mylogfile.log If it returns any ORA- (oracle error) I whant to have : echo "subject : RMAN in Error " >> /appli/rap.txt But if no error echo "subject : RMAN OK " >> /appli/rap.txt Can you help me ? The grep return code would it be... (1 Reply)
Discussion started by: big123456
1 Replies

6. UNIX for Dummies Questions & Answers

Can grep command return word instead of complete line

Hi Is there any way GREP command can return word and not complete line. My file has following data: Hello Everyone I am NitinrajSrivastava Hi Friends Welcome VrajSrivastava I am using grep 'raj' which is returning me complete line.However I want only the word having keyword 'raj'. Required... (11 Replies)
Discussion started by: dashing201
11 Replies

7. UNIX for Dummies Questions & Answers

| help | unix | grep - Can I use grep to return a string with exactly n matches?

Hello, I looking to use grep to return a string with exactly n matches. I'm building off this: ls -aLl /bin | grep '^.\{9\}x' | tr -s ' ' -rwxr-xr-x 1 root root 632816 Nov 25 2008 vi -rwxr-xr-x 1 root root 632816 Nov 25 2008 view -rwxr-xr-x 1 root root 16008 May 25 2008... (7 Replies)
Discussion started by: MykC
7 Replies

8. Shell Programming and Scripting

modifying grep to return latest files

Hi guys, I currently use the below mwntioned grep statemen to get the timestamp of the last generated file in the directory. (ls -ltr eCustomerCME* | grep ^- | tail -1 | awk ' { print $6,$7,$8 } ') I need to modify this grep to search for files generated only within last 2 hrs. Can you pls... (5 Replies)
Discussion started by: ragha81
5 Replies

9. Shell Programming and Scripting

problem in the return of grep

hi, i am running this command inside the script var=`grep -il $1 "${logdir}"* | xargs grep -ivl adding | xargs grep -ivl equation | xargs ls -ctr | tail -1` in that grep will find the latest logfile for the variable "$" if it fnd the logfile, then it reutrns the filename to the var if... (1 Reply)
Discussion started by: mail2sant
1 Replies

10. UNIX for Dummies Questions & Answers

grep for a line then return lines above

Hey guys, I just want to grep for a line then return a few lines above it I can't seem to find what im looking for on google can someone help me out? This is on Solaris 9..... I don't have GNU grep so -B and -A commands will not work (3 Replies)
Discussion started by: kingdbag
3 Replies
Login or Register to Ask a Question