If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > How can I reverse the order of two paragraphs?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-02-04, 14:27
valexena valexena is offline
Registered User
 
Join Date: Oct 2003
Posts: 138
How can I reverse the order of two paragraphs?

How can I reverse the order of two paragraphs?
Reply With Quote
  #2 (permalink)  
Old 02-03-04, 05:43
Damian Ibbotson Damian Ibbotson is offline
Padawan
 
Join Date: Jun 2002
Location: UK
Posts: 525
You could use awk...

awk 'BEGIN{RS="";OFS="\n\n"}{paragraph[NR]=$0}END{print
paragraph[2],paragraph[1]}'

But that's just code for code's sake! It's probably better to do this interactively in something like vi.

d} - delete (and buffer) from cursor position to end of paragraph
d{ - delete (and buffer) from cursor position to beginning of paragraph
p - paste buffer on line below cursor
P - paste buffer on line above cursor

HTH
Reply With Quote
  #3 (permalink)  
Old 02-06-04, 21:51
valexena valexena is offline
Registered User
 
Join Date: Oct 2003
Posts: 138
Thanks,
-valexena.
Reply With Quote
  #4 (permalink)  
Old 02-07-04, 10:12
sundialsvcs sundialsvcs is offline
Registered User
 
Join Date: Oct 2003
Posts: 706
Smile

Well, I guess "it all depends." If you do something like this all the time, then this is yet another demonstration of just how powerful awk can be. If it's a one-time task then obviously it's overkill; do it with an editor and be done. But I suspect that this isn't a one-time task.

In the example listed below, by the way, I suspect that FS ("field separator") should have been used versus OFS ("output-field separator"). But the strategy used is:
  • At the beginning of the file (BEGIN rule), set the record-separator to "nothing" and the field separator to "two adjacent newlines." Thus, awk will see the paragraphs as "fields."
  • For each record (no rule), stack up the paragraphs in an array.
  • At the end of the file (END rule), output (only) the first two paragraphs. (Arrays in awk are associative, so the subscripts, "[1]" and "[2]", are correct.)
__________________
ChimneySweep(R): fast, automatic
table repair at a click of the
mouse! http://www.sundialservices.com
Reply With Quote
  #5 (permalink)  
Old 02-07-04, 11:31
Damian Ibbotson Damian Ibbotson is offline
Padawan
 
Join Date: Jun 2002
Location: UK
Posts: 525
Quote:
Originally posted by sundialsvcs
Well, I guess "it all depends." If you do something like this all the time, then this is yet another demonstration of just how powerful awk can be. If it's a one-time task then obviously it's overkill; do it with an editor and be done. But I suspect that this isn't a one-time task.

In the example listed below, by the way, I suspect that FS ("field separator") should have been used versus OFS ("output-field separator"). But the strategy used is:
  • At the beginning of the file (BEGIN rule), set the record-separator to "nothing" and the field separator to "two adjacent newlines." Thus, awk will see the paragraphs as "fields."
  • For each record (no rule), stack up the paragraphs in an array.
  • At the end of the file (END rule), output (only) the first two paragraphs. (Arrays in awk are associative, so the subscripts, "[1]" and "[2]", are correct.)
Close. RS="" actually sets the record separator to a blank line. That is why paragraph[NR] sets the paragraph 'records' to paragraph[1] and paragraph[2]. Modifying the field separator (FS) would have no effect in this instance. I have modified the output field separator (OFS) because its default is a space but we would like the records, which are being output as fields, separated by newlines to retain the appearance of paragraphs.

If you were to read each paragraph as a field, you would have to somehow determine how to set RS in order that each paragraph 'field' belonged to the same record. In doing this, you would increase the risk of blowing the 2k record limit that most awks impose.

See... I do like awk :-)

Last edited by Damian Ibbotson; 02-07-04 at 18:35.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On