Finding word in file then print the preceding....


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Finding word in file then print the preceding....
# 1  
Old 10-05-2006
Finding word in file then print the preceding....

Hi,

I am looking for a way to find a particular word in a file then print a line that precedes this line, as well as this line.

Sometimes in a log file there is only one word per line and I need to print one of the lines leading up to the single worded line.

Example - I can grep for ouch and need line 1 and the ouch line printed as one line for output.

2006-10-04 this is line one
this is line two
this is line three
ouch
this is line four

thx.
# 2  
Old 10-05-2006
Hi - this should work...

#!/bin/sh

# p_line is previous line
# c_line is current line
# myfile is path to target file

myfile=/path/to/myfile
cat $myfile | while read c_line
do
loop=
while [ -z "$loop" ]
do
if [ `echo "$c_line" | grep -c ouch` -ne 0 ]
then
echo $p_line
echo $c_line
else
p_line=$c_line
fi
loop=1
done
done
# 3  
Old 10-05-2006
Got perl? Try...
Code:
perl -00ne 'printf "%s %s\n",$1,$2 if $_=~/(.*)\n.*\n.*\n(ouch)/' file1

...gives...
Code:
2006-10-04 this is line one ouch

# 4  
Old 10-05-2006
Quote:
Originally Posted by g_jumpin
Hi,

I am looking for a way to find a particular word in a file then print a line that precedes this line, as well as this line.

Sometimes in a log file there is only one word per line and I need to print one of the lines leading up to the single worded line.

Example - I can grep for ouch and need line 1 and the ouch line printed as one line for output.

2006-10-04 this is line one
this is line two
this is line three
ouch
this is line four

thx.
Python alternative:

Code:
#!/usr/bin/python
data = open("test.txt").readlines()
for i in range(len(data)):
 	if "ouch" in data[i] :
 		print data[i-1] , data[i]

Output:
Code:
this is line three
ouch

# 5  
Old 10-05-2006
One line solution in awk, if you know the line number of the preceeding line

Code:
 awk 'NR==1{str=$0}$0~/ouch/{ print str,$0}' filename

# 6  
Old 10-05-2006
Thanks all - I will give it try today.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract whole word preceding a specific character pattern with first occurence of the pattern

Hello. Here is a file contents : declare -Ax NEW_FORCE_IGNORE_ARRAY=(="§" ="§" ="§" ="§" ="§" .................. ="§"Here is a pattern =I want to extract 'NEW_FORCE_IGNORE_ARRAY' which is the whole word before the first occurrence of pattern '=' Is there a better solution than mine :... (3 Replies)
Discussion started by: jcdole
3 Replies

2. UNIX for Beginners Questions & Answers

Finding a word through substring in a file

I have a text file that has some data like: PADHOGOA1 IOP055_VINREG5_1 ( .IO(VINREG5_1), .MONI(), .MON_D(px_IOP055_VINREG5_1_MON_D), .R0T(px_IOP054_VINREG5_0_R0T), .IO1() ); PADV30MA0 IOP056_VOUT3_IN ( .IO(VOUT3_IN), .V30M(px_IOP056_VOUT3_IN_V30M)); PADV30MA0 IOP057_VOUT3_OUT (... (2 Replies)
Discussion started by: utkarshkhanna44
2 Replies

3. Shell Programming and Scripting

Print next line beside preceding line on column match

Hi, I have some data like below: John 254 Chris 254 Matt 123 Abe 123 Raj 487 Moh 487 How can i print it using awk to have: 254 John,Chris 123 Matt,Abe 487 Raj,Moh Thanks. (4 Replies)
Discussion started by: james2009
4 Replies

4. Linux

Print each ; separated value, in front of the value preceding them

I have a file that looks like this, P38112 Q12389; Q03532; P43586; Q08208; P37838; P40693; P38805 Q12389 P32892; P36049; Q03532; P43586; Q12176; P38112; P39744; P37838 P11154 P39940 P50094 P50095 P32892 Q12389; Q04660; P43586; P37838; P40693 Q05022 Q08235; Q12176; P39744 P40347 P32333... (2 Replies)
Discussion started by: Syeda Sumayya
2 Replies

5. Shell Programming and Scripting

Search for a specific word and print only the word from the input file

Hi, I have a sample file as shown below, I am looking for sed or any command which prints the complete word only from the input file. Ex: $ cat "sample.log" I am searching for a word which is present in this file We can do a pattern search using grep but I need to cut only the word which... (1 Reply)
Discussion started by: mohan_kumarcs
1 Replies

6. Shell Programming and Scripting

Finding pattern in a text file and returning a part of the word

Dear All, assume that we have a text file or a folder of files, I want to find this pattern followers*.csv in the text file , and get * as the output. There are different matches and * means every character. Thank you in advance. Best, David (1 Reply)
Discussion started by: davidfreed
1 Replies

7. UNIX for Dummies Questions & Answers

How to print the specific word in a file.

Hi , My input file is below like that :- $cat abc.txt Service name: test_taf Service is enabled Server pool: test_tac Cardinality: 2 Disconnect: false Service role: PRIMARY Management policy: AUTOMATIC DTP transaction: false AQ HA notifications: true Failover type: SESSION... (3 Replies)
Discussion started by: sp001
3 Replies

8. Shell Programming and Scripting

need help with finding a word in file's contents

Hi, I need to check if a particular name is already in the file or not and i am using following code for this... match=$(grep -n -e "$output1" outputfiles.txt ) where output1 is the variable name having names in it and outputfiles.txt is the file name ..and i am using ksh can anybosy... (6 Replies)
Discussion started by: manmeet
6 Replies

9. Shell Programming and Scripting

Finding a word in a file

Hi frndz, i have a flat file like, xxx yyy zzz sss aaa bbb yyy xxx rrr sss ttt yyy ddd zzzz cccc.. look, in this file i want to fetch the substring from one yyy to another one and need to print it then from next values between yyy's.. can you please give me some inputs on this.. ... (10 Replies)
Discussion started by: smr_rashmy
10 Replies

10. Shell Programming and Scripting

Print out just a word from the file

Hi, Would like to find a more suitable solution for the following. I have a file eg test.log. In this file, i have to find the line that has "Final rating" which is the starting of the line. I need to print out only 5.75 instead of the whole line using "grep". May I know what suitable command... (8 Replies)
Discussion started by: Kinki
8 Replies
Login or Register to Ask a Question