LinuxQuestions.org
Review your favorite Linux distribution.
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 10-09-2009, 11:57 AM   #31
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 59

Quote:
Originally Posted by vladtz View Post
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).
OK, I'll go further than hint.

The form I called pedantic requires 2 processes. Filters should by nature, since you don't know how they will be called, be as cheap as possible, and no impose unnecessary performance penalties. There is too easy a solution around that penalty in this case.

Filters should not require a user to specify arbitrary script files to include.

Filters should not require a user to type out the filter script on the command line.

So, the best solution, requires 1 process invocation, no specifying of a script file, or typing of a script. Use the kernels built in interpreter execution syntax instead, placing the contents below into a file that is then made executable:

Code:
#!/usr/pkg/bin/gsed -nf
h
s/^/\n/
:again
s/\(.*\n\)\(.\)/\2\1/
t again
G
/^\(.*\)\n\n\1$/{
x
p
}
 
Old 10-10-2009, 06:08 AM   #32
vladtz
LQ Newbie
 
Registered: Oct 2009
Posts: 3

Rep: Reputation: 0
OK, yes I did consider doing it that way (did forget about the -n though).
I agree completely.
 
Old 04-30-2010, 03:54 PM   #33
John Lumby
LQ Newbie
 
Registered: Oct 2008
Posts: 11

Rep: Reputation: 0
In case anyone else uses the posted sed script - I believe one correction is needed ---

instead of

sed '1,/^$/ {/^$/a\
aaaa
}'

it should be

sed '0,/^$/ {/^$/a\
aaaa
}'

The difference is apparent only for an input in which both the first two lines are blank.
The originally posted script will add the aaaa line twice.
The corrected one will do so only for the first blank line as specified.

John
 
Old 05-06-2010, 06:29 AM   #34
ArthurSittler
Member
 
Registered: Jul 2008
Distribution: Slackware
Posts: 124

Rep: Reputation: 30
sed might not be able to recognize palindromes

I am not totally certain about this, but I believe that sed cannot recognize arbitrarily long palindromes. Sed has the power of regular expressions, which is equivalent to deterministic finite automaton (DFA) which is also provably equivalent to a non-deterministic finite automaton (NDFA, also known as NFA). But this is not sufficient to recognize arbitrarily long palindromes. Recognizing arbitrarily long palindromes requires the power of a push-down automaton (PDA). The NFA is not equivalent to the capability of a PDA.
 
Old 05-06-2010, 08:13 AM   #35
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,689

Rep: Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987
Just in case another awk alternative might be considered:
Code:
awk '/^$/ && !f++ && gsub(//,"&\nnext")||1' file
 
Old 05-06-2010, 10:51 AM   #36
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714
This thread is over 2 years old!
 
Old 05-06-2010, 01:05 PM   #37
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,689

Rep: Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987
oops ... sorry ... didn't look at date of original my bad
 
Old 08-09-2010, 05:26 PM   #38
chuafengru
LQ Newbie
 
Registered: Jun 2010
Posts: 28

Rep: Reputation: 0
Variables

Quote:
Originally Posted by Kenhelm View Post
This inserts the newline "aaaa" only after the first blank line.
Code:
sed '1,/^$/ {/^$/a\
aaaa
}'
Is it possible to replace aaaa with a shell variable? im using bash script.
 
Old 08-10-2010, 10:23 AM   #39
John Lumby
LQ Newbie
 
Registered: Oct 2008
Posts: 11

Rep: Reputation: 0
Yes - simply use double-quotes instead of single and then explicitly quote the $ and \ chars :
e.g.

addw=foobar;echo -e "\\n\\n" | sed "0,/^\$/ {/^\$/ a \\
${addw}
}"
 
  


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 09:21 AM
grep/sed/awk - find match, then match on next line gctaylor1 Programming 3 07-11-2007 09:55 AM
sed display line after pattern match inonzi_prowler Linux - Software 3 02-19-2007 02:47 PM
SED - replace / insert furquan Programming 5 03-01-2006 07:58 PM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 07:12 AM


All times are GMT -5. The time now is 02:51 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration