Convert a paragraph to single line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Convert a paragraph to single line
# 1  
Old 06-07-2006
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  
Old 06-07-2006
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 03:55 PM..
# 3  
Old 06-07-2006
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  
Old 06-07-2006
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  
Old 06-07-2006
Sorry dont see the latest code on your reply

Sorry dont see the latest code on your reply
# 6  
Old 06-07-2006
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=""
   }
}

# 7  
Old 06-07-2006
Thanks a lot - It worked !!!!!!!!!!

vgersh99 - Thank you very much you made my day !!! Really appreciate your help on this. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search several string and convert into a single line for each search string using awk command AIX?.

I need to search the file using strings "Request Type" , " Request Method" , "Response Type" and by using result set find the xml tags and convert into a single line?. below are the scenarios. Cat test Nov 10, 2012 5:17:53 AM INFO: Request Type Line 1.... (5 Replies)
Discussion started by: laknar
5 Replies

2. Shell Programming and Scripting

convert single line output to multiple line

Hi all, I have a single line output like below echo $ips 10.26.208.28 10.26.208.26 10.26.208.27 want to convert above single line output as below format. Pls advice how to do ? 10.26.208.28 10.26.208.26 10.26.208.27 Regards Kannan (6 Replies)
Discussion started by: kamauv234
6 Replies

3. Shell Programming and Scripting

Multiple lines in a single column to be merged as a single line for a record

Hi, I have a requirement with, No~Dt~Notes 1~2011/08/1~"aaa bbb ccc ddd eee fff ggg hhh" Single column alone got splitted into multiple lines. I require the output as No~Dt~Notes 1~2011/08/1~"aaa<>bbb<>ccc<>ddd<>eee<>fff<>ggg<>hhh" mean to say those new lines to be... (1 Reply)
Discussion started by: Bhuvaneswari
1 Replies

4. Shell Programming and Scripting

How to extract multiple line in a paragraph? Please help.

Hi all, The following lines are taken from a long paragraph: Labels of output orbitals: RY* RY* RY* RY* RY* RY* 1\1\GINC-COMPUTE-1-3\SP\UB3LYP\6-31G\C2H5Cr1O1(1+,5)\LIUZHEN\19-Jan-20 10\0\\# ub3lyp/6-31G pop=(nbo,savenbo) gfprint\\E101GECP\\1,5\O,0,-1.7 ... (1 Reply)
Discussion started by: liuzhencc
1 Replies

5. UNIX for Advanced & Expert Users

convert rows to single row

Hi I want to convert multiple rows ro single row ,I have tried with below one but I am not getting what I am expecting.Please any idea a.txt conn1=stg conn2=dev path=\xxx\a1.txt fre=a conn1=stg conn2=dev path=\xxx\a2.txt freq=a awk '/a/{ORS=" "}{print}END{print "\n"}'... (5 Replies)
Discussion started by: akil
5 Replies

6. Shell Programming and Scripting

Concatenate lines in a single paragraph

Hi all, May you help me with this? After extract only some lines that I need from a file, I would like to concatenate the remaining lines in a single line (paragraph). Source file pattern: Event Type: Warning Event Source: Sorting Event Category: None Event ID: 1188 Date: 9/10/2008... (4 Replies)
Discussion started by: cgkmal
4 Replies

7. Shell Programming and Scripting

print first line of each paragraph

Hi everybody, Say I have a file that looks like this: How could I print a new file with only the first lines of each paragraph: (3 Replies)
Discussion started by: thedude
3 Replies

8. Shell Programming and Scripting

convert text contents/paragraph to cvs

hi all, iam new to awk & sed cmd and appreacite help from all the script expert from this forum, i have a file like below: Fri Aug 01 00:01:19 2008 NAS-IP-Address = 172.16.160.1 Class = "P1-SHT-AAA01;1217116976;70999" Calling-Station-Id = "00-11-FB-01-03-06" Acct-Status-Type = Start... (13 Replies)
Discussion started by: xajax7
13 Replies

9. Shell Programming and Scripting

script for a 3 line paragraph

i would like to ask how to make a script that in evry 3 lines of my paragraph(below) it would appear like this: $ cat myparagraph this is line 1 this is line 2 this is line3 this is line 4 this is 5 this 6 this is 7 this 8 ==================================================== $ cat... (2 Replies)
Discussion started by: invinzin21
2 Replies

10. UNIX for Dummies Questions & Answers

SED or AWK: Appending a line Identifier that changes with paragraph?

Have another question that has been eluding me all day. I have data file I'm trying to reformat so that each line is appended with an ID code, but the ID code needs to update as it searches through the file. I.e. ----Begin Original Datafile----- Condition = XXX Header Line 1 Header... (1 Reply)
Discussion started by: selkirk
1 Replies
Login or Register to Ask a Question