Getting lines before and until next pattern in file /awk, sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Getting lines before and until next pattern in file /awk, sed
# 15  
Old 10-24-2012
Hi Bakunin, good one...but somehow it isn't giving any o/p...can you please recheck once

---------- Post updated at 04:58 PM ---------- Previous update was at 04:48 PM ----------

Per my analysis every N is erasing the buffer and appending the current pattern line with next line..
Code:
cat temp4
***SNMP-ST***
IP = 1.1.1.1
some text here
***SNMP-END***
***SNMP-ST***
dhsadadk
hdhksdhkadka
IP = 2.1.1.1
some text here
***SNMP-END***

I tried displaying without any condition
Code:
sed -n '/\*\*\*SNMP-ST\*\*\*/,/\*\*\*SNMP-END\*\*\*/N
        /\*\*\*SNMP-END\*\*\*/ {
              p
             d
        }' temp4

o/p
Code:
some text here
***SNMP-END***
some text here
***SNMP-END***

So its holding only 2 lines per read of the file...
# 16  
Old 10-24-2012
This one excluding the line with SNMP-END:
Code:
awk '/SNMP-END/{p=0} $0=="IP = 1.1.1.1" {p=1}p' infile


Last edited by Scrutinizer; 10-24-2012 at 06:06 PM..
# 17  
Old 10-24-2012
I was close to the result using hold buffer H and later with exchange buffer x when /SNMP-END/ line reaches. But not able to extract exactly...

Code:
sed -n '/SNMP-ST/,/SNMP-END/ H;/SNMP-END/{x;/1\.1\.1\.1/ p;}' temp4

o/p
Code:
***SNMP-ST***
IP = 1.1.1.1
some text here
***SNMP-END***
***SNMP-END***
***SNMP-ST***
dhsadadk
hdhksdhkadka
IP = 1.1.1.1
some text here
***SNMP-END***

# 18  
Old 10-25-2012
Quote:
Originally Posted by bakunin
You can do it in sed (which is probably faster than PERL) with the following algorithm: collect all lines, starting from the beginning of a block to and ending with the end of a block, in a buffer. Then print only the blocks which contain the search string and clear the buffer then, regardless of it being printed or not:

Code:
sed -n '/\*\*\*SNMP-ST\*\*\*/,/\*\*\*SNMP-END\*\*\*/N
        /\*\*\*SNMP-END\*\*\*/ {
             /IP *= *1\.1\.1\.1\./p
             d
        }' /path/to/infile

After looking at it again having gotten a portion of nights sleep, i notice this will only work in some special circumstances:

Any line starting with the designated start line and ending with the designated end line (i will call such a portion a "paragraph" in the following) will trigger the reading of the next line in the first rule, which means that all lines of the paragraph in question plus the first line of the next one are being read.

If two paragraphs to be printed lie adjacent the first set of rule 1 triggering will consume the starting line of the second paragraph and the second paragraph will therefore never get printed.

So, here the revised version, which should work:
Code:
sed -n '/\*\*\*SNMP-ST\*\*\*/,/\*\*\*SNMP-END\*\*\*/ {
             /\*\*\*SNMP-ST\*\*\*/ {
                  h
                  d
             }
             /\*\*\*SNMP-END\*\*\*/!H
        }
        /\*\*\*SNMP-END\*\*\*/ {
             x
             G
             /IP *= *1\.1\.1\.1/p
             d
        }' /path/to/input

The first range catches possible paragraphs. The first rule inside this range makes start lines trigger clearing the hold space, all other lines, except for the last in the paragraph add to the hold space content.

The second rule triggers when the last line is reached. Right now there all lines save for the last one in the hold space and the last one in the pattern space. Pattern and hold space are exchanged and the hold space (now only the last line) is appended to the pattern space. This makes the whole paragraph available in pattern space.

The next rule simply decides if it is printed or not, then the pattern is discarded and the next cycle starts. The leftover in the hold space is taken care by the next triggering of the first rule.

I also removed a superfluous trailing dot in the rule deciding the printing.

I hope this helps.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk with sed to combine lines and remove specific odd # pattern from line

In the awk piped to sed below I am trying to format file by removing the odd xxxx_digits and whitespace after, then move the even xxxx_digit to the line above it and add a space between them. There may be multiple lines in file but they are in the same format. The Filename_ID line is the last line... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. UNIX for Beginners Questions & Answers

Sed/awk join lines once pattern found

Hi all OS - RHEL6.4 I have input file -f1.txt I need to search line which starts with \Start and read next line till it gets blank line and join them all. I need to trim any trailing spaces for each line.So output.txt should be.. \Start\now\fine stepwatch this space for toolsends... (7 Replies)
Discussion started by: krsnadasa
7 Replies

3. Shell Programming and Scripting

Using awk or sed to find a pattern that has lines before and after it

Dear gurus, Please help this beginner to write and understand the required script. I am looking for useing awk for sed. I have a few thousand lines file whose contain are mostly as below and I am trying to achieve followings. 1. Find a string, say user1. Then hash the line containing the... (6 Replies)
Discussion started by: ran_bon_78
6 Replies

4. Shell Programming and Scripting

sed -- Find pattern -- print remainder -- plus lines up to pattern -- Minus pattern

The intended result should be : PDF converters 'empty line' gpdftext and pdftotext?xml version="1.0"?> xml:space="preserve"><note-content version="0.1" xmlns:/tomboy/link" xmlns:size="http://beatniksoftware.com/tomboy/size">PDF converters gpdftext and pdftotext</note-content>... (9 Replies)
Discussion started by: Klasform
9 Replies

5. Shell Programming and Scripting

Replacing lines matching a multi-line pattern (sed/perl/awk)

Dear Unix Forums, I am hoping you can help me with a pattern matching problem. What am I trying to do? I want to replace multiple lines of a text file (that match a multi-line pattern) with a single line of text. These patterns can span several lines and do not always have the same number of... (10 Replies)
Discussion started by: thefang
10 Replies

6. Shell Programming and Scripting

Sed/awk/perl command to replace pattern in multiple lines

Hi I know sed and awk has options to give range of line numbers, but I need to replace pattern in specific lines Something like sed -e '1s,14s,26s/pattern/new pattern/' file name Can somebody help me in this.... I am fine with see/awk/perl Thank you in advance (9 Replies)
Discussion started by: dani777
9 Replies

7. Shell Programming and Scripting

How to print the lines between the pattern using awk/grep/sed?

Hi, I need a help to search a pattern and print the multiple lines between them. Input file: Tue May 29 12:30:33 EDT 2012:threadWebContainer : 357:com.travimp.hotelierlinks.abba.service.RequestHandler.requestService(String, ITICSDataSet): hotelCancelReservation request: ... (4 Replies)
Discussion started by: aroragaurav.84
4 Replies

8. Shell Programming and Scripting

sed/awk : how to delete lines based on IP pattern ?

Hi, I would like to delete lines in /etc/hosts on few workstations, basically I want to delete all the lines for a list of machines like this : for HOST in $(cat stations.lst |uniq) do # echo -n "$HOST" if ping -c 1 $HOST > /dev/null 2>&1 then HOSTNAME_val=`rsh $HOST "sed... (3 Replies)
Discussion started by: albator1932
3 Replies

9. Shell Programming and Scripting

sed/awk to insert multiple lines before pattern

I'm attempting to insert multiple lines before a line matching a given search pattern. These lines are generated in a separate function and can either be piped in as stdout or read from a temporary file. I've been able to insert the lines from a file after the pattern using: sed -i '/pattern/... (2 Replies)
Discussion started by: zksailor534
2 Replies

10. Shell Programming and Scripting

How to awk/sed/grep lines which contains a pattern at a given position

Dear friends I am new to linux and was trying to split some files userwise in our linux server. I have a data file of 156 continuous columns named ecscr final. I want the script to redirect all the lines containing a pattern of 7 digits to separate files. I was using grep to do that,... (2 Replies)
Discussion started by: anoopvraj
2 Replies
Login or Register to Ask a Question