|
Prev: printf "hi\000"|egrep hi #why no STDERR output
Next: Matching Previous and Append with Sed / Awk
From: tomasorti on 22 Apr 2008 10:23 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...
From: Dave B on 22 Apr 2008 10:32 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.
From: Dave B on 22 Apr 2008 10:34 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.
From: tomasorti on 22 Apr 2008 11:29 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...(a)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.
From: Dave B on 22 Apr 2008 11:41 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.
|
Next
|
Last
Pages: 1 2 Prev: printf "hi\000"|egrep hi #why no STDERR output Next: Matching Previous and Append with Sed / Awk |