Filtering chars with sed

Filtering chars with sed

am 22.04.2008 16:23:11 von 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...

Re: Filtering chars with sed

am 22.04.2008 16:32:15 von 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.

Re: Filtering chars with sed

am 22.04.2008 16:34:53 von 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.

Re: Filtering chars with sed

am 22.04.2008 17:29:46 von 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 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.

Re: Filtering chars with sed

am 22.04.2008 17:41:29 von 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.

Re: Filtering chars with sed

am 22.04.2008 17:50:57 von Bill Marcum

On 2008-04-22, 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?
>
'%' 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
>>

Re: Filtering chars with sed

am 22.04.2008 18:09:38 von Scott McMillan

On Tue, 22 Apr 2008 07:23:11 -0700 (PDT), 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?
>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

Re: Filtering chars with sed

am 23.04.2008 00:49:45 von Maxwell Lol

tomasorti 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/' 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_' new

Some people use colons:

sed 's:/usr/local/bin:/common/bin:' new

Others use the "|" character.

sed 's|/usr/local/bin|/common/bin|' 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.
--------------------------