Search for a pattern in a file and print previous lines from a particular point


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search for a pattern in a file and print previous lines from a particular point
# 1  
Old 03-27-2009
Search for a pattern in a file and print previous lines from a particular point

Hi,

I am new to ksh scripting and I have a problem.
I have a file in which I have to search for a particular pattern say 'a' then from that line I need to search for another pattern say 'b' in the previous lines and thne print the file from pattern 'b' till the end of file.

For eg:

============
aaaaaaaa
bbbbbbbb
ccccccccc
dddddddddd
eeeeeeeee
==================

Now I need to search for string ccccccc from there I need to search for the first occrance of ===== pattern and from that point I need to print till the end of the file. This is so complicated because the pattern repeats in a similar way in the file.

I know this is very confusing Smilie please let me know if i need to clarify anything thats mentioned above

Last edited by umaislearning; 04-01-2009 at 12:12 PM..
# 2  
Old 03-27-2009
Something like this??
Code:
root@jeepanywhere /usr/src # cat test.log
1
2
3
4
5
1
2
3
4
5
1
2
3
4
5
root@jeepanywhere /usr/src # sed -n '/^2/,/^4/{/LABEL$/!p}' test.log
2
3
4
2
3
4
2
3
4

# 3  
Old 03-28-2009
Quote:
Originally Posted by Ikon
Something like this??
Code:
root@jeepanywhere /usr/src # cat test.log
1
2
3
4
5
1
2
3
4
5
1
2
3
4
5
root@jeepanywhere /usr/src # sed -n '/^2/,/^4/{/LABEL$/!p}' test.log
2
3
4
2
3
4
2
3
4

I am not able to understand this command very clearly, but this is wat I want, please tell me if that sed command will give me the below output:

suppose I have a file like the one below:

start of file-----
***********
1
2
3
4
5
========
***********
6
7
8
9
========
end of file-----
Now for eg I need to search for 8, then search for the first occurance of the pattern ***** thats jus few lines(no. of lines not fixed) above 8...then from there I need to print till end of file....
so my output needs to be something like this:
***********
6
7
8
9
============
end of file-----

Thanks in advance...
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search Pattern and Print lines in Single Column

Hi Experts I have small query where I request the into a single file Suppose: File1: {Unique entries} AA BB CC DD FileB: AA, 123 AA, 234 AA, 2345 CC, 123 CC, 5678 DD,123 BB, 7890 (5 Replies)
Discussion started by: navkanwal
5 Replies

2. Linux

Perl program to print previous set of lines once a pattern is matched

Hi all, I have a text data file. My aim here is to find line called *FIELD* AV for every record and print lines after that till *FIELD* RF. But here I want first 3 to four lines for very record as well. FIELD AV is some where in between for very record. SO I am not sure how to retrieve lines in... (2 Replies)
Discussion started by: kaav06
2 Replies

3. Shell Programming and Scripting

Need one liner to search pattern and print everything expect 6 lines from where pattern match made

i need to search for a pattern from a big file and print everything expect the next 6 lines from where the pattern match was made. (8 Replies)
Discussion started by: chidori
8 Replies

4. Shell Programming and Scripting

print range of lines matching pattern and previous line

Hi all, on Solaris 10, I'd like to print a range of lines starting at pattern but also including the very first line before pattern. the following doesn't print the range starting at pattern and going down to the end of file: cat <my file> | sed -n -e '/<pattern>{x;p;}/' I need to include the... (1 Reply)
Discussion started by: siriche
1 Replies

5. Shell Programming and Scripting

Finding lines matching the Pattern and their previous lines in a file

Hi, I am trying to locate the occurences of certain pattern like 'Possible network disconnect' in a text file. I can get the actual lines matching the pttern using: grep -w 'Possible network disconnect' file_name. But I am more interested in getting the timing of these events which are... (7 Replies)
Discussion started by: sagarparadkar
7 Replies

6. Shell Programming and Scripting

Search for Pattern and Print including Lines in between

Gurus, I have a big file that needs to be sorted out and I cant figure out what to do. The file name is as below: Name: xxxx yyyy nnnn Description: dfffgs sdgsgsf hsfhhs afgghhjdgj fjklllll gsfhfh Updated: jafgadsgg gsg Corrected: date today The file consists of line like these. ... (13 Replies)
Discussion started by: The One
13 Replies

7. Shell Programming and Scripting

How to use sed to search for string and Print previous two lines and current line

Hello, Can anybody help me to correct my sed syntax to find the string and print previous two lines and current line and next one line. i am using string as "testing" netstat -v | sed -n -e '/test/{x;2!p;g;$!N;p;D;}' -e h i am able to get the previous line current line next line but... (1 Reply)
Discussion started by: nmadhuhb
1 Replies

8. UNIX for Dummies Questions & Answers

Print lines between the search pattern

hi, I have a file say x.txt containing xxx 123 bla bla ... you xxx dfk dbf ... me xxx ... ... keeps on.. i need to search for pattern in the line starting xxx in the file. If pattern matched, I need to fetch all the lines till i find next xxx. (17 Replies)
Discussion started by: prsshini
17 Replies

9. Shell Programming and Scripting

Search file for pattern and grab some lines before pattern

I want to search a file for a string and then if the string is found I need the line that the string is on - but also the previous two lines from the file (that the pattern will not be found in) This is on solaris Can you help? (2 Replies)
Discussion started by: frustrated1
2 Replies
Login or Register to Ask a Question