SED question: entering a blank line above a character searched and

SED question: entering a blank line above a character searched and

am 28.12.2007 08:01:01 von Kompu Kid

Hello All:

After finding a ":", I would like to enter a blank line above the line
that the colon is found.

How do I do this with SED?

Deguza

Re: SED question: entering a blank line above a character searchedand found

am 28.12.2007 08:51:53 von Cyrus Kriticos

Kompu Kid wrote:
> After finding a ":", I would like to enter a blank line above the line
> that the colon is found.
>
> How do I do this with SED?

[GNU sed]

sed 's/\(.*:.*\)/\n\1/' filename

--
Best regards | Be nice to America or they'll bring democracy to
Cyrus | your country.

Re: SED question: entering a blank line above a character searched

am 28.12.2007 09:40:41 von Rakesh Sharma

sed -e '
/:/!b
H
s/.*//
x
' < yourfile

On Dec 28, 12:01 pm, Kompu Kid wrote:
> Hello All:
>
> After finding a ":", I would like to enter a blank line above the line
> that the colon is found.
>
> How do I do this with SED?
>
> Deguza

Re: SED question: entering a blank line above a character searched

am 28.12.2007 09:46:54 von Rakesh Sharma

# 1
/:/!b
H
s/.*//
x

# 2
/:/!b
G
s/^\(.*\)\(.\)$/\2\1/

# 3
/:/i\


# 4
/:/s/^/\
/

# 5
/:/!b
x;p;x




On Dec 28, 12:01 pm, Kompu Kid wrote:
> Hello All:
>
> After finding a ":", I would like to enter a blank line above the line
> that the colon is found.
>
> How do I do this with SED?
>
> Deguza

Re: SED question: entering a blank line above a character searchedand found

am 28.12.2007 18:23:11 von Ed Morton

On 12/28/2007 1:01 AM, Kompu Kid wrote:
> Hello All:
>
> After finding a ":", I would like to enter a blank line above the line
> that the colon is found.
>
> How do I do this with SED?

Appologies if there's some reason you NEED to use sed, but often people request
a solution with one tool because they don't know the alternatives. In this case:

awk '/:/{print ""}1' file

In general sed should only be used for simple substitutions.

Ed.

Re: SED question: entering a blank line above a character searchedand found

am 28.12.2007 20:23:42 von Icarus Sparry

On Fri, 28 Dec 2007 11:23:11 -0600, Ed Morton wrote:

> On 12/28/2007 1:01 AM, Kompu Kid wrote:
>> Hello All:
>>
>> After finding a ":", I would like to enter a blank line above the line
>> that the colon is found.
>>
>> How do I do this with SED?
>
> Appologies if there's some reason you NEED to use sed, but often people
> request a solution with one tool because they don't know the
> alternatives. In this case:
>
> awk '/:/{print ""}1' file
>
> In general sed should only be used for simple substitutions.
>
> Ed.

OK, so lets look at the two programs.

awk

/:/{print ""}
1

This requires you to know the idiom '1' to trigger the default action,
which is not obvious. So a more reasonable program to use would be

/:/{print ""}
{print}

Contrast this with the sed program

/:/i\


They are both 2 lines long, they take about the same amount of time to
run. Personally I find the sed program "when you see a ':' insert a blank
line" more readable than the awk program "when you see a ':' print a
blank line. For every line, print it". Using the GNU versions of awk &
sed, we see that awk is 5 times the size.

So apart from Ed's dogma "In general sed should only be used for simple
substitutions", there is no reason in this case to use awk rather than
sed.

Both awk and sed are useful tools. If you can only learn one tool then
learn perl (or python or ruby or whatever the latest scripting language
flavour of the month is). If you can learn more than one, then learn both
sed and awk and use them at the correct times.

Re: SED question: entering a blank line above a character searchedand found

am 28.12.2007 21:29:05 von Ed Morton

On 12/28/2007 1:23 PM, Icarus Sparry wrote:
> On Fri, 28 Dec 2007 11:23:11 -0600, Ed Morton wrote:
>
>
>>On 12/28/2007 1:01 AM, Kompu Kid wrote:
>>
>>>Hello All:
>>>
>>>After finding a ":", I would like to enter a blank line above the line
>>>that the colon is found.
>>>
>>>How do I do this with SED?
>>
>>Appologies if there's some reason you NEED to use sed, but often people
>>request a solution with one tool because they don't know the
>>alternatives. In this case:
>>
>>awk '/:/{print ""}1' file
>>
>>In general sed should only be used for simple substitutions.
>>
>> Ed.
>
>
> OK, so lets look at the two programs.
>
> awk
>
> /:/{print ""}
> 1
>
> This requires you to know the idiom '1' to trigger the default action,
> which is not obvious.

Right, but since the program is so small it's the perfect opportunity to learn
it and once you understanfd that small program it's so easy to do so much more
in future.

So a more reasonable program to use would be
>
> /:/{print ""}
> {print}
>
> Contrast this with the sed program
>
> /:/i\
>
>
> They are both 2 lines long, they take about the same amount of time to
> run. Personally I find the sed program "when you see a ':' insert a blank
> line" more readable than the awk program "when you see a ':' print a
> blank line. For every line, print it". Using the GNU versions of awk &
> sed, we see that awk is 5 times the size.

It doesn't matter. The awk program introduces a style that's easy to build on in
future as your requirements evolve. The sed one's just something that isn't too
hard to do IN THIS PARTICULAR case with sed, but if you know how to do it in awk
then it's a waste of time learning this particular sed-ism.

> So apart from Ed's dogma "In general sed should only be used for simple
> substitutions", there is no reason in this case to use awk rather than
> sed.

It's a "for the future" thing.

> Both awk and sed are useful tools.

Absolutely. Just pick the right one for the right job.

> If you can only learn one tool then
> learn perl (or python or ruby or whatever the latest scripting language
> flavour of the month is).

I disagree. I think there may be jobs/environments where perl or ruby are useful
but it seems like sed and awk are more readily available and easier to understand.

> If you can learn more than one, then learn both
> sed and awk and use them at the correct times.

That I agree with.

Ed.