Display specific lines content from the file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Display specific lines content from the file
# 1  
Old 03-15-2013
Display specific lines content from the file

Hell,

I want to grep certain word from file and display above 2 lines and after two lines.

Here is the content of sample file.

Code:
Mar 14, 2013 12:56:59 AM Agent.Agent SendTo
INFO: Connection to server:7041 - Credential Transmit Successesful
Mar 14, 2013 8:54:21 AM cgent SendTo
WARNING: Data Transmit to Server Failed - Broken pipe
Mar 14, 2013 9:54:01 AM cAgent SendTo
INFO: Connection to server:7041 - Connect Successesful
Mar 14, 2013 10:56:59 AM Agent SendTo

I wanted to grep the word "WARNING" and I want +2 and -2 lines of the grep word("WARNING") along with the grep word line.

Output should be:
Code:
INFO: Connection to server:7041 - Credential Transmit Successesful
Mar 14, 2013 8:54:21 AM cgent SendTo
WARNING: Data Transmit to Server Failed - Broken pipe
Mar 14, 2013 9:54:01 AM cAgent SendTo
INFO: Connection to server:7041 - Connect Successesful

I Tried grep -A -B command but am getting the below error.
Code:
$ grep -B 2 "WARNING"  sampleFile
grep: illegal option -- B
Usage: grep -hblcnsviw pattern file . . .


Last edited by Corona688; 03-15-2013 at 12:57 PM..
# 2  
Old 03-15-2013
Hope the below link helps you

grep and display few lines before and after
# 3  
Old 03-15-2013
Hi, try this: warning, the sampleFile is present two times in this script
Code:
awk '/WARNING/{
printf "awk %cNR>=%d && NR<=%d%c sampleFile\n",39,NR-2,NR+2,39; 
}' sampleFile | sh

# 4  
Old 03-15-2013
Interesting method, but this will be extremely slow on even medium sized files since it must reread the entire file for each separate event...

It's possible to use an array in awk to save lines for later and print them at need.

Code:
$ cat warnlog.awk

BEGIN { CONTEXT=2 }

# Recall N lines ago up to 9 lines
function last(N)
{
        if(N>L) return("");
        return(LINE[(L-N)%10]);
}

{ LINE[(++L)%10]=$0 } # Remember line for later

$0 ~ REGEX  {
        for(N=CONTEXT; N>=0; N--) print last(N);
        for(N=0; N<CONTEXT; N++) { getline ; print }
}

$ awk -f warnlog.awk REGEX="WARNING" inputfile

INFO: Connection to server:7041 - Credential Transmit Successesful
Mar 14, 2013 8:54:21 AM cgent SendTo
WARNING: Data Transmit to Server Failed - Broken pipe
Mar 14, 2013 9:54:01 AM cAgent SendTo
INFO: Connection to server:7041 - Connect Successesful

$

You can override the value of CONTEXT too if you want more lines. Use nawk on solaris.
# 5  
Old 03-16-2013
Can you get a better version of grep? Beyond this problem, it will save you much future time. Your version is missing a lot of useful options.
Code:
$ uname
Linux
$ grep -C 2 WARNING temp.x
INFO: Connection to server:7041 - Credential Transmit Successesful
Mar 14, 2013 8:54:21 AM cgent SendTo
WARNING: Data Transmit to Server Failed - Broken pipe
Mar 14, 2013 9:54:01 AM cAgent SendTo
INFO: Connection to server:7041 - Connect Successesful

# 6  
Old 03-16-2013
You must tell which version you are using and let's check if your OS supports grep -A and grep -B supports.
# 7  
Old 03-16-2013
He got an error when he tried grep -B option.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep content between specific lines

cat file1 *FileHeader* Partition 0 Total Data Bytes 1416 Avg Bytes/Record 1416 Others 1 PRDX22.AUDIT_DATA_INFO Partition 4 Total Data Bytes 4615 Avg... (8 Replies)
Discussion started by: Veera_V
8 Replies

2. Shell Programming and Scripting

Need Suggestion, on how to constantly moniter a file and display content with in that time

Dear Gurus, I'm in a strange situation, hence need some kind of advice or possible syntax to carry on. let's say current time/date is 2013-12-18, 15:58:15 I got a file something like this, which keep getting updated with respect to time. 2013-12-18,... (4 Replies)
Discussion started by: manas_ranjan
4 Replies

3. UNIX for Dummies Questions & Answers

Quick UNIX command to display specific lines in the middle of a file from/to specific word

This could be a really dummy question. I have a log text file. What unix command to extract line from specific string to another specific string. Is it something similar to?: more +/"string" file_name Thanks (4 Replies)
Discussion started by: aku
4 Replies

4. Shell Programming and Scripting

Grep to display file name along with content in Solaris

Am using the following grep to match a particular patter in grep. grep xyz abc.txt now while i run this command, if the pattern matched, am getting the line containing xyz Output: xyz is doing some work Now if i want the file name also along with my output, what should i do Expected... (2 Replies)
Discussion started by: rituparna_gupta
2 Replies

5. UNIX Desktop Questions & Answers

Display a specific words from a multiple lines

well, i am so not familiar with this kind of things but i am gonna explain extactly what i am looking for so hopfully someone can figure it out :) i have a command that shows memory usage besides the process name, for example(the command output): 500 kb process_1 600 kb process_2 700 kb... (4 Replies)
Discussion started by: Portabello
4 Replies

6. UNIX for Dummies Questions & Answers

Display the content of the unix file

cat is the normal unix command to display the content of a file. Is there any program to display the content of the file. If there is, then can you send me the code. (5 Replies)
Discussion started by: vedanjalig
5 Replies

7. Shell Programming and Scripting

Display the content in different lines in email

Hi, I am checking if a file exists or not and based on the result I am sending an email. The email part works fine but the content of the email comes in a single line and I am not able to display it in diferent lines. if ; then echo "Hi, \\n An exception exists in the process.... (3 Replies)
Discussion started by: shanth_chandra
3 Replies

8. UNIX for Dummies Questions & Answers

how to display specific lines of a specific file

are there any basic commands that can display lines 99 - 101 of the /etc/passwd file? I'm thinking use of head and tail, but I forget what numbers to use and where to put /etc/passwd in the command. (2 Replies)
Discussion started by: raidkridley
2 Replies

9. Shell Programming and Scripting

Retreive content between specific lines ina file

Hi I have a text file which has two sets of lines repeating for "n" number of times.Some data is printed between the two lines.I want to retrieve all the data thats there in between those two set of lines.I have the string value of those two set of lines. To be much more clearer ... (4 Replies)
Discussion started by: chennaitomcruis
4 Replies

10. UNIX for Dummies Questions & Answers

grep/cat/more -- search in a txt file and display content from a specific "keyword"

Hi, I have a .txt file Sample: ===================== NEXT HOST ===================== AEADBAS001 ip access-list extended BLA_Incoming_Filter ip access-list extended BLA_Outgoing_Filter access-list 1 permit xxxxxxxxxxxxxx access-list 2 permit xxxxxxxxxxxxxx =====================... (4 Replies)
Discussion started by: I-1
4 Replies
Login or Register to Ask a Question