Results 1 to 5 of 5
  1. #1
    Join Date
    Oct 2003
    Posts
    138

    Unanswered: How can I reverse the order of two paragraphs?

    How can I reverse the order of two paragraphs?

  2. #2
    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

  3. #3
    Join Date
    Oct 2003
    Posts
    138
    Thanks,
    -valexena.

  4. #4
    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

  5. #5
    Join Date
    Jun 2002
    Location
    UK
    Posts
    525
    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 19:35.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •