Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getline help 2

Status
Not open for further replies.

ehope1

Technical User
Feb 4, 2005
16
0
0
US
I have the following code:
i = $1
while ($1 != "These" && $1 ~ /[A-Za-z0-9]/ || substr($1,1,4) ~ /____/)
{
getline
{
test[i++] = $0
i++
}
}

and then later I do a
printf ("%s\n",date2) >> fn
printf ("%s\n",ipc) >> fn
printf ("%s\n",domain2) >> fn
printf ("%s\n",browser) >> fn
for (i in test)
{
printf ("%s\n",test) >> fn
}
printf ("%s\n",shipto) >> fn
printf ("%s\n",shipname) >> fn

I get all the data from the getline function but the lines don't print in the order they are read by the file. Can anyone tell me how I get them to print in the order they were read?
 
simply remove the { } brackets from your getline 'block', getline is a command, not a control statement.

. Mac for productivity
.. Linux for developement
... Windows for solitaire
 
Thank you for responding. I changed the code to read the following but I still get the same results. I'm wondering if I understood what you were suggesting to me?
i = $1
while ($1 != "These" && $1 ~ /[A-Za-z0-9]/ || substr($1,1,4) ~ /____/)
{
getline
test[i++] = $0
i++
}


if (NF == 6 && $5 ~ "shipped")
{
shipto = $0
 
i missed the whole text after.. maybe test[++i] = $0 instead would work.
 
what's the point of doing two increments of 'i'?
Code:
   test[i++] = $0
   i++

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
you wanted something like this ?
i = j = $1
while (($1 != "These" && $1 ~ /[A-Za-z0-9]/) || substr($1,1,4) == "____")
{
getline
test[i++] = $0
}

and then later I do a
printf ("%s\n",date2) >> fn
printf ("%s\n",ipc) >> fn
printf ("%s\n",domain2) >> fn
printf ("%s\n",browser) >> fn
for (; j<i; ++j)
printf ("%s\n",test[j]) >> fn
printf ("%s\n",shipto) >> fn
printf ("%s\n",shipname) >> fn

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
I tried the suggestion from PHV and although I appreciate the answer, it didn't solve the problem. In answer to vgersh99, I used test[i++] = $0 i++ because I found it in an example -- I took the second + out and everything turns out right except the very first line of the output...it is going to the end of the output...I'm getting closer....still not there though....thanks to all who have provided input..

I now have:

i = $1
while ($1 != "These" && $1 ~ /[A-Za-z0-9]/ || substr($1,1,4) ~ /____/)
{
getline
test[+i] = $0
i++
}


if (NF == 6 && $5 ~ "shipped")
{
shipto = $0 (plus a whole lot more code....)

 
Change
[tt]
test[+i] = $0
i++
[/tt]
to
[tt]
test[i++] = $0
[/tt]
or
[tt]
test[++i] = $0
[/tt]
 
I get all the data from the getline function but the lines don't print in the order they are read by the file. Can anyone tell me how I get them to print in the order they were read?
Assuming that the first entry is numbered 1:
Code:
for (k=1; k in test; k++)
  print test[k]
Also, instead of[tt]
printf ("%s\n",date2) >> fn
[/tt] and so forth, do
[tt] print date2
[/tt]and run your program this way:
[tt]
awk -f prog.awk infile >outfile
[/tt]
Also, it is rarely wise to use "getline" in an Awk program. Why don't you give us some sample input and output so that we can show you how to structure your program?
 
Below is a sample of a document I'm working with...I'm basically trying to rearrange how the data is arranged plus I want to rename the file with the invoice number and last name of the person ordering. The number of products ordered and the number of lines of detail on each product can vary in length
*********************************************
Date: Mon Dec 13 10:16:26 2004
To: Toy Fun
From: Dinette Dixon <dd@earthlink.net>
Subject: 37140 - Secure Online Credit Card Order
TOTAL CHARGES: 27.90
Name: Dinettte Dixon
Street: 116 N. Harrington Lane
City: Charlotte
State or Province: NC
Zip or Postal Code: 28270
Country: USA
Phone: 7773528798
E-Mail: dd@earthlink.net
Invoice# 37140
I.P. Address:
Domain: 177.98.106.254
Expiration Month: 02
Year: 2008
CC Type: Visa
CreditCard# xxxxxxxxx
_______________________________________
passenger dog
Quantity: 1
Details: 224 Dalmatian - Black & White
Each: $ 6.00
Item Total: $ 6.00
_______________________________________
On The Move Holiday
Quantity: 1
Details: Convertible - Red
224 Dalmatian - Black & White
Each: $ 16.95
Item Total: $ 16.95
_______________________________________
Sub Total: $ 22.95
Shipping Total: (Continental USA) $ 4.95
Grand Total: $ 27.90
_______________________________________
These products will be shipped to:
Teddi Rumple
111 E. Highway
Jupiter
FL
33477
USA
7615466553
_______________________________________
Billing address:
Dinette Dixon
116 N. Harrington Lane
Charlotte
NC
28270
USA
7773528798
dd@earthlink.net
_______________________________________
This is a holiday gift.
################################################
***********************************************
I want to be able to print all the lines but also put quantity on the same line as the product....put the city and state and zip on the same line for both shipping and billing....and save the file with the invoice number and last name of the person ordering.

Any help you can give would be greatly appreciated...It's the product piece I'm having trouble with....
 
This will not rename the old file; it will create a new file whose name is made up of the invoice number and the customer's surname.
Code:
# Instead of printing the lines as they are read,
# we'll use this function to store them until all
# of the lines have been read.
function append( a,b,c,d )
{ all_of_it = all_of_it a b c d
}

/^From:/    { name = $(NF-1) }
/^Invoice#/ { invoice = $2 }
/^Sub Total:/ { reading_products = 0 }

/^(These products|Billing address).*:/,/^__/ {
  if ( /^__/ )
  { n = address_index
    # There may be an additional line for an
    # e-mail address.
    if (address[n] ~ /@/ )
    { cityline = n-5; zipline = n-3 }
    else
    { cityline = n-4; zipline = n-2 }
    for (k=1; k<=n; k++)
    { append( address[k] )
      if (k<cityline || k>=zipline )
        append( RS )
      else
        append( " " )
    }
    append( $0, RS )
    address_index = 0
  }
  else
    address[++address_index] = $0
  next
}

reading_products && !/^__/,/^__/ {
  product_line++
  append( $0 )
  if ( product_line > 1 )
    append( RS )
  else
    append( "   " )
  if ( /^__/ )
    product_line = 0
  next
}

{ append( $0, RS ) }

/CreditCard#/ { reading_products = 1 }

END {
  outfile = invoice name
  printf "%s", all_of_it  >outfile
}
For an introduction to Awk, see faq271-5564.
 
Thank you for this help. There are some things in here I've never used (like the function append) so I need to do some reading to understand it. I tried just running this as it was written against my input file and got an error message of "badly placed ()'s."... I don't know if my copy-paste added something it shouldn't have....I will play with it and see what I can do. Thanks again for the help!

 
I tried just running this as it was written against my input file and got an error message of "badly placed ()'s."... I don't know if my copy-paste added something it shouldn't have....

I copied and pasted it and it ran perfectly under standard Awk.

Very important! Many versions of Unix come with an obsolete, archaic, crippled version of Awk!

If you have nawk, use it instead of awk. Under Solaris, use /usr/xpg4/bin/awk.

There are some things in here I've never used (like the function append) so I need to do some reading to understand it.

The reason you've never seen it is that I wrote it. Awk allows you to write your own functions.

Running the code produces a file named "37140Dixon", which contains
[tt]
Date: Mon Dec 13 10:16:26 2004
To: Toy Fun
From: Dinette Dixon <dd@earthlink.net>
Subject: 37140 - Secure Online Credit Card Order
TOTAL CHARGES: 27.90
Name: Dinettte Dixon
Street: 116 N. Harrington Lane
City: Charlotte
State or Province: NC
Zip or Postal Code: 28270
Country: USA
Phone: 7773528798
E-Mail: dd@earthlink.net
Invoice# 37140
I.P. Address:
Domain: 177.98.106.254
Expiration Month: 02
Year: 2008
CC Type: Visa
CreditCard# xxxxxxxxx
_______________________________________
passenger dog Quantity: 1
Details: 224 Dalmatian - Black & White
Each: $ 6.00
Item Total: $ 6.00
_______________________________________
On The Move Holiday Quantity: 1
Details: Convertible - Red
224 Dalmatian - Black & White
Each: $ 16.95
Item Total: $ 16.95
_______________________________________
Sub Total: $ 22.95
Shipping Total: (Continental USA) $ 4.95
Grand Total: $ 27.90
_______________________________________
These products will be shipped to:
Teddi Rumple
111 E. Highway
Jupiter FL 33477
USA
7615466553
_______________________________________
Billing address:
Dinette Dixon
116 N. Harrington Lane
Charlotte NC 28270
USA
7773528798
dd@earthlink.net
_______________________________________
This is a holiday gift.
################################################
[/tt]
 
It was the syntax I was using to execute the file. This is beautiful!!! Thank you so much!!! It does exactly what I need!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top