How to get a next line of a matched word?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to get a next line of a matched word?
# 1  
Old 08-06-2007
How to get a next line of a matched word?

Hi ,

If I match a word in a file, I want to pick the next line of that matched word.

My file is a below format-

The ntrag trace has auditError
Line5005 contains transaction Ntrag data

------------

Here I wanted if I match a word auditError, I need to get the next line
"Line5005 contains transaction Ntrag data"
# 2  
Old 08-06-2007
I don't know what utility you are using, but with awk, you can do something like this:
Code:
awk '/auditError/{getline; print}' inputfile

# 3  
Old 08-06-2007
if you have GNU grep, you can use the -A switch
# 4  
Old 08-06-2007
Thanks blowtorch.

awk '/auditError/{getline; print}' inputfile this has helped me a lot.

But how if I wanted to print the next two lines of matched word.
# 5  
Old 08-06-2007
Muktesh,
Try to always specify the entire request at once.
Any change in the original specification could change the entire solution.

In any event, here is a flexible and parametized way to do what you want:
Code:
#!/bin/ksh
typeset -i mCntLines=0
mFound="N"
mTotLines=2
mPattern='YourPattern'
while read mLine
do
  mCntGrep=`echo ${mLine} | grep -c "${mPattern}"`
  if [ ${mCntGrep} = "1" ]; then
    mCntLines=0
    mFound="Y"
    continue
  fi
  if [ "$mFound" = "Y" -a ${mCntLines} -lt ${mTotLines} ]; then
    echo ${mLine}
    mCntLines=${mCntLines}+1
  fi
done < input_file

Where:
mPattern --> Your search pattern.
mTotLines --> Number of lines to print after pattern line.
# 6  
Old 08-06-2007
A sed solution:

Code:
sed -n '/auditError/ {n;p;}' file

# 7  
Old 08-06-2007
line_no=`cat <file-name> | grep -c <pattern-name>`
req_line=`cat <file-name> | awk '{NE=$line_no-1}'`

PS: Not tested the above one. Pls correct me if I m wrong
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to print previous line of multiple pattern matched line?

Hello, I have below format log file, Comparing csv_converted_files/2201/9747.1012H67126.5077292103609547345.csv and csv_converted_files/22019/97447.1012H67126.5077292103609547345.csv Comparing csv_converted_files/2559/9447.1012H67126.5077292103609547345.csv and... (6 Replies)
Discussion started by: arvindshukla81
6 Replies

2. Shell Programming and Scripting

Find word in a line and output in which line the word occurs / no. of times it occurred

I have a file: file.txt, which contains the following data in it. This is a file, my name is Karl, what is this process, karl is karl junior, file is a test file, file's name is file.txt My name is not Karl, my name is Karl Joey What is your name? Do you know your name and... (3 Replies)
Discussion started by: anuragpgtgerman
3 Replies

3. Shell Programming and Scripting

Read a File line by line and split into array word by word

Hi All, Hope you guys had a wonderful weekend I have a scenario where in which I have to read a file line by line and check for few words before redirecting to a file I have searched the forum but,either those answers dint work (perhaps because of my wrong under standing of how IFS... (6 Replies)
Discussion started by: Kingcobra
6 Replies

4. Shell Programming and Scripting

Write a word at 72nd position of a matched line in a file

Hi, I need to search a file for a pattern,replace some other word and write a word at its 72nd position. For example, My name is Mano.Im learning Unix. I want to search the file in all lines containing the word "Mano".In that matched line,replace the word "Unix" with "Java".And... (5 Replies)
Discussion started by: mano1 n
5 Replies

5. Shell Programming and Scripting

Help required on joining one line above & below to the pattern matched string line.

Hi Experts, Help needed on joining one line above & below to the pattern matched string line. The input file, required output is mentioned below Input file ABCD DEFG5 42.0.1-63.38.31 KKKK iokl IP Connection Available ABCD DEFG5 42.0.1-63.38.31 ... (7 Replies)
Discussion started by: krao
7 Replies

6. Shell Programming and Scripting

Grep word between matched pattern

would like to print word between matched patterns using sed for example : create INDEX SCOTT.OR_PK ON table_name(....) would like to print between SCOTT. and ON which is OR_PK Please help me out Thanks (4 Replies)
Discussion started by: jhonnyrip
4 Replies

7. Shell Programming and Scripting

start to print when a specified word is matched

Hello everybody, How can I start to print an output when a word hits the pattern space. for example: file.txt : first line second line third line fourth line ... cat file.txt > function "second" should print second line third line fourth line ... thanks for the replies now... (3 Replies)
Discussion started by: Oddant
3 Replies

8. Shell Programming and Scripting

awk script to move a line after the matched pattern line

I have the following text format in a file which lists the question first and then 5 choices after that the explanantion and finally the answer. 1.The amount of time it takes for most of a worker’s occupational knowledge and skills to become obsolete has been declining because of the... (2 Replies)
Discussion started by: nanchil_guy
2 Replies

9. Shell Programming and Scripting

Print word 1 in line 1 and word 2 in line 2 if it matches a pattern

i have a file in this pattern MATCH1 word1 IMAGE word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH1 word1 IMAGE word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1... (7 Replies)
Discussion started by: bangaram
7 Replies

10. Shell Programming and Scripting

Replacing a word after a matched pattern

Hello, Actually i want to replace the word after a matched pattern. For Ex: lets say that i am reading a file line by line while read line do echo $line # i need to search whether a pattern exists in the file and replace the word after if the pattern exist. # for example :... (1 Reply)
Discussion started by: maxmave
1 Replies
Login or Register to Ask a Question