The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
ksh scripting help praveenbvarrier Shell Programming and Scripting 1 04-15-2008 10:19 AM
Scripting Help! liezer Shell Programming and Scripting 7 09-15-2006 03:52 AM
difference between AIX shell scripting and Unix shell scripting. haroonec Shell Programming and Scripting 2 04-12-2006 08:12 AM
scripting guru's pls help me with scripting on AIX thatiprashant Shell Programming and Scripting 1 01-20-2006 07:58 PM
HELP! Need HELP scripting! adawg1283 Shell Programming and Scripting 7 09-29-2004 03:48 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-26-2008
Muki101 Muki101 is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 9
AWK scripting

I have a text file in which the text has been divided into paragraphs (two line breaks or tab marks a new paragraph) and I want to make a script which output would delete line breaks within the paragraph and the different paragraphs would be separated by two line breaks.

So, if my input file is:

Code:
     The first line.
Second line.

First line of the second paragraph.
Second line of the second paragraph.
I want the output to be something like:
Code:
The first line. Second line.

First line of the second paragraph. Second line of the second paragraph.
I have tried now for some hours to come up with something reasonable, but I seem to be heading the wrong way. I would be really pleased if someone gave their idea of how to solve the problem.

Thanks!
  #2 (permalink)  
Old 11-26-2008
Tytalus's Avatar
Tytalus Tytalus is offline Forum Advisor  
echo {1..9}^2\;|bc
  
 

Join Date: Jun 2003
Location: Scotland
Posts: 414
Code:
#  paste - - - <infile | sed G
     The first line.    Second line.

First line of the second paragraph.     Second line of the second paragraph.
  #3 (permalink)  
Old 11-26-2008
Tytalus's Avatar
Tytalus Tytalus is offline Forum Advisor  
echo {1..9}^2\;|bc
  
 

Join Date: Jun 2003
Location: Scotland
Posts: 414
or in nawk:

Code:
# nawk 'NR%3 {printf "%s ", $0;next}1(NR+1)%3{print"\n"}' infile
     The first line. Second line.

First line of the second paragraph. Second line of the second paragraph.
  #4 (permalink)  
Old 11-26-2008
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,116
And what if the paragraph is more than 2 lines long?
Code:
nawk 'BEGIN {FS=RS=""; ORS="\n\n\n"} $1=$1' infile
  #5 (permalink)  
Old 11-29-2008
Muki101 Muki101 is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 9
The output of Tytalus' code is pretty much what needed, but yes, I want it to work with paragraphs longer than 2 lines as well. But I really didn't understand the code itself very well to change it properly. Could someone explain it a bit or give ideas of how to change it?
  #6 (permalink)  
Old 11-29-2008
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,116
Quote:
Originally Posted by Muki101 View Post
The output of Tytalus' code is pretty much what needed, but yes, I want it to work with paragraphs longer than 2 lines as well. But I really didn't understand the code itself very well to change it properly. Could someone explain it a bit or give ideas of how to change it?
Have you tried my suggestion?
  #7 (permalink)  
Old 11-29-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,212
vgersh99, with your solution I get this:

Code:
$ nawk 'BEGIN {FS=RS=""; ORS="\n\n\n"} $1=$1' file
          T h e   f i r s t   l i n e .
 S e c o n d   l i n e .


F i r s t   l i n e   o f   t h e   s e c o n d   p a r a g r a p h .
 S e c o n d   l i n e   o f   t h e   s e c o n d   p a r a g r a p h .
Set the record separators to 2 newlines:

Code:
$ awk 'BEGIN {RS=ORS="\n\n"} $1=$1' file
The first line. Second line.

First line of the second paragraph. Second line of the second paragraph.
Regards
Sponsored Links
Closed Thread

Bookmarks

Tags
sed delete line

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 08:19 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0