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 > Newbie question: delete newline character with sed
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

Newbie question: delete newline character with sed

Réponse
 
LinkBack Outils de la discussion
Vieux 27/08/2006, 00h23   #1
hzmonte@hotmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Newbie question: delete newline character with sed

From: qazwart
Date: Thurs, Apr 7 2005 9:20 am
Email: "qazwart" <qazw...@gmail.com>
Groups: comp.unix.shell

Remember that sed has more commands in it than just "s" for
substitution:

sed '/^$/d'

Any line that matches the pattern "/^$/" (blank lines) will be deleted
from the input. If you want to delete lines that may contain spaces and
tabs, but nothing else, you need to use this:

sed '/^[ \t]*$/d

The "\t" doesn't work with all versions of sed, so you might need to
use a real tab character instead.

============================
sed '/^$/d' works, i.e. removes empty lines, but sed '/^[ \t]*$/d' does
not - the lines that have tabs and spaces are still there. I tried
sed '/^[]*$/d' (a real tab between the square brackets)
and
sed '/^*$/d' (a real tab between ^ and *)
and neither works. (The real tab does not show up in the command
line.)

  Réponse avec citation
Vieux 27/08/2006, 00h55   #2
hzmonte@hotmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Newbie question: delete newline character with sed

> sed '/^[ \t]*$/d' does not work
Sorry guys, I just found out that
1. I probably missed the space inside the square brackets, and
2. I did not know I need to type a Ctrl-v before typing a real tab in
order for the shell to recognize the tab character
Now it works.
Further, sed 's/[ ]*$//'
where there is a space and a tab character between the [ and the ]
removes trailing spaces from each line.
How can I combine these two sed commands so that both empty lines and
trailing spaces (and tabs) are removed? Thanks.

  Réponse avec citation
Vieux 27/08/2006, 00h56   #3
Xicheng Jia
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Newbie question: delete newline character with sed

hzmonte@hotmail.com wrote:
> From: qazwart
> Date: Thurs, Apr 7 2005 9:20 am
> Email: "qazwart" <qazw...@gmail.com>
> Groups: comp.unix.shell
>
> Remember that sed has more commands in it than just "s" for
> substitution:
>
> sed '/^$/d'
>
> Any line that matches the pattern "/^$/" (blank lines) will be deleted
> from the input. If you want to delete lines that may contain spaces and
> tabs, but nothing else, you need to use this:
>
> sed '/^[ \t]*$/d
>
> The "\t" doesn't work with all versions of sed, so you might need to
> use a real tab character instead.
>
> ============================
> sed '/^$/d' works, i.e. removes empty lines, but sed '/^[ \t]*$/d' does
> not - the lines that have tabs and spaces are still there. I tried
> sed '/^[]*$/d' (a real tab between the square brackets)
> and
> sed '/^*$/d' (a real tab between ^ and *)
> and neither works. (The real tab does not show up in the command
> line.)


you may try "POSIX character class" to see if it s.

sed '/^[[:space:]]*$/d'

which removes lines containing only whitespaces(TAB, NEWLINE,
SPACE,.....)

Xicheng

  Réponse avec citation
Vieux 27/08/2006, 01h02   #4
Xicheng Jia
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Newbie question: delete newline character with sed


hzmo...@hotmail.com wrote:
> > sed '/^[ \t]*$/d' does not work

> Sorry guys, I just found out that
> 1. I probably missed the space inside the square brackets, and
> 2. I did not know I need to type a Ctrl-v before typing a real tab in
> order for the shell to recognize the tab character
> Now it works.
> Further, sed 's/[ ]*$//'
> where there is a space and a tab character between the [ and the ]
> removes trailing spaces from each line.
> How can I combine these two sed commands so that both empty lines and
> trailing spaces (and tabs) are removed? Thanks.


Just connect them with a simicolon:

sed '/^[ ]*$/d; s/[ ]*$//;'

Xicheng :-)

  Réponse avec citation
Vieux 27/08/2006, 01h51   #5
John W. Krahn
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Newbie question: delete newline character with sed

Xicheng Jia wrote:
> hzmo...@hotmail.com wrote:
>>>sed '/^[ \t]*$/d' does not work

>>Sorry guys, I just found out that
>>1. I probably missed the space inside the square brackets, and
>>2. I did not know I need to type a Ctrl-v before typing a real tab in
>>order for the shell to recognize the tab character
>>Now it works.
>>Further, sed 's/[ ]*$//'
>>where there is a space and a tab character between the [ and the ]
>>removes trailing spaces from each line.
>>How can I combine these two sed commands so that both empty lines and
>>trailing spaces (and tabs) are removed? Thanks.

>
> Just connect them with a simicolon:

^^^^^^^^^
An ape's digestive tract? That must be messy.


John
--
use Perl;
program
fulfillment
  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 02h44.


Édité par : vBulletin® version 3.7.2
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
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,10239 seconds with 13 queries