ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
$ 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).
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
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
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:
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?
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.
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
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.
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.
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
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
}
' "$@"
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. :-)
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.