LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-17-2018, 05:33 PM   #1
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240

Rep: Reputation: 103Reputation: 103
subtitute pattern that crosses new line


This is snippet of the file I'm trying to change:
Code:
1:       00:02:43:24 00:02:45:22 01:23
Why haven't you ever asked me
Warum hast du mich nie gefragt,

2:       00:02:46:03 00:02:49:04 03:01
what this film is about?
worum es in diesem Film geht?

3:       00:02:53:16 00:02:58:18 05:02
And why haven't I ever told you, anyway?
Und warum hab ich es dir eigentlich nie erzählt?

4:       00:03:02:13 00:03:07:00 04:12
Was it just you not being curious?
Warst du einfach nicht neugierig?

5:       00:03:09:09 00:03:12:00 02:16
Or also me being relieved
Oder war ich einfach erleichtert,
What I'm trying to do is to delete the colon, add a new line and delete the empty space before the time interval, so that the line starts with that line interval, like this:
Code:
1
00:02:43:24 00:02:45:22 01:23
I'd like to do it with sed. This is what I've tried so far:
Code:
sed -r -e '/^[0-9]+:/{N;s/(^[0-9]+):\n\s+/\1\n}' file.txt
I've also tried using \t+ instead of \s+, but to no avail. There's no match, the text doesn't change at all.
On the other hand, I'm not sure using \n on both sides of the substitute sentence is going to work.

I did manage to create a new line after the number with each those lines begin, but I wasn't able to delete the space that preceeds the time interval, so that's not effective:
Code:
sed -r -e '/^[0-9]+:/{N;s/(^[0-9]+):/\1\n/g}' file.txt
1
       00:02:43:24 00:02:45:22 01:23
Why haven't you ever asked me
Warum hast du mich nie gefragt,
Any ideas?

[later edit]
Now I've realised that the initial idea doesn't make sense, as there is no \n in the initial line, so there can't possibly be a match there.

So this seems to be doing what I'm looking for
Code:
sed -r -e '/^[0-9]+:/{N;s/(^[0-9]+):\s+/\1\n/g}' file.txt

Last edited by vincix; 08-17-2018 at 05:37 PM.
 
Old 08-17-2018, 06:39 PM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,258
Blog Entries: 24

Rep: Reputation: 4193Reputation: 4193Reputation: 4193Reputation: 4193Reputation: 4193Reputation: 4193Reputation: 4193Reputation: 4193Reputation: 4193Reputation: 4193Reputation: 4193
Good work!

You can simplify that a bit:

Code:
sed -r 's/^([0-9]+:)\s*/\1\n/' file.txt

Last edited by astrogeek; 08-17-2018 at 06:41 PM. Reason: Better...
 
Old 08-17-2018, 06:41 PM   #3
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240

Original Poster
Rep: Reputation: 103Reputation: 103
But I'm trying to get rid of the colon
 
Old 08-17-2018, 06:45 PM   #4
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,258
Blog Entries: 24

Rep: Reputation: 4193Reputation: 4193Reputation: 4193Reputation: 4193Reputation: 4193Reputation: 4193Reputation: 4193Reputation: 4193Reputation: 4193Reputation: 4193Reputation: 4193
Quote:
Originally Posted by vincix View Post
But I'm trying to get rid of the colon
Sorry, I missed that.

Easy to fix, simply move the capture parenthesis:

Code:
sed -r 's/^([0-9]+):\s*/\1\n/' file.txt
 
1 members found this post helpful.
Old 08-17-2018, 06:51 PM   #5
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240

Original Poster
Rep: Reputation: 103Reputation: 103
I thought \n doesn't work without N. But now I realise that what N does is simply to translate the end of line into \n, so that it can be matched, whereas in your example (and mine too actually), \n is part of the string that substitutes, not the string that is being substituted.

And yes, it doesn't make too much sense to match a string and create a { } sentence if the substituted string is going to be the same with the initial string (the one before the open brace) I'm trying to match. I see what you're getting at. Indeed, much simpler

Last edited by vincix; 08-17-2018 at 06:55 PM.
 
1 members found this post helpful.
Old 08-17-2018, 06:58 PM   #6
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,258
Blog Entries: 24

Rep: Reputation: 4193Reputation: 4193Reputation: 4193Reputation: 4193Reputation: 4193Reputation: 4193Reputation: 4193Reputation: 4193Reputation: 4193Reputation: 4193Reputation: 4193
Glad that worked!

Equally glad to see that you are trying to understand it all! Very good exercise and time well spent for us both!
 
Old 08-17-2018, 07:01 PM   #7
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240

Original Poster
Rep: Reputation: 103Reputation: 103
Thanks for the help
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
AWK - skip line if line contains pattern and print next line udiubu Linux - Newbie 15 09-06-2017 10:28 PM
[SOLVED] sed to display the pattern string, the line above it and the first line of that para rockie321 Linux - Newbie 3 04-03-2011 02:48 PM
What crosses the line into pirating? replica9000 General 64 02-18-2010 09:12 AM
printing pattern match and not whole line that matches pattern Avatar33 Programming 13 05-06-2009 06:17 AM
LXer: Microsoft Crosses a Line LXer Syndicated Linux News 0 07-10-2008 05:00 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:07 AM.

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