LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 06-03-2011, 10:45 AM   #1
paullanders
LQ Newbie
 
Registered: Sep 2009
Posts: 12

Rep: Reputation: 1
Limited newline replacement


I have a file where I would like to replace a newline with a space only if it follows an anchor and 2 digits:

infile:

Quote:
12
This is a line of text
1987
13
This is another line
12/14/2001
outfile:

Quote:
12 This is a line of text
1987
13 This is another line
12/14/2001
I tried the following with sed and it does not work:

Quote:
cat infile.txt | sed -r 's/(^[0-9][0-9])$/\1 /gm'
Could someone please advise on a tool to do this?

Thanks!
 
Old 06-03-2011, 11:30 AM   #2
krizzz
Member
 
Registered: Oct 2004
Location: NY
Distribution: Slackware
Posts: 200

Rep: Reputation: 30
sed -e 'N;N;s/\(^[0-9][0-9]\)\n/\1 /g'

This is it

Last edited by krizzz; 06-03-2011 at 11:33 AM.
 
Old 06-03-2011, 11:43 AM   #3
paullanders
LQ Newbie
 
Registered: Sep 2009
Posts: 12

Original Poster
Rep: Reputation: 1
This seems to partly work, but it matches only every other occurance. For example:

infile:

Quote:
12
This is a line of text
1987
13
This is another line
12/14/2001
14
This is a line of text
1987
15
This is another line
12/14/2001
outfile:

Quote:
12 This is a line of text
1987
13
This is another line
12/14/2001
14 This is a line of text
1987
15
This is another line
12/14/2001
It matched 12 and 14, but not 13 or 15.
 
Old 06-03-2011, 12:07 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Code:
awk 'ORS = /^[0-9][0-9]$/?" ":"\n"' file
 
Old 06-03-2011, 12:53 PM   #5
krizzz
Member
 
Registered: Oct 2004
Location: NY
Distribution: Slackware
Posts: 200

Rep: Reputation: 30
Quote:
Originally Posted by paullanders View Post
This seems to partly work, but it matches only every other occurance.
Yes, I edited the post right after posting it (noticed the error) buy you were quicker

This is the right syntax. You don't have to use cat, pass the file directly to the sed:

sed -e 'N;N;s/\(^[0-9][0-9]\)\n/\1 /g' filename.txt

Tested and it works.

Last edited by krizzz; 06-03-2011 at 12:55 PM.
 
Old 06-03-2011, 01:10 PM   #6
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by krizzz View Post
sed -e 'N;N;s/\(^[0-9][0-9]\)\n/\1 /g' filename.txt
This will still fail if the data looks like this:
Code:
1987
12
This is a line of text
13
This is another line
12/14/2001
14
This is a line of text
1987
15
This is another line
12/14/2001
Alternative:
Code:
sed -r '/^[0-9]{2}$/ {N;s/\n/ /}' file
 
Old 06-03-2011, 01:14 PM   #7
krizzz
Member
 
Registered: Oct 2004
Location: NY
Distribution: Slackware
Posts: 200

Rep: Reputation: 30
Code:
root@chris-desktop:/tmp# cat test.txt 
12
This is a line of text
1987
13
This is another line
12/14/2001
14
This is a line of text
1987
15
This is another line
12/14/2001
root@chris-desktop:/tmp# sed -e 'N;N;s/\(^[0-9][0-9]\)\n/\1 /g' test.txt 
12 This is a line of text
1987
13 This is another line
12/14/2001
14 This is a line of text
1987
15 This is another line
12/14/2001
root@chris-desktop:/tmp#
 
Old 06-03-2011, 01:51 PM   #8
paullanders
LQ Newbie
 
Registered: Sep 2009
Posts: 12

Original Poster
Rep: Reputation: 1
Works perfectly. Thanks so much!

I'm not familiar with the curly brackets and N in sed. Why is it:

Quote:
sed -r '/^[0-9]{2}$/ {N;s/\n/ /}' file
rather than:

Quote:
sed -r '/^[0-9]{2}$/ s/\n/ /' file
 
Old 06-03-2011, 03:45 PM   #9
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by paullanders View Post
Works perfectly. Thanks so much!

I'm not familiar with the curly brackets and N in sed.
Well, the first set of {} specifies a quantifier, i.e. sed will only match digits if there are exactly two of them.

The second pair of {} simply groups the commands 'N' and 's', i.e. only if a match occurs will those two commands be executed. Normally, one would have to escape the first set of {}. However, the '-r' option activates sed's extended RegEx mechanism. Therefore it can determine the meaning of {} by context.

The 'N' command simply reads the next line and appends it to the pattern-buffer. E.g., this is how your pattern-buffer looks like before and after 'N' is executed:
Code:
# before 'N' is executed
12

# afeter 'N' is executed
12\nThis is a line of text
Hope this clears things up a bit.
If you are interested in more details, here is one of the very best online tutorials on 'sed':
http://www.grymoire.com/Unix/Sed.html
 
  


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
[SOLVED] How to replace newline pattern in file by other newline pattern in a shell script XXLRay Linux - Software 9 11-29-2010 07:57 AM
VI replacement: newline for newline not working! jhwilliams Linux - Software 3 08-16-2007 06:11 PM
c++ file IO without newline? PatrickNew Programming 3 12-31-2006 03:24 PM
Limited Exchange Mail Server replacement albracco Linux - Software 0 10-12-2004 01:19 PM
What is a newline? raptorsoft2000 Linux - Newbie 6 08-04-2003 10:05 PM

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

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