LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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-25-2008, 01:08 PM   #16
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63

Hmmm, not working here.
Code:
$ echo "I am abba abba am I" | bash ./pal.sh  
Yes

$ echo "I am abba bba am I" | bash ./pal.sh   
Yes

$ echo "I am abba bb am I" | bash ./pal.sh   
Yes

$ echo "I am abb" | bash ./pal.sh           
Yes

$ echo "No workie" | bash ./pal.sh 
Yes
It won't work with older sed's I'm afraid. On NetBSD:

Code:
$ echo "I am abba abba am I" | ./pal.sh  
sed: 1: "2 {/\n/!G;s/\(.\)\(.*\n ...": bad flag in substitute command: '}'
Regardless, that's multiple sed's. It has to be accomplished with one sed script (and hence invocation).

Last edited by Mr. C.; 07-25-2008 at 01:09 PM.
 
Old 07-25-2008, 01:59 PM   #17
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Is there a prize for solving this puzzle?

How many people are known to have solved it?

When you say "one sed script", I read it to mean that the word "sed" can only appear once. For example, this would be legal:

sed -e 'stuff' -e 'morestuff' ....etc.
 
Old 07-25-2008, 03:27 PM   #18
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
Quote:
Originally Posted by pixellany View Post
Is there a prize for solving this puzzle?
Knowledge!

Ok, maybe I'll send $5 US. PM/email me the answers, and I'll post solutions if you want to allow everyone to get a chance to solve.

Quote:
Originally Posted by pixellany View Post
How many people are known to have solved it?
Nobody thus far has taken the challenge. I solved it years ago (along with all sorts of torturous sed scripts, using sed as a basic dual-register state machine).
Quote:
Originally Posted by pixellany View Post
When you say "one sed script", I read it to mean that the word "sed" can only appear once. For example, this would be legal:

sed -e 'stuff' -e 'morestuff' ....etc.
Sure, sed -e A -e B is really the same as :

Code:
$ sed -f sedfile
$ cat sedfile:
A
B
so I'd call this transmutable.
 
Old 07-26-2008, 01:59 AM   #19
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 360

Rep: Reputation: 170Reputation: 170
Quote:
Originally Posted by Mr. C.
Hmmm, not working here.
Code:
$ echo "No workie" | bash ./pal.sh
Yes
This is because the script is meant to be used with input from a command-line parameter e.g.
Code:
$ bash ./pal.sh "No workie"
No
If no parameter is given the script behaves as though it has been given a null string to consider and returns "Yes" (a null string is treated as a trivial case of a palindrome).
To convert the script to use piped input just remove
echo $1 |
from the first line of the script code and then using GNU sed version 4.1.4 it works with all those examples of piped input where you gave it as not working.

Quote:
Originally Posted by Mr. C.
It won't work with older sed's I'm afraid
Is it part of the challenge that the script has to work with every version of sed?
 
Old 07-26-2008, 02:13 AM   #20
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
I saw and knew that! Code that requires an argument should error out when there is no argument present. Since sed is a filter, a sed script should also act in like fashion. The challenge is "make a sed script". What you have is a shell script that calls sed ... several times. But still nice!

Since I didn't indicate which platform, it should work on any platform. Obviously this isn't reasonable if you don't have multiple environments in which to test. So we'll just call portability a bonus.
 
Old 07-27-2008, 01:21 AM   #21
mcv
LQ Newbie
 
Registered: Jul 2008
Location: São Paulo - Brasil
Distribution: Debian (amd64)
Posts: 2

Rep: Reputation: 0
Simple and efficacious.

Quote:
a="Assim a aluna anula a missa"
echo "$a"|sed -e 's/\s//g' -e ':loop; s/^\(.\)\(.*\)\1$/\2/gi; t loop' -e 's/..\+/No/g' -e 's/^$\|^.$/Yes/g'

Last edited by mcv; 07-27-2008 at 06:07 AM.
 
Old 07-27-2008, 11:39 AM   #22
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
Very nice.

No need for your /g flag on the substitution in the loop.
 
Old 07-27-2008, 04:01 PM   #23
mcv
LQ Newbie
 
Registered: Jul 2008
Location: São Paulo - Brasil
Distribution: Debian (amd64)
Posts: 2

Rep: Reputation: 0
Really. Its necessary only in the first command of the sed. But because it dont affect the result, I forgot remove it.



_

Last edited by mcv; 07-27-2008 at 04:46 PM.
 
Old 01-13-2009, 02:52 PM   #24
was6guy
LQ Newbie
 
Registered: Jan 2009
Posts: 2

Rep: Reputation: 0
Need help with the same topic...

Im trying to search a text file for a specific string, if the string is present, i'd like to insert a newline below the string with a value.

This works fine:


Code:
#!/bin/sh

sed '/START NEW MAPPING RULES/ a\
'TEST 1.conf >> 1.conf.bak


This inserts TEST into the file below the search criteria. But when I need to supply the real world string, sed blows up. This example works via cmd line:



Code:
echo 1 | sed 's/1/proxy            \/*      http:\/\/server-tstb\.com\.com:12345\/*        server-tstb\.com\.com/g'

But when trying the same below sed fails in the script.

#!/bin/sh

sed '/START NEW MAPPING RULES/ a\
'proxy            \/*      http:\/\/server-tstb\.com\.com:12345\/*        server-tstb\.com\.com 1.conf >> 1.conf.bak
 
Old 01-13-2009, 08:28 PM   #25
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
To generally replace TEST with some arbitrary string in your shell script, you'll need to escape the metacharacters for both sed's consumption, and the shell as well. This get's tricky very quickly. Any time you can eliminate one quoting layer the better. So you might find perl's qr// operator easier to use.

How / where is the TEST string being assigned?
 
Old 01-13-2009, 10:05 PM   #26
was6guy
LQ Newbie
 
Registered: Jan 2009
Posts: 2

Rep: Reputation: 0
The string im searching for is in a file that grows, but there's only one occurrence of the string in the file. Once I find the string, I need to write something along the lines of this right below the string.


proxy /* http://server-tstb.com.com:12345/* server-tstb.com.com
 
Old 01-14-2009, 12:42 AM   #27
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
Ok, this isn't so bad the via the shell. Since that's the direction you've taken, let's go that way:

Code:
#!/bin/bash

# First, assign the string we'll append to a shell variable
#
append='proxy /*xhttp://server-tstb.com.com:12345/* server-tstb.com.com'

# Place the sed pattern in double quotes will allow the append variable to expand.
# But we need to escape the sed append backslash because the shell will strip it within
# double quotes
#
sed "/pattern/ a\\
$append" 1.conf
 
Old 10-08-2009, 12:41 AM   #28
vladtz
LQ Newbie
 
Registered: Oct 2009
Posts: 3

Rep: Reputation: 0
Another approach to the palindrome challenge

All though I saw a solution in this discussion, it did niot solve the problem completely inside sed.
This solution does just that by keeping the original word in the hold space.

This is just a filter: words go in, palindromes come out.
It does not adres the Yes/No problem, but that is trivial.

#!/usr/bin/ksh

sed -n '
h
s/^/\n/
:again
s/\(.*\n\)\(.\)/\2\1/
t again
G
/^\(.*\)\n\n\1$/{
x
p
}
' "$@"
 
Old 10-08-2009, 10:46 AM   #29
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
Welcome to the party!

Good solution. To be pedantic, this is a KSH script, not a sed script. The language is KSH, which happens to call the sed command line utility with a sed script argument. :-)
 
Old 10-09-2009, 03:24 AM   #30
vladtz
LQ Newbie
 
Registered: Oct 2009
Posts: 3

Rep: Reputation: 0
Yup, you are pedantic... but, aren't we all?
Just put everything between the single quotes in a file (palindromes.sed) and run sed -f palindromes.sed file...
It will then be 100% pure sed :-)

The shell in the example doesn't add anything at all (just turns the whole thing in a convenient filter form).

Last edited by vladtz; 10-09-2009 at 03:26 AM. Reason: typos
 
  


Reply

Tags
insert, sed


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
sed insert # at the beginning of a line ilo Linux - Newbie 17 12-19-2012 08:21 AM
grep/sed/awk - find match, then match on next line gctaylor1 Programming 3 07-11-2007 08:55 AM
sed display line after pattern match inonzi_prowler Linux - Software 3 02-19-2007 01:47 PM
SED - replace / insert furquan Programming 5 03-01-2006 06:58 PM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM

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

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