How to grep the line with error where keyword in next line is known.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to grep the line with error where keyword in next line is known.
# 1  
Old 09-16-2011
How to grep the line with error where keyword in next line is known.

If a file consists of a thousands of line. There is a error line in the file which exists just before the line with word "Manish".

How could I write a script to grep the line with error.

Ex:-
If I have a UNIX file which contains the following:

bash-3.2$ cat unix.txt
Unix (officially trademarked as UNIX, sometimes also written as Unix) is a multitasking, multi-user computer operating system originally developed in 1969.
The Unix operating system was first developed in assembly language, but by 1973 had been almost entirely recoded in C, greatly facilitating its further development and porting to other hardware.
Today's Unix systems are split into various branches, developed over time by AT&T as well as various commercial vendors and non-profit organizations.
The second edition of Unix was released on December 6th, 1972.
The Open Group, an industry standards consortium, owns the UNIX trademark.
During the late 1970s and early 1980s, the influence of Unix in academic circles led to large-scale adoption of Unix (particularly of the BSD variant, originating from the University of California, Berkeley) by commercial startups, the most notable of which are Solaris, HP-UX and AIX.
The term "traditional Unix" may be used to describe a Unix or an operating system that has the characteristics of either Version 7 Unix or UNIX System V.
Unix operating systems are widely used in servers, workstations, and mobile devices.
The Unix environment and the client server program model were essential elements in the development of the Internet.
Originally, Unix was meant to be a programmer's workbench rather than be used to run application software.
The system grew larger when the operating system started spreading in the academic circle.
Many individual users started adding their own tools to the system and passing it along to colleagues.
Both Unix and the C programming language were developed by AT&T and distributed to government and academic institutions.
------------------ THIS IS THE ERROR LINE, (just before the line contains Manish).-------------------------------------------
Manish is currently learning UNIX.
Unix was designed to be portable, multi-tasking and multi-user in a time-sharing configuration.
Unix systems are characterized by various concepts: the use of plain text for storing data; a hierarchical file system.
Under Unix, the "operating system" consists of many utilities along with the master control program, the kernel.
The microkernel concept was introduced in an effort to reverse the trend towards larger kernels and return to a system in which most tasks were completed by smaller utilities.
bash-3.2$

Now in the above file I know the error line exixts just before the line contains Manish word.

what the script line will be used to fetch the error line directly?

Please Note:- Manish word is no where used in the error line, it is only in the next line of error.

Many thanks in advance.
# 2  
Old 09-16-2011
if you have gun grep, then you can try below
Code:
 
 
grep -B 2 Manish  unix.txt

# 3  
Old 09-16-2011
Thanks a lot for quick reply. I got the desired output but having 1 more query.


By using "grep -B 2 Manish unix.txt" I am getting an output which consists of two lines:
1st is the error line and 2nd is the lines contains keyword Manish.

But is there any way I can only fetch the error line, not the lines contains keyword Manish?
# 4  
Old 09-16-2011
This is one way:

Code:
grep -B 2 Manish unix.txt | grep -v Manish

# 5  
Old 09-16-2011
Code:
$ awk '{a=$0; getline; if ($0~/Manish/) {print a}}' input.txt 
------------------ THIS IS THE ERROR LINE, (just before the line contains Manish).-------------------------------------------

---------- Post updated at 07:51 PM ---------- Previous update was at 07:51 PM ----------

Code:
 grep -B 2 Manish unix.txt | head -1

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep multi line with keyword ?

Hello Everyone, i need to grep specific keyword in a file. i want need solution to output. example file.txt 03-08-2019 21:02:20,938 ::: Recieve Data From Amazon ::: 03-08-2019 21:02:20,938 IP : 192.168.1.1 | msg = Your confirmation code for 'Verify phone number' is xxxxx | sno =... (2 Replies)
Discussion started by: ooilinlove
2 Replies

2. Shell Programming and Scripting

Sed/grep: check if line exists, if not add line?

Hello, I'm trying to figure out how to speed up the following as I want to use multiple commands to search thousands of files. is there a way to speed things up? Example I want to search a bunch of files for a specific line, if this line already exists do nothing, if it doesn't exist add it... (4 Replies)
Discussion started by: f77hack
4 Replies

3. Shell Programming and Scripting

Search for a Keyword in file and replace another keyword or add at the end of line

Hi I want to implement something like this: if( keyword1 exists) then check if(keyword2 exists in the same line) then replace keyword 2 with New_Keyword else Add New_Keyword at the end of line end if eg: Check for Keyword JUNGLE and add/replace... (7 Replies)
Discussion started by: dashing201
7 Replies

4. Shell Programming and Scripting

sed command to grep multiple pattern present in single line and delete that line

here is what i want to achieve.. i have a file with below contents cat fileName blah blah blah . .DROP this REJECT that . --sport 7800 -j REJECT --reject-with icmp-port-unreachable --dport 7800 -j REJECT --reject-with icmp-port-unreachable . . . more blah blah blah --dport 3306... (14 Replies)
Discussion started by: vivek d r
14 Replies

5. Shell Programming and Scripting

How to grep multi line with keyword ?

Hi All. how to grep with same keyword. 23-07-2012 15:15:30,117 ::: Recieve Message From Commadu ::: .. ... .... 23-07-2012 16:15:28,481 ::: Recieve Message From Commadu ::: 23-07-2012 16:15:28,481 IP : 127.0.0.1 | msg =... (1 Reply)
Discussion started by: ooilinlove
1 Replies

6. Solaris

Line too long error Replace string with new line line character

I get a file which has all its content in a single row. The file contains xml data containing 3000 records, but all in a single row, making it difficult for Unix to Process the file. I decided to insert a new line character at all occurrences of a particular string in this file (say replacing... (4 Replies)
Discussion started by: ducati
4 Replies

7. Shell Programming and Scripting

Using sed to delete a line having a particular keyword

Hi Geeks :b:, I need to delete a line from file that contains a particular keyword. I had read in some forum of unix.com that below code could be used sed "/$titlesearch/d" movielist >tmp mv tmp movielist But my file contains lines which contain slashes (/) FOr eg: /etc/movie/title/... (5 Replies)
Discussion started by: ajincoep
5 Replies

8. Shell Programming and Scripting

Sed or Grep to delete line containing patter plus extra line

I'm new to using sed and grep commands, but have found them extremely useful. However I am having a hard time figuring this one out: Delete every line containing the word CEN and the next line as well. ie. test.txt blue 324 CEN green red blue 324 CEN green red blue to produce:... (2 Replies)
Discussion started by: rocketman88
2 Replies

9. Shell Programming and Scripting

Replace only if the keyword is the first word in every line

How do I replace only if the keyword is at the begining of a line? Code: -- a = “This is a print statement” print a -- What if I want to replace print by #print only in the second line i.e only if the line starts with that keyword. Please help me out. I'm new to SED. -----Post... (5 Replies)
Discussion started by: alexzubin
5 Replies

10. Shell Programming and Scripting

cat file1 read line-per-line then grep -A 15 lines down in fileb

STEP 1 # Set variable FILE=/tmp/mainfile SEARCHFILE =/tmp/searchfile # THIS IS THE MAIN FILE. cat /tmp/mainfile Interface Ethernet0/0 "outside", is up, line protocol is up Hardware is i82546GB rev03, BW 100 Mbps Full-Duplex(Full-duplex), 100 Mbps(100 Mbps) MAC address... (6 Replies)
Discussion started by: irongeekio
6 Replies
Login or Register to Ask a Question