Need 10 lines before the first occurance of a pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need 10 lines before the first occurance of a pattern
# 1  
Old 01-27-2010
Question Need 10 lines before the first occurance of a pattern

Here is the text file:
Code:
1This is a text file
2this is a text file
3This is a text file
4this is a text file
5This is a text file
6this is a text file
7This is a text file
8this is a text file
9This is a text file
10this is a text file
11This is a text file
12this is a text file
13This is a text file
14this is a text file
check
15This is a text file
16this is a text file
check
17This is a text file
18this is a text file
19This is a text file
20this is a text file

I need to search for first occurrence of "check" and need to show 10 lines above this pattern.

The result should be:
Code:
5This is a text file
6this is a text file
7This is a text file
8this is a text file
9This is a text file
10this is a text file
11This is a text file
12this is a text file
13This is a text file
14this is a text file

Please help!

Last edited by Yogesh Sawant; 01-27-2010 at 04:02 PM.. Reason: added code tags
# 2  
Old 01-27-2010
If your grep version don't support the -B option, this should do the job:

Code:
awk '
/check/ {
  for(i=0;i<10;i++) {
    print a[(NR+i)%10]
  }
  exit
}
{a[NR%10]=$0}
' file

# 3  
Old 01-27-2010
Or a (mostly) sed solution:

Code:
sed -n '/check/q;p' input_file | tail -10

# 4  
Old 01-27-2010
Bug its working

hey its working... thanks so much!
# 5  
Old 02-26-2010
MySQL

see the following grep command.
Code:
grep --max-count 1 -B 10 'check' input  | grep -v 'check'

Output:
Code:
5This is a text file
6this is a text file
7This is a text file
8this is a text file
9This is a text file
10this is a text file
11This is a text file
12this is a text file
13This is a text file
14this is a text file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

ksh sed - Extract specific lines with mulitple occurance of interesting lines

Data file example I look for primary and * to isolate the interesting slot number. slot=`sed '/^primary$/,/\*/!d' filename | tail -1 | sed s'/*//' | awk '{print $1" "$2}'` Now I want to get the Touch line for only the associate slot number, in this case, because the asterisk... (2 Replies)
Discussion started by: popeye
2 Replies

2. Shell Programming and Scripting

Grep word after last occurance of string and display next few lines

Hi, I wanted to grep string "ERROR" and "WORNING" after last occurrence of String "Starting" only and wanted to display two lines after searched ERROR and WORNING string and one line before. I have following cronjob log file "errorlog" file and I have written the code for same in Unix as below... (17 Replies)
Discussion started by: nes
17 Replies

3. Shell Programming and Scripting

Search for a pattern in a String file and count the occurance of each pattern

I am trying to search a file for a patterns ERR- in a file and return a count for each of the error reported Input file is a free flowing file without any format example of output ERR-00001=5 .... ERR-01010=10 ..... ERR-99999=10 (4 Replies)
Discussion started by: swayam123
4 Replies

4. Shell Programming and Scripting

sed pattern to delete lines containing a pattern, except the first occurance

Hello sed gurus. I am using ksh on Sun and have a file created by concatenating several other files. All files contain header rows. I just need to keep the first occurrence and remove all other header rows. header for file 1111 2222 3333 header for file 1111 2222 3333 header for file... (8 Replies)
Discussion started by: gary_w
8 Replies

5. Shell Programming and Scripting

Pattern matching first occurance only wanted

I have the following code: declare -a search search=(name label elapsed totalfilecount totalfilebytes starttime exitcode errors warnings fatals) for (( i = 0 ; i < ${#search} ; i++ )) do data=`awk -F '^'${search}'=\"' 'BEGIN{RS="\" "; OFS="\n"; ORS=""} { print $2 }' summary.alg` ... (1 Reply)
Discussion started by: Ikon
1 Replies

6. Shell Programming and Scripting

How can I match lines with just one occurance of a string in awk?

Hi, I'm trying to match records using awk which contain only one occurance of my string, I know how to match one or more (+) but matching only one is eluding me without developing some convoluted bit of code. I was hoping there would be some simple pattern matching thing similar to '+' but... (9 Replies)
Discussion started by: jonathanm
9 Replies

7. UNIX for Dummies Questions & Answers

Replace first 5 occurance of a pattern

There is scenario that i have to replace a pattern at the first 5 occurance in a file. say i need to replace 'abc' by 'xyz' at its first 5 occurance in a file a.txt, can anybody help me how it can be done, i can do complete replacement of the pattern using sed throughtout the file. (1 Reply)
Discussion started by: palash2k
1 Replies

8. Shell Programming and Scripting

How to find files which has more than one occurance of pattern

Hello Everyone, Please help me in finding out the solution. The problem is .. lets say i have 600 files in a directory. All 600 files are shell script files. Now i need to find out the files which contains a pattern "SHELL" more than once. No matter how the pattern occurs , it can be in... (10 Replies)
Discussion started by: Prahlad
10 Replies

9. Shell Programming and Scripting

Finding Last occurance of another pattern when a pattern is found.

Hi, I have two files viz, rak1: $ cat rak1 rak2: $ cat rak2 sdiff rak1 rak2 returns: I want the lines that got modified, changed, or deleted preceding with the section they are in. I have done this so far: (1 Reply)
Discussion started by: rakeshou
1 Replies

10. UNIX for Dummies Questions & Answers

Count of matched pattern occurance

In a file a pattern is occured many times randomly. Even it may appear more then once in the same line too. How i can get the number of times that pattern appeared in the file? let the file name is abc.txt and the pattern is "xyz". I used the following code: grep -ic "xyz" abc.txt but it is... (3 Replies)
Discussion started by: palash2k
3 Replies
Login or Register to Ask a Question