PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > SED question: entering a blank line above a character searched andfound
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

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

Réponse
 
LinkBack Outils de la discussion
Vieux 28/12/2007, 07h01   #1
Kompu Kid
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut SED question: entering a blank line above a character searched andfound

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
  Réponse avec citation
Vieux 28/12/2007, 07h51   #2
Cyrus Kriticos
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: SED question: entering a blank line above a character searchedand found

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.
  Réponse avec citation
Vieux 28/12/2007, 08h40   #3
Rakesh Sharma
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: SED question: entering a blank line above a character searchedand found


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

On Dec 28, 12:01 pm, Kompu Kid <deg...@hotmail.com> 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


  Réponse avec citation
Vieux 28/12/2007, 08h46   #4
Rakesh Sharma
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: SED question: entering a blank line above a character searchedand found



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

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

# 3
/:/i\


# 4
/:/s/^/\
/

# 5
/:/!b
x;x




On Dec 28, 12:01 pm, Kompu Kid <deg...@hotmail.com> 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


  Réponse avec citation
Vieux 28/12/2007, 17h23   #5
Ed Morton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: SED question: entering a blank line above a character searchedand found



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.


  Réponse avec citation
Vieux 28/12/2007, 19h23   #6
Icarus Sparry
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: SED question: entering a blank line above a character searchedand found

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.
  Réponse avec citation
Vieux 28/12/2007, 20h29   #7
Ed Morton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: SED question: entering a blank line above a character searchedand found



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.

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 05h33.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,14713 seconds with 15 queries