Quick question on grep: grabbing lines above and below


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Quick question on grep: grabbing lines above and below
# 1  
Old 05-27-2008
Quick question on grep: grabbing lines above and below

Just a quick question on grep/egrep.

I am writing a shell script that is looking for certain strings in a text file. It works well and gets exactly what I need.

However, the way the program writes to the text file, it puts the timestamp in a line above the string I am looking for and the path below the string I am looking for.

So my question is, how do I grab all three lines in my search? I am thinking I might need to go to something more powerful like awk.

I appreciate the help.

TCG
# 2  
Old 05-28-2008
In the basic implementations of grep, not without putting a lot of shell script round it. In newer version of grep, there's a commandline parameter to display a set number of lines before and after each match. man grep for more info and to see if you have the right version.
# 3  
Old 05-28-2008
Quote:
Originally Posted by Smiling Dragon
In the basic implementations of grep, not without putting a lot of shell script round it. In newer version of grep, there's a commandline parameter to display a set number of lines before and after each match. man grep for more info and to see if you have the right version.
Hmm. Ok. I will check it out.

On my particular system, we are running Solaris 9. Not sure if that would support the newer version.

Assuming that I do not have the newer version, any other suggestions?

Thanks.

TCG
# 4  
Old 05-29-2008
Can you show a bit of the file as example?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grabbing text between two lines with shell variables.

I would like to grab complex html text between lines using variables. I am running Debian and using mksh shell. Here is the part of the html that I want to extract from. I would like to extract the words 'to love,' and I would like to use the above and below lines as reference points. ... (3 Replies)
Discussion started by: bedtime
3 Replies

2. UNIX for Dummies Questions & Answers

General find /grep question: files with lines ending in r

I am trying to find files that have lines in them that end in an r. I have been able to locate files by using the following command: find . -type f -name "*RECORDS"| xargs grep -l r$ However, I now want to find files that don't end in r anywhere. That means that no sentences or lines in... (9 Replies)
Discussion started by: newbie2010
9 Replies

3. Shell Programming and Scripting

Quick help on grep -w please

Hello Experts, Here is my problem.. cat abc.txt 1,"nathan available" 2,"MW nathan available" 3,"HW nathan available" How can i grep for "nathan available" alone. I tried grep -w "nathan available" Problem is that the pattern is enclosed in Quotes " ". I know we can do grep... (2 Replies)
Discussion started by: sathyaonnuix
2 Replies

4. UNIX for Dummies Questions & Answers

[Quick question]Problem with execl and GREP

Guys, I have the following code #include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/wait.h> #include <unistd.h> void read2(); main(int argc, char** argv) { int pid,status; pid=fork(); if ( pid == 0 ) { read2(argv,... (4 Replies)
Discussion started by: pfpietro
4 Replies

5. Shell Programming and Scripting

Grabbing lines out of a file based on a date

Hello, I'm new to this forum and am not exactly sure where to post this question, so I'll start here. I'm looking for a command or simple script that will read in a large flat file (contains 2005 data) and will output a new file based on a quarter. Within each row, position 87-90 is a julian... (2 Replies)
Discussion started by: bsp18974
2 Replies

6. UNIX for Dummies Questions & Answers

Quick Grep question

Is it possible to grep for two words at once? I want to grep for the words SEVERE or FATAL. Thanks. (1 Reply)
Discussion started by: ssmiths001
1 Replies

7. UNIX for Dummies Questions & Answers

Another quick question

Hi guys sed -e "s/$<//g" the $< can allow me to assign an input value to the variable right? do the double quotes check the previous context? (1 Reply)
Discussion started by: hamoudzz
1 Replies

8. UNIX for Dummies Questions & Answers

quick question

hi guys trying to understand what this line means sed is a stream editor and i understand that, i have a file already selected i want to edit so i use -e sed -e the next stesp is s/$* s is a subsititute replacement sed -e s/$*//g $ is in reference of the last line /g makes it... (2 Replies)
Discussion started by: hamoudzz
2 Replies

9. UNIX for Advanced & Expert Users

Quick VI question

This "SHOULD" be a simple question, but looking through several books has turned up nothing, so I turn once again to the experts!! How do you vi a file so that you can see special characters. I believe my /etc/passwd file is being corrupted during an upgrade process, however the files... (6 Replies)
Discussion started by: Recon
6 Replies

10. UNIX for Dummies Questions & Answers

Quick Question

Hello There! I am trying to write this SIMPLE script in Bourne Shell but I keep on getting syntax errors. Can you see what I am doing wrong? I've done this before but I don't see the difference. I am simply trying to take the day of the week from our system and when the teachers sign on I want... (7 Replies)
Discussion started by: catbad
7 Replies
Login or Register to Ask a Question