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 > match end of line in bash
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

match end of line in bash

Réponse
 
LinkBack Outils de la discussion
Vieux 07/01/2008, 16h33   #1
artix
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut match end of line in bash

Hi All,

I have the following situation:

var="line 1
line 2
line 3"
....

I want to replace the end of line chars (LF) with a "_". I want this to
be a bash only solution and the following does not work:

echo ${var//\012/_}

Any ideas? Thanks.

--
artix
http://www.abstractart.ws _Abstract Art Directory_
  Réponse avec citation
Vieux 07/01/2008, 16h45   #2
Icarus Sparry
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: match end of line in bash

On Mon, 07 Jan 2008 17:33:36 +0100, artix wrote:

> Hi All,
>
> I have the following situation:
>
> var="line 1
> line 2
> line 3"
> ...
>
> I want to replace the end of line chars (LF) with a "_". I want this to
> be a bash only solution and the following does not work:
>
> echo ${var//\012/_}
>
> Any ideas? Thanks.


echo "${var//$'\n'/_}"
  Réponse avec citation
Vieux 07/01/2008, 16h49   #3
Stephane Chazelas
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: match end of line in bash

On Mon, 07 Jan 2008 17:33:36 +0100, artix wrote:
[...]
> I have the following situation:
>
> var="line 1
> line 2
> line 3"
> ...
>
> I want to replace the end of line chars (LF) with a "_". I want this to
> be a bash only solution and the following does not work:


bash only? You understand a shell is a command interpreter
before all, right?

> echo ${var//\012/_}


printf '%s\n' "${var//$'\n'/_}"

this should work in zsh and ksh93 as well (those syntaxes come
from ksh93)

Or POSIXly:

set -f
IFS='
'
set -- $var
IFS=_
newvar="$*"

(that squeezes several NLs into 1 "_").

Strange that you should have shell variables with several lines
in them. Smells like you script could be written in a more
shell-oriented way.

--
Stephane
  Réponse avec citation
Vieux 07/01/2008, 17h05   #4
Luuk
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: match end of line in bash


"artix" <info@abstractart.ws> schreef in bericht
news:478254bd$0$2096$edfadb0f@dtext02.news.tele.dk ...
> Hi All,
>
> I have the following situation:
>
> var="line 1
> line 2
> line 3"
> ...
>
> I want to replace the end of line chars (LF) with a "_". I want this to be
> a bash only solution and the following does not work:
>
> echo ${var//\012/_}
>
> Any ideas? Thanks.
>
> --
> artix
> http://www.abstractart.ws _Abstract Art Directory_


script:
#!/bin/bash

var="line 1# line 2# line 3"
echo -e "1: $var"

var=${var//\#/'\012'}
echo -e "2: $var"

var=${var//'\012'/_}
echo -e "3: $var"


output of this:
1: line 1# line 2# line 3
2: line 1
line 2
line 3
3: line 1_ line 2_ line 3



  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 06h56.


É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,08176 seconds with 12 queries