Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
There's no -E option in sed so maybe you mean -e instead? While we're looking at options, you might want to use -r to get extended regular expressions. With extended regular expressions the parenthesis and braces do not need to be escaped:
Code:
sed -r -e 's/^[0-9]d([0-9]{8})([0-9]+)p([0-9]+)\.m4a$/\1 \2 \3/;'
If you are preparing to rename some files, you might look at rename instead. It uses perl and thus you get the full flexibility and power of perl regular expressions.
After a little bit of testing, I've realised that the problem is related to regex repetition, i.e. \{8\} It doesn't want to make a match and I don't really understand why.
(cross posted)
Right, I kept using the mac os style (which needs -E, not -r). I keep getting them confused
So the problem, as you stated, was not only that I didn't use -r for extended regex, but also that I was escaping the curly braces. Before trying 'rename', I'd like to understand what is going on with sed, though. The problem is that \1 still doesn't work, but if simply delete the matched string, it works - that's how I know that the string is matched.
(sed -E is the same thing as sed -r, but unix-style or mac os style - I'm not sure if it works on all unix-based operating systems. I've already figured that out )
The formula in #2 above works for me in sed (GNU sed) 4.2.2
The formula below will save the part within the parenthesis and delete everything else:
Code:
sed -r 's/^[0-9]d([0-9]{8}).*$/\1/;'
The s/// command does not need the g modifier since the substitution only needs to take place a single time. It's not necessary to tell it to go back and look again after the first replacement.
(sed -E is the same thing as sed -r, but unix-style or mac os style - I'm not sure if it works on all unix-based operating systems. I've already figured that out )
Ah. I wish GNU sed's manual page mentioned that. Non-GNU sed's pages do though. Thanks for spotting that.
But the problem still persists. \1, \2 still won't work in sed, although the pattern does match - if I simply delete the matched string, it works. So what am I doing wrong?
Yes, it does work.
I'm using sed 4.2.2 in Centos 7.3.1611. So I couldn't go more mainstream than that I guess I might just have got a little bit confused and that's why it probably didn't work. I need to practise more to understand where I have to pay attention more, I guess. I know the basics of regex but it's harder when I actually place them into a context and make them work together, etc.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.