LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 05-02-2017, 03:37 AM   #1
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240

Rep: Reputation: 103Reputation: 103
separate information with \1 \2 in sed


I've got this string:
1d20170324115727p0755600111.m4a

And I'm trying to do this (separate the date from the time of the day (115727) and from the last number by a space, and drop the extension):
Code:
sed -E 's/^[0-9]d([0-9]\{8\})([0-9]+)p([0-9]+)\.m4a$/\1 \2 \3/g'
It returns the initial string. Nothing changes.
Any ideas what's going on?

Last edited by vincix; 05-02-2017 at 03:40 AM.
 
Old 05-02-2017, 03:46 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,307
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
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.
 
1 members found this post helpful.
Old 05-02-2017, 03:46 AM   #3
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240

Original Poster
Rep: Reputation: 103Reputation: 103
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)
 
Old 05-02-2017, 03:49 AM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,307
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
In addition to the above, see man 7 regex for the syntax for bounds { } and atoms ()
 
Old 05-02-2017, 03:55 AM   #5
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240

Original Poster
Rep: Reputation: 103Reputation: 103
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.
Code:
sed -r 's/^[0-9]d([0-9]{8})//g'
 
Old 05-02-2017, 03:57 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
I don't know what is sed -E. From the other hand sed and sed -r have different syntax, probably you mixed them.
 
Old 05-02-2017, 03:59 AM   #7
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240

Original Poster
Rep: Reputation: 103Reputation: 103
(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 )
 
Old 05-02-2017, 04:00 AM   #8
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,307
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
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.
 
Old 05-02-2017, 04:02 AM   #9
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,307
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
Quote:
Originally Posted by vincix View Post
(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.
 
Old 05-02-2017, 04:18 AM   #10
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240

Original Poster
Rep: Reputation: 103Reputation: 103
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?
 
Old 05-02-2017, 04:25 AM   #11
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,307
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
Which version of sed are you using? The patterns above work for me in GNU sed 4.2.2 and in OpenBSD's sed for 6.1-current.
 
1 members found this post helpful.
Old 05-02-2017, 04:27 AM   #12
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
this worked for me:
Code:
> echo '1d20170324115727p0755600111.m4a' | sed -r 's/^[0-9]d([0-9]{8})([0-9]+)p([0-9]+)\.m4a$/\1 \2 \3/g'
20170324 115727 0755600111
 
1 members found this post helpful.
Old 05-02-2017, 04:41 AM   #13
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240

Original Poster
Rep: Reputation: 103Reputation: 103
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.

Thanks both for your help.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
passing control and data information on separate channels in ns2 humairaafzal73 Linux - Software 0 02-26-2012 08:42 AM
How do I replace the text between patterns located on separate lines? (sed, awk, etc) Quon Programming 5 02-12-2012 06:27 AM
How do I separate columns using sed Basse1 Programming 4 07-01-2010 12:54 PM
sed: appending words residing in separate file mr_scary Programming 3 10-05-2006 01:25 PM
saving log information to a separate file cuss Linux - General 2 02-24-2003 11:04 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:15 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration