Bookmarks

Yahoo Gmail Google Facebook Delicious Twitter Reddit Stumpleupon Myspace Digg

Search queries

sqldatasource dal, wwwxxxenden, convert raid5 to raid 10 mdadm, apache force chunked, nrao wwwxxx, xxxxxdup, procmail change subject header, wwwXxx not20, Wwwxxx.doks sas, linux raid resync after reboot

Links

XODOX
Impressum

#1: Filtering chars with sed

Posted on 2008-04-22 16:23:11 by tomasorti

Hi.
I'm trying to do this with sed, but can't get it.
I'm not a sed guru. Can anyone help 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...

Report this message

#2: Re: Filtering chars with sed

Posted on 2008-04-22 16:32:15 by Dave B

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 help 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.

Report this message

#3: Re: Filtering chars with sed

Posted on 2008-04-22 16:34:53 by Dave B

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.

Report this message

#4: Re: Filtering chars with sed

Posted on 2008-04-22 17:29:46 by tomasorti

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.

Report this message

#5: Re: Filtering chars with sed

Posted on 2008-04-22 17:41:29 by Dave B

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.

Report this message

#6: Re: Filtering chars with sed

Posted on 2008-04-22 17:50:57 by Bill Marcum

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
>>

Report this message

#7: Re: Filtering chars with sed

Posted on 2008-04-22 18:09:38 by Scott McMillan

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 help 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

Report this message

#8: Re: Filtering chars with sed

Posted on 2008-04-23 00:49:45 by Maxwell Lol

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

Report this message