LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 02-13-2013, 10:01 AM   #1
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Question sed-with-loop, unexpected behavior


I've encountered a puzzling phenomenon while working on a different thread in this forum.

InFile1 ...
Code:
aaa
bbb
ccc
InFile2 ...
Code:
Hello
Tom
pat_start
line1
line2
pat_end
Goodbye
Tom
Code ...
Code:
W1='~'$(paste -sd"~" $InFile1)
paste -sd"~" $InFile2 > $Work2
# Note: \1 contains everything up to, but not including, pat_start.
#       \2 contains pat_start.
#       \3 contains everthing between pat_start up to but not including pat_end.
#       \4 contains pat_end.
#       \5 contains everything following pat_end.
sed -r '   s/(.*)(~pat_start)(.*)(~pat_end)(.*)/\1\2~~  \5/   ' $Work2
sed -r ':a s/(.*)(~pat_start)(.*)(~pat_end)(.*)/\1\2~~  \5/;ta' $Work2
sed -r '   s/(.*)(~pat_start)(.*)(~pat_end)(.*)/\1\2~~\4\5/   ' $Work2
sed -r ':a s/(.*)(~pat_start)(.*)(~pat_end)(.*)/\1\2~~\4\5/;ta' $Work2
For illustration the code shown has four successive seds.

The first one uses no loop and does not cite \4.
The second one uses a loop and does not cite \4.
The first and second sed generate the expected results, and they are identical.

The third one uses no loop and does cite \4.
The fourth one uses a loop and does cite \4.
The third sed generates the expected results.
I thought the third and fourth seds would generate identical results but that's not what happens.
The fourth one unlocks the keyboard and waits.

What have I done wrong?

Daniel B. Martin
 
Old 02-13-2013, 12:25 PM   #2
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi, Daniel.

The ta command jumps to label :a if last substitution was successful. Your substitution is always successful, because you first break up the string into constituents (\1..\5) and then reconstruct the same string from them. Thus you have an infinite loop. Add l command in front of ta to make it obvious.
 
1 members found this post helpful.
Old 02-13-2013, 01:50 PM   #3
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Original Poster
Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by firstfire View Post
... Thus you have an infinite loop. ...
You are right. Thank you! SOLVED!

Daniel B. Martin
 
  


Reply

Tags
sed



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
[SOLVED] sed loop gives unexpected results jgombos Programming 13 09-10-2011 12:33 AM
Ubuntu 9.10 unexpected internet behavior Zac Matthew Joseph Linux - Newbie 5 03-10-2010 11:47 PM
unexpected behavior while installing flock vineet7kumar Linux - Newbie 8 11-29-2008 12:59 AM
Unexpected Behavior (passwd command) Penguin666 Linux - General 3 11-19-2003 11:34 PM

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

All times are GMT -5. The time now is 11:40 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