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

Filtering chars with sed

Réponse
 
LinkBack Outils de la discussion
Vieux 22/04/2008, 15h23   #1
tomasorti
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Filtering chars with sed

Hi.
I'm trying to do this with sed, but can't get it.
I'm not a sed guru. Can anyone me?

I have a file with long paths like:

/home/user/dir1/dir2/libXXX.a
/usr/dir1/dir2/libYYY.a
etc...

I wan't to get just the lib stuff like:

libXXX.a
libYYY.a
etc...

How can I do that with sed? Is it the best approach?
Thanks in advance.
Tom

PD: And if I what to get just

libXXX
libYYY
etc...
  Réponse avec citation
Vieux 22/04/2008, 15h32   #2
Dave B
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Filtering chars with sed

On Tuesday 22 April 2008 16:23, tomasorti wrote:

> Hi.
> I'm trying to do this with sed, but can't get it.
> I'm not a sed guru. Can anyone me?
>
> I have a file with long paths like:
>
> /home/user/dir1/dir2/libXXX.a
> /usr/dir1/dir2/libYYY.a
> etc...
>
> I wan't to get just the lib stuff like:
>
> libXXX.a
> libYYY.a
> etc...
>
> How can I do that with sed? Is it the best approach?


Try this:

sed 's%^.*/%%' yourfile

> Thanks in advance.
> Tom
>
> PD: And if I what to get just
>
> libXXX
> libYYY
> etc...


sed 's%^.*/%%;s%\.a$%%' yourfile

or

sed 's%^.*/\([^/]*\)\.a$%\1%' yourfile'

--
D.
  Réponse avec citation
Vieux 22/04/2008, 15h34   #3
Dave B
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Filtering chars with sed

On Tuesday 22 April 2008 16:32, Dave B wrote:

> sed 's%^.*/%%' yourfile


Actually, the ^ isn't even necessary due to greedy matching, so it's just

sed 's%.*/%%' yourfile

and

sed 's%.*/%%;s%\.a$%%' yourfile
sed 's%.*/\([^/]*\)\.a$%\1%' yourfile'

--
D.
  Réponse avec citation
Vieux 22/04/2008, 16h29   #4
tomasorti
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Filtering chars with sed

Thank you very much, Dave.
I can't find an online sed tutorial about the uses of % in sed.
At least, I can't find it in here:
http://www.grymoire.com/Unix/Sed.html
and it is the first match for "sed tutorial" on Google.
Could you link me one?

On Apr 22, 4:34 pm, Dave B <da...@addr.invalid> wrote:
> On Tuesday 22 April 2008 16:32, Dave B wrote:
>
> > sed 's%^.*/%%' yourfile

>
> Actually, the ^ isn't even necessary due to greedy matching, so it's just
>
> sed 's%.*/%%' yourfile
>
> and
>
> sed 's%.*/%%;s%\.a$%%' yourfile
> sed 's%.*/\([^/]*\)\.a$%\1%' yourfile'
>
> --
> D.


  Réponse avec citation
Vieux 22/04/2008, 16h41   #5
Dave B
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Filtering chars with sed

On Tuesday 22 April 2008 17:29, tomasorti wrote:

> Thank you very much, Dave.
> I can't find an online sed tutorial about the uses of % in sed.
> At least, I can't find it in here:
> http://www.grymoire.com/Unix/Sed.html
> and it is the first match for "sed tutorial" on Google.
> Could you link me one?


Well, s%...%...% is just like s/.../.../ (you find the latter in all sed
tutorials). Sed takes whatever character it finds after the "s" as the
separator, so you can use any character (I used %).
Usually, a character different than / is used when the patterns involved
contain themselves slashes, to avoid cluttering the expressions with too
many escapes. In your case, compare

sed 's/.*\///' yourfile

with

sed 's%.*/%%' yourfile

Not a big difference here, but suppose you wanted to replace "//a//b//c//d"
with "foo", then you would have to do

sed 's/\/\/a\/\/b\/\/c\/\/d/foo/' yourfile

Changing the separator (for instance ":") results in

sed 's://a//b//c//d:foo:' yourfile

which imho is more readable.

--
D.
  Réponse avec citation
Vieux 22/04/2008, 16h50   #6
Bill Marcum
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Filtering chars with sed

On 2008-04-22, tomasorti <tomasorti@gmail.com> wrote:
>
>
> Thank you very much, Dave.
> I can't find an online sed tutorial about the uses of % in sed.
> At least, I can't find it in here:
> http://www.grymoire.com/Unix/Sed.html
> and it is the first match for "sed tutorial" on Google.
> Could you link me one?
>

'%' isn't special in sed, but the 's' command lets you use any
character including '%' as a delimiter.

>> On Tuesday 22 April 2008 16:32, Dave B wrote:
>>
>> > sed 's%^.*/%%' yourfile

>>

  Réponse avec citation
Vieux 22/04/2008, 17h09   #7
Scott McMillan
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Filtering chars with sed

On Tue, 22 Apr 2008 07:23:11 -0700 (PDT), tomasorti
<tomasorti@gmail.com> wrote:

>Hi.
>I'm trying to do this with sed, but can't get it.
>I'm not a sed guru. Can anyone me?
>
>I have a file with long paths like:
>
>/home/user/dir1/dir2/libXXX.a
>/usr/dir1/dir2/libYYY.a
>etc...
>
>I wan't to get just the lib stuff like:
>
>libXXX.a
>libYYY.a
>etc...
>
>How can I do that with sed? Is it the best approach?
>Thanks in advance.
>Tom
>
>PD: And if I what to get just
>
>libXXX
>libYYY
>etc...


Does

basename /home/user/dir1/dir2/libXXX.a

give you what you want?


Scott McMillan
  Réponse avec citation
Vieux 22/04/2008, 23h49   #8
Maxwell Lol
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Filtering chars with sed

tomasorti <tomasorti@gmail.com> writes:

> Thank you very much, Dave.
> I can't find an online sed tutorial about the uses of % in sed.
> At least, I can't find it in here:
> http://www.grymoire.com/Unix/Sed.html


It's there. See http://www.grymoire.com/Unix/Sed.html#uh-2
-----------------------------------
Title: The slash as a delimiter

The character after the s is the delimiter. It is conventionally a
slash, because this is what ed, more, and vi use. It can be anything
you want, however. If you want to change a pathname that contains a
slash - say /usr/local/bin to /common/bin - you could use the
backslash to quote the slash:

sed 's/\/usr\/local\/bin/\/common\/bin/' <old >new

Gulp. Some call this a 'Picket Fence' and it's ugly. It is easier to
read if you use an underline instead of a slash as a delimiter:

sed 's_/usr/local/bin_/common/bin_' <old >new

Some people use colons:

sed 's:/usr/local/bin:/common/bin:' <old >new

Others use the "|" character.

sed 's|/usr/local/bin|/common/bin|' <old >new

Pick one you like. As long as it's not in the string you are looking
for, anything goes. And remember that you need three delimiters. If
you get a "Unterminated `s' command" it's because you are missing one
of them.
--------------------------


  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 12h50.


É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
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,17389 seconds with 16 queries