LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 07-14-2009, 02:58 PM   #16
akelder
Member
 
Registered: Jan 2007
Distribution: debian on servers, ubuntu on desktops/laptops
Posts: 45

Original Poster
Rep: Reputation: 16

Oreilly sed & awk book to the rescue!

Quote:
The replacement string recalls the first saved substring as "\1" and the second as "\2,"
which is surrounded by bold-font requests.
Great book!
 
Old 07-14-2009, 03:02 PM   #17
akelder
Member
 
Registered: Jan 2007
Distribution: debian on servers, ubuntu on desktops/laptops
Posts: 45

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by sundialsvcs View Post
awk, or the Perl programming language, would be an appropriate tool for this, because the task at hand is expressed algorithmically.

(1) Initialize a line-buffer to an empty string.

(2) While not end-of-file, read another newline-delimited string and append it to the buffer.

(3) Look within the buffer for "some text, and a date." If you find that, remove it from the head of the buffer and output it. Keep the tail of the string in the buffer.

(4) Repeat step (3) until no more matches can be found.

(5) When you reach the end-of-file, don't forget what's still in the buffer (if anything). In this case I don't think you intend to do anything with it.

This algorithm suggests itself because, in the data you provide, I see that newlines can appear anywhere in a date, which is nevertheless seen as one.

The two tools that I spoke of are "power tools" for doing this kind of string-manipulation and file parsing.
sundialsvcs, this is very helpful, thanks much. How would this be expressed in awk?
 
Old 07-14-2009, 05:47 PM   #18
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 360

Rep: Reputation: 170Reputation: 170
Code:
     1   2 3   4        5        6   7 8 9   10   11    12
sed -r ':a N; /\n[0-9]{1,5}\.\s[^\n]*$/! s/(.*)\n/\1 /; ba'
1. Tells sed to expect extended regexp
2. Creates a label to return to later
3. Appends the next line of input into the pattern space
4. Pattern starts with newline
5. Pattern to match
6. [^\n]* is a string which does not contain a newline. This limits the pattern to the last line in a multi-line pattern space. Otherwise the 'greedy matching' of * would try to match back to the second line in the pattern space.
7. Matches till the end of line
8. Negates previous expression
9. Substitutes
10. Due to 'greedy matching' .*\n matches everything up to the start of the last line in the pattern space.
(The end of the last line in the pattern space is matched by $ not by \n )
11. \1 is whatever is matched by the pattern in the ( )
i.e. Everything up to the last \n but not including it.
12. Returns to label

Last edited by Kenhelm; 07-14-2009 at 05:49 PM.
 
Old 07-14-2009, 07:46 PM   #19
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by akelder View Post
Ghostdog, this works perfectly, but could you explain how it works?

Cheers!
if line has digit + dot, print newline
for the rest of the line, using printf (without \n) will concat lines
 
  


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
printing pattern match and not whole line that matches pattern Avatar33 Programming 13 05-06-2009 06:17 AM
Putting blank line after the search pattern. dina3e Programming 2 09-21-2008 07:38 AM
Texmaker & inverse-search setting Blue Jacket Linux - Software 1 04-19-2006 03:27 PM
LXer: The Inverse Extension Design Pattern LXer Syndicated Linux News 0 12-21-2005 11:16 PM
Pattern search in a line jitz Linux - General 2 12-06-2003 04:50 AM

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

All times are GMT -5. The time now is 12:39 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