|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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... |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
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 >> |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
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. -------------------------- |
|
![]() |
| Outils de la discussion | |
|
|