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
make multiple line containing a pattern into single line VTAWKVT Shell Programming and Scripting 13 12-04-2008 06:40 PM
How to convert a single column into several rows and columns? ashton_smith UNIX for Dummies Questions & Answers 5 05-24-2008 04:44 PM
script for a 3 line paragraph invinzin21 Shell Programming and Scripting 2 12-18-2007 01:11 AM
SED or AWK: Appending a line Identifier that changes with paragraph? selkirk UNIX for Dummies Questions & Answers 1 04-28-2007 02:03 AM
here-doc convert 2 script convert to single script? yongho Shell Programming and Scripting 2 07-07-2005 04:14 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-07-2006
rimss rimss is offline
Registered User
  
 

Join Date: Jun 2006
Posts: 8
Convert a paragraph to single line

I need to check the count of pipes on each line of the data to make sure we 4 pipes if its less i need to keep adding the string to form a single line ( Need to join/ concat the below lines until i get the 4 pipes which is end of record ). This fields is basicall a memo where the user would have typed a small paragraph that needs to be joined into a single line.

Sample Broken Lines and data
-----------------------------

467|Computer Monitor|Purchase Prise $150
Best Price $100

Cheapest Price $75


highest price $200|T|

Correct record would look like this
467|Computer Monitor|Purchase Prise $150 Best Price $100 Cheapest Price $75 highest price $200|T|

Thanks,
  #2 (permalink)  
Old 06-07-2006
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
nawk -f rim.awk myFile.txt

rim.awk:
Code:
BEGIN {
  FS=OFS="|"
  FLDmax="4"
}

NF > FLDmax { print; next }

NF {
   line=(line=="") ? $0 : line " " $0
   if ( split(line, lineA, OFS) > FLDmax ) {
      print line
      line=""
   }
}

Last edited by vgersh99; 06-07-2006 at 02:55 PM..
  #3 (permalink)  
Old 06-07-2006
rimss rimss is offline
Registered User
  
 

Join Date: Jun 2006
Posts: 8
vgersh99 Thanks a lot for the quick response.

It worked but i am getting some blank / white spaces before each text in this case it looks like this

467|Computer Monitor|Purchase Prise $150 Best Price $100
Cheapest Price $75
highest price $200T|

Sorry i tried my best to paste it could not get it to appear here. Baically there are lot of spaces between each for eg

Purchase Prise $150 <SPACE SPACE SPACE > Best Price $100 <SPACE SPACE SPACE >Cheapest Price $75<SPACE SPACE SPACE >highest price $200|T|

Would be really great if i can get it as

467|Computer Monitor|Purchase Prise $150 Best Price $100 Cheapest Price $75 highest price $200|T|

Thanks for all your help
  #4 (permalink)  
Old 06-07-2006
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
try the latest code - I've made some modifications.

Also when posting code and/or sample data files, pls use vB codes.

Pls post the exact input file using the the vB codes.
  #5 (permalink)  
Old 06-07-2006
rimss rimss is offline
Registered User
  
 

Join Date: Jun 2006
Posts: 8
Sorry dont see the latest code on your reply

Sorry dont see the latest code on your reply
  #6 (permalink)  
Old 06-07-2006
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
Code:
BEGIN {
  FS=OFS="|"
  FLDmax="4"
}

function compressSpace(str) {
  gsub(/[ ][ ]*/, " ", str)
  return str
}

NF >= FLDmax { print compressSpace($0); next }

NF {
   line=(line=="") ? $0 : line " " $0
   if ( split(line, lineA, OFS) > FLDmax ) {
      print compressSpace(line)
      line=""
   }
}
Closed Thread

Bookmarks

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 10:40 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
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