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 > How to replace substr?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

How to replace substr?

Réponse
 
LinkBack Outils de la discussion
Vieux 04/12/2006, 12h39   #1
Bo Yang
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut How to replace substr?

for example, i have a string like
/root/dir1/dir2/file

I want to replace the leading dirs to three
tab, and the result is :

[tab][tab][tab]file

I do this because I want to print a dir tree
in a tree format in my screen.
How to achive this?
  Réponse avec citation
Vieux 04/12/2006, 12h53   #2
RolandRB
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to replace substr?


Bo Yang wrote:
> for example, i have a string like
> /root/dir1/dir2/file
>
> I want to replace the leading dirs to three
> tab, and the result is :
>
> [tab][tab][tab]file
>
> I do this because I want to print a dir tree
> in a tree format in my screen.
> How to achive this?


Try this. Put the three tabs in between the two % signs for the
substitution part.

rashlro1@ichn21>echo /one/two/three/file | sed 's%.*/% %'
file

  Réponse avec citation
Vieux 04/12/2006, 13h11   #3
Lasse Kliemann
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to replace substr?

Bo Yang <struggle@mail.nankai.edu.cn> wrote:
> for example, i have a string like
> /root/dir1/dir2/file
>
> I want to replace the leading dirs to three
> tab, and the result is :
>
> [tab][tab][tab]file


echo /root/dir1/dir2/file | sed 's%^/%%
s%[^/]*/%YYY%g'

Something like that. The YYY has to be replaced by a tab, or whatever
should be inserted instead of the three leading dirs.

Keep in mind that filenames themselves may contain tabs and even
newlines. This could lead to unexpected results with this simple
solution.


regards, lasse
  Réponse avec citation
Vieux 04/12/2006, 13h27   #4
RolandRB
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to replace substr?


Bo Yang wrote:
> for example, i have a string like
> /root/dir1/dir2/file
>
> I want to replace the leading dirs to three
> tab, and the result is :
>
> [tab][tab][tab]file
>
> I do this because I want to print a dir tree
> in a tree format in my screen.
> How to achive this?


Oh, I get you now, you want a tab for each level of the directory. This
is as near as damn it. Put the tab between the last two %s.

rashlro1@ichn21>echo /one/two/three/four/file | sed 's%[^/]*/% %g'
file

  Réponse avec citation
Vieux 04/12/2006, 13h31   #5
loic-dev@gmx.net
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to replace substr?

Hi,

> for example, i have a string like
> /root/dir1/dir2/file
>
> I want to replace the leading dirs to three
> tab, and the result is :
>
> [tab][tab][tab]file
>
> I do this because I want to print a dir tree
> in a tree format in my screen.
> How to achive this?


A solution with awk:
$ FILE="/root/dir1/dir2/file"
$ echo "$FILE" | awk -F'/' '{for(i=2; i<NF; i++) { printf "\t"}}
{print $NF}'

Cheers,
Loic.

  Réponse avec citation
Vieux 04/12/2006, 16h07   #6
Bo Yang
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to replace substr?

RolandRB :
> Bo Yang wrote:
>> for example, i have a string like
>> /root/dir1/dir2/file
>>
>> I want to replace the leading dirs to three
>> tab, and the result is :
>>
>> [tab][tab][tab]file
>>
>> I do this because I want to print a dir tree
>> in a tree format in my screen.
>> How to achive this?

>
> Oh, I get you now, you want a tab for each level of the directory. This
> is as near as damn it. Put the tab between the last two %s.
>
> rashlro1@ichn21>echo /one/two/three/four/file | sed 's%[^/]*/% %g'
> file
>

Thank you, it works, thanks!
  Réponse avec citation
Vieux 04/12/2006, 16h07   #7
Bo Yang
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to replace substr?

loic-dev@gmx.net :
> Hi,
>
>> for example, i have a string like
>> /root/dir1/dir2/file
>>
>> I want to replace the leading dirs to three
>> tab, and the result is :
>>
>> [tab][tab][tab]file
>>
>> I do this because I want to print a dir tree
>> in a tree format in my screen.
>> How to achive this?

>
> A solution with awk:
> $ FILE="/root/dir1/dir2/file"
> $ echo "$FILE" | awk -F'/' '{for(i=2; i<NF; i++) { printf "\t"}}
> {print $NF}'
>

Thanks!
  Réponse avec citation
Vieux 04/12/2006, 17h54   #8
Michael Tosch
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to replace substr?

Bo Yang wrote:
> for example, i have a string like
> /root/dir1/dir2/file
>
> I want to replace the leading dirs to three
> tab, and the result is :
>
> [tab][tab][tab]file
>
> I do this because I want to print a dir tree
> in a tree format in my screen.
> How to achive this?



If you wouldn't mind for spaces, you can use

sed 's#[^/]*/# #g'

Otherwise you can replace the two space characters
by a TAB character.

--
Michael Tosch @ hp : com
  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 13h18.


É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,12158 seconds with 16 queries