Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Awk > A better way to...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 7 Topic 2096 of 2317
Post > Topic >>

A better way to convert spaces to tabs while joining 2 lines

by Bruce Gilmore <bruce84@[EMAIL PROTECTED] > Dec 7, 2007 at 10:58 AM

Greetings,
Here's my sample data
The "\n" represent new lines and aren't literally in the sample. 
They're there just to show where the line breaks are.


          80688351 10/28/04   07/06/05               .00        94.75 R 
      S      3         94.75  07/06/05\n
                   07/06/05 
     S\n
          60162450 02/09/05   07/07/05               .00      5592.48 G 
       H      1       5592.48  07/07/05\n
                   07/07/05 
    H\n
          30434369 07/07/04   07/08/05               .00        78.88 E 
       H      3         78.88  07/08/05\n
                   07/08/05 
    H\n
          60064052 08/27/03   07/08/05               .00        92.41 G 
       H      3         92.41  07/08/05\n
                   02/29/04 
     H\n
          30213466 07/31/03   07/11/05               .00       962.23 E 
       S      5        962.23  08/09/07\n
                   07/11/05 
       1\n


The file is space delimited and every two lines need to be joined 
together to form a single line.  The output should be tab delimited.
Here's what I came up with and it appears to work with the exception of 
an extra tab at the end of every output line.  My question is, how can 
this script be made better (i.e. more succinct or efficient)?

BEGIN { FS=" "; OFS="	" }
{ if (NR%2 == 1) {
	for (a = 1; a <= NF;  a++) {
		prev[a]=$a }
		prevNF=NF
		} else	{
		for (i = 1; i <= prevNF; i++) {
			printf "%s\t", prev[i]
			#print "Im first line", i
			}
		for (i = 1; i <= NF; i++) {
			printf "%s\t", $i
			#print "Im 2nd line", i
			}
		printf "%s\n", ""
		}	
}


Thanks.
 




 7 Posts in Topic:
A better way to convert spaces to tabs while joining 2 lines
Bruce Gilmore <bruce84  2007-12-07 10:58:52 
Re: A better way to convert spaces to tabs while joining 2 lines
Janis Papanagnou <Jani  2007-12-07 18:11:27 
Re: A better way to convert spaces to tabs while joining 2 lines
Ed Morton <morton@[EMA  2007-12-07 11:24:33 
Re: A better way to convert spaces to tabs while joining 2 lines
Ed Morton <morton@[EMA  2007-12-07 11:32:50 
Re: A better way to convert spaces to tabs while joining 2 lines
Bruce Gilmore <bruce84  2007-12-07 13:18:01 
Re: A better way to convert spaces to tabs while joining 2 lines
Ed Morton <morton@[EMA  2007-12-07 13:21:41 
Re: A better way to convert spaces to tabs while joining 2 lines
Bruce Gilmore <bruce84  2007-12-07 13:37:50 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Thu Jul 24 15:08:18 CDT 2008.