Grep a portion of the log file


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Grep a portion of the log file
# 1  
Old 03-18-2009
Grep a portion of the log file

I want to grep a portion of the log file. grepping a particular pattern and including 10 lines before that and after that occurence.

grep -n "SomeString Pattern" filename

10 lines before this occurence and 10 lines after that.

Please help. Need the simple script not in awk or sed.

Last edited by sainipardeep; 03-18-2009 at 02:54 AM..
# 2  
Old 03-18-2009
try this..
Code:
awk 'c-->0;$0~s{if(b)for(c=b+1;c>1;c--)print r[(NR-c+1)%b];print;c=a}b{r[NR%b]=$0}' b=10 a=10 s="searchpattern"  filename

# 3  
Old 03-18-2009
Simple script not in awk or sed

Thanks Vidhya
But I dont know much about these editors. I am trying for a pattern like

grep -n "745100000000013" filename | while read line
do
echo "Line is --$LINE--"
done > outputfilename
# 4  
Old 03-18-2009
Quote:
Originally Posted by sainipardeep
Thanks Vidhya
But I dont know much about these editors. I am trying for a pattern like

grep -n "745100000000013" filename | while read line
do
echo "Line is --$LINE--"
done > outputfilename
try that awk or what you are trying to do is
Code:
 
grep -n "pattern" filename|awk -F":" '{print $1}' > temp_123
while read line ; do
li=$line
sed -n "$line,$((line+10))p" filename
echo "-----------------------------------------"
sed -n "$((li-10)),$((li))p" filename
done < temp_123

# 5  
Old 03-18-2009
dont want the code in awk or sed

dont want the code in awk or sed

Quote:
Originally Posted by vidyadhar85
try that awk or what you are trying to do is
Code:
 
grep -n "pattern" filename|awk -F":" '{print $1}' > temp_123
while read line ; do
li=$line
sed -n "$line,$((line+10))p" filename
echo "-----------------------------------------"
sed -n "$((li-10)),$((li))p" filename
done < temp_123

# 6  
Old 03-18-2009
This one-line command will do:

sed -n 100,200p filename | grep -10 pattern

searches for a pattern from line 100 to line 200 of filename and prints 10 lines above and 10 lines below the pattern.
# 7  
Old 03-18-2009
Quote:
Originally Posted by sainipardeep
dont want the code in awk or sed
then its hard to do.. for simple req you have to write pages of script
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How do I separate a portion of a file name to use grep on?

I'm trying to write a script that takes a file name in the form of Name_Num1_Num2.Extension and I want to separate the name portion and then use grep to see if that name part has any illegal characters in it. I already have my grep command written and it works, I'm not sure how to separate the... (5 Replies)
Discussion started by: steezuschrist96
5 Replies

2. UNIX for Dummies Questions & Answers

How to append portion of a file content to another file when a certain pattern is matching?

Hi ladies and gentleman.. I have two text file with me. I need to replace one of the file content to another file if one both files have a matching pattern. Example: text1.txt: ABCD 1234567,HELLO_WORLDA,HELLO_WORLDB DCBA 3456789,HELLO_WORLDE,HELLO_WORLDF text2.txt: XXXX,ABCD... (25 Replies)
Discussion started by: bananamen
25 Replies

3. Shell Programming and Scripting

Unix Scripting : Sort a Portion of a File and not the complete file

Need to sort a portion of a file in a Alphabetical Order. Example : The user adam is not sorted and the user should get sorted. I don't want the complete file to get sorted. Currently All_users.txt contains the following lines. ############## # ARS USERS ############## mike, Mike... (6 Replies)
Discussion started by: evrurs
6 Replies

4. Shell Programming and Scripting

How to grep a portion of line

Mysql log has something like: I want to grep only the portion "ernie-1328697839.1233158" from each line. How to do this? (6 Replies)
Discussion started by: proactiveaditya
6 Replies

5. Shell Programming and Scripting

Extract portion of log info based on specific word

Hi Gurus, I'm using HP-UX B.11.23 operating system. I've been trying to extract a specific wording for example: "A tool used by tp produced warnings" from my below log data, but could not find a way to solve it. My intention is, if the log contain the word: "A tool used by tp produced... (9 Replies)
Discussion started by: superHonda123
9 Replies

6. UNIX for Dummies Questions & Answers

Grab Portion of Output Text (sed, grep, awk?)

Alright, here's the deal. I'm running the following ruby script (output follows): >> /Users/name/bin/acweather.rb -z 54321 -o /Users/name/bin -c Clouds AND Sun 57/33 - Mostly sunny and cool I want to just grab the "57/33" portion, but that's it. I don't want any other portion of the line. I... (5 Replies)
Discussion started by: compulsiveguile
5 Replies

7. Shell Programming and Scripting

Grep certain portion from the file

Dear Friends, Here I am with another difficulty. I have a flat file from which I wanna grep following pattern. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Statement Date : Blah blah Blah blah Blah blah Blah blah... (1 Reply)
Discussion started by: anushree.a
1 Replies

8. UNIX for Dummies Questions & Answers

How to extract a portion of text from a log file

I am using Unix on Mac OS X 10.5.6. I am trying to extract the last entry of a log (text) file. As seen below, each log entry looks like the following (date and time change with each log entry): I want the script to extract everything quoted above, including the "===" dividers. ... (2 Replies)
Discussion started by: atilano
2 Replies

9. Programming

Delete Portion of a file

hi i would like to know whether i can delete a part of a file in C for eg. if my file contained 1234567890 and i want to delete 456 so that it becomes 1237890 is there a way i can do this. well, one way i can achieve this is by creating a new file, copy whatever i want, then delete the... (2 Replies)
Discussion started by: sameersbn
2 Replies

10. Shell Programming and Scripting

extract a portion of log file

hello, I want to grep the log file according to time and get the portion of log from one particular time to other. I can grep for individual lines by time but how should I print lines continuously from given start time till end till given end time. Appreciate your ideas, Thanks chandra (8 Replies)
Discussion started by: chandra004
8 Replies
Login or Register to Ask a Question