LinuxQuestions.org
Review your favorite Linux distribution.
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 12-06-2010, 07:42 PM   #1
neuroimager
LQ Newbie
 
Registered: Dec 2010
Posts: 3

Rep: Reputation: 0
sed to change patterns on multiple lines


Here is a sample of my data in a text file:
...
3717.5 9 0.1
3720 11 2.5
3725 8 0.1
3727.5 14 2.5
3735 9 0.1
3737.5 14 2.5
3742.5 9 0.1
3747.5 9 0.1
3750 12 2.5
...

I need to change all instances of:
"xxxx 9 xx
xxxx 14 xx"
to:
"xxxx 9 xx
xxxx 41 xx"
without changing anything else. I know i can use SED but I can't get it to work. Any ideas?
Thanks
 
Old 12-06-2010, 07:54 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 12,445

Rep: Reputation: 1068Reputation: 1068Reputation: 1068Reputation: 1068Reputation: 1068Reputation: 1068Reputation: 1068Reputation: 1068
What have you tried - what didn't work ?
 
Old 12-07-2010, 12:05 PM   #3
neuroimager
LQ Newbie
 
Registered: Dec 2010
Posts: 3

Original Poster
Rep: Reputation: 0
I have tried using sed with the N feature, but it seems that it can only detect and change strings if the values are continuous. My problem is that the important values have other numbers between them at the end and beginning of the lines.
 
Old 12-07-2010, 02:31 PM   #4
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Hi,

this works with your sample data:
Code:
sed -r '/[^ ]* 9 .*/ {N;s/([^ ]* )14( .*)/\141\2/}' file
However, it fails in case there are two consecutive lines with a '9' which are then followed by a line with '14', e.g. with this data set:
Code:
3717.5 9 0.1
3720 11 2.5
3725 8 0.1
3727.5 14 2.5
3735 9 0.1
3737.5 14 2.5
3742.5 9 0.1
3747.5 9 0.1
3737.5 14 2.5 <-- will not be changed!
3750 12 2.5
To handle the latter case the 'sed' gets a bit more complicated:
Code:
sed -r '/[^ ]* 9 .*/ {:a n;/[^ ]* 9 .*/ ba; s/([^ ]* )14( .*)/\141\2/}' file
This will also cope with the aforementioned problematic data arrangement.
 
Old 12-08-2010, 12:02 PM   #5
neuroimager
LQ Newbie
 
Registered: Dec 2010
Posts: 3

Original Poster
Rep: Reputation: 0
Great! Thanks crts.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Sed. Delete blank lines between two patterns supersoni3 Programming 5 07-29-2010 11:40 AM
SED multiple lines masterdam79 Linux - Newbie 6 10-29-2009 05:25 AM
SED how to find multiple patterns on a single line yaazz Programming 9 07-31-2009 05:20 AM
AWK/SED Multiple pattern matching over multiple lines issue GigerMalmensteen Programming 15 12-03-2006 06:08 PM
Remembering patterns and printing only those patterns using sed bernie82 Programming 5 05-26-2005 06:18 PM


All times are GMT -5. The time now is 06:01 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration