LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Insert line on match only once with sed? (https://www.linuxquestions.org/questions/programming-9/insert-line-on-match-only-once-with-sed-657764/)

Mr. C. 10-09-2009 10:57 AM

Quote:

Originally Posted by vladtz (Post 3713215)
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
}


vladtz 10-10-2009 05:08 AM

OK, yes I did consider doing it that way (did forget about the -n though).
I agree completely.

John Lumby 04-30-2010 02:54 PM

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

ArthurSittler 05-06-2010 05:29 AM

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.

grail 05-06-2010 07:13 AM

Just in case another awk alternative might be considered:
Code:

awk '/^$/ && !f++ && gsub(//,"&\nnext")||1' file

MTK358 05-06-2010 09:51 AM

This thread is over 2 years old!

grail 05-06-2010 12:05 PM

oops ... sorry ... didn't look at date of original :( my bad :redface:

chuafengru 08-09-2010 04:26 PM

Variables
 
Quote:

Originally Posted by Kenhelm (Post 3224868)
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.

John Lumby 08-10-2010 09:23 AM

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}
}"


All times are GMT -5. The time now is 02:19 AM.