LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 02-20-2009, 05:35 AM   #1
anjanesh
Member
 
Registered: Sep 2004
Location: Navi Mumbai
Distribution: Ubuntu 14.04 64-bit
Posts: 230

Rep: Reputation: 30
sed: pattern matching with newlines


Hi

How do I get sed to read patterns within newlines ?

Code:
sed -i 's|hello.*?world|just-hello|g' *.txt
sed -i 's|hello.*?\n.*?world|just-hello|g' *.txt
Thanks
 
Old 02-20-2009, 06:31 AM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
You need to build up more than one line in either the pattern space or the hold space. Then have commands that use `\n' for the newline. The $ metacharacter will match the end of the buffer and not an embedded newline.

Use the N command to append to the pattern space, and H to append to the hold space.

Code:
cat testfile.txt
Hello World.
Hello
World.

Hello cruel world.
Code:
sed '/Hello/{ s/Hello World/Just Hello/;t
                               N;/Hello\nWorld/{N;s//Just\nHello/}
                             }' testfile.txt
Just Hello.
Just
Hello.

Hello cruel world.

Last edited by jschiwal; 02-20-2009 at 06:32 AM.
 
Old 02-20-2009, 06:36 AM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Or maybe use a tool better designed for the task - perl, or even awk.
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
complicated pattern matching with awk or sed... alirezan1 Linux - Newbie 1 10-10-2008 06:45 PM
pattern matching nadeemr Linux - Newbie 8 06-13-2007 11:05 AM
AWK/SED Multiple pattern matching over multiple lines issue GigerMalmensteen Programming 15 12-03-2006 05:08 PM
Sed pattern matching digitalbrutus Programming 1 08-20-2006 01:37 PM
pattern matching problem in sed digitalbrutus Programming 4 08-20-2006 04:40 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 11:08 AM.

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