LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-14-2012, 11:59 AM   #1
mvairavan
Member
 
Registered: Feb 2011
Posts: 31

Rep: Reputation: 0
Sed multiline problem


I have to find pattern which is followed by pattern2 on the 3 rd line.

so if pattern1 comes in line10 there should be pattern2 in line 13.

so i got
sed ' /PATTERN1/{
N
/.*/{
N
/.*/{
N
/PATTERN2/ p
}
}
} file

It kinna works but the problem is when pattern2 isn't matched i wanted it start searching for pattern1 from wr it left while searching for pattern1.

i.e., if pattern 1 found on line 10 and pattern 2 isn't found on line 13 it should search for pattern1 again from line 11 rather its not starting from line 14 i guess.


Thanks in advance.
 
Old 07-14-2012, 02:55 PM   #2
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 360

Rep: Reputation: 170Reputation: 170
This keeps a sliding window of four lines in the pattern space as it goes through the file.
The '=' command prints out the line number of the last line of each matched group of four lines.
It finds overlapping matches, as shown in this example.
Code:
echo 'PATTERN1
text
PATTERN1
abc
PATTERN1
PATTERN2
def
PATTERN2
text' | sed -n '
1{N;N}
N
/^PATTERN1.*PATTERN2$/{p;=}
D'

PATTERN1
abc
PATTERN1
PATTERN2             # Line 6
6
PATTERN1
PATTERN2             # This is also line 6
def
PATTERN2             # Line 8
8
 
1 members found this post helpful.
Old 07-15-2012, 08:39 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Please use ***[code][/code] tags*** around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, bolding, colors, or other fancy formatting.

Vague descriptions mean people have to make guesses and assumptions, which leads to vague or unhelpful answers. If you'd provide an actual example of input text, and show us what needs to be matched/extracted/changed/whatever, maybe we could provide better solutions. sed isn't a particularly good multi-line editor, and perhaps awk or another tool would end up being more suitable.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sed variable multiline match cptsockpuppet Programming 6 04-30-2012 03:12 PM
SED multiline pattern matching AutoC Programming 1 07-18-2009 12:04 AM
Multiline sed quesion jax8 Linux - Software 2 03-31-2009 03:42 PM
sed and multiline delete aliyesami Programming 3 06-27-2008 02:03 PM
substituting multiline string in sed rohit2983 Programming 3 01-31-2007 09:00 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:48 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration