Google Answers Logo
View Question
 
Q: AWK adds extra spaces ( Answered 5 out of 5 stars,   0 Comments )
Question  
Subject: AWK adds extra spaces
Category: Computers > Algorithms
Asked by: questioner1234-ga
List Price: $5.00
Posted: 29 Oct 2003 11:36 PST
Expires: 28 Nov 2003 11:36 PST
Question ID: 270836
AWK is inserting spaces in this:
Price: $",$2,$7,

So instead of "Price: $10"
it outputs "Price: $ 10"

the database is tab-delimited and doesn't appear to have extra spaces.

Here is the whole awk:
BEGIN { FS = "\t" }
{print "<html>\n<head>\n<title>",$1,"</title>\n</head>\n<body>\n",
"\<\!--webbot bot=\"Include\" U-Include=\"../top.htm\" TAG=\"BODY\"
--\>\n<table>\n<tr>\n<td width=\"200\" valign=\"top\">\n",
"\<\!--webbot bot=\"Include\" U-Include=\"../left-nav.htm\"
TAG=\"BODY\" --\><p>&nbsp;</td>\n",
"<td valign=\"top\">\n<h1>",$1,"</h1>\n",
"<img src=\"",$5,"\"><br>Price: $",$2,$7,
"<a href=\"",$6,"\">Order Now</a>","\n",$6,"\n",
"</td>\n</tr>\n</table>\n</body>\n</html>\n" > $4}
Answer  
Subject: Re: AWK adds extra spaces
Answered By: maniac-ga on 29 Oct 2003 17:34 PST
Rated:5 out of 5 stars
 
Hello Questioner1234,

By default, the results of the print command in awk are separated by
 - output field separators (between each argument to print)
 - record field separators (between each input record processed).
The default output field separator (OFS) is a space.

Add 
  {OFS=""}
to your awk script if you want no characters between field output.

For example, I created the awk script... (a.awk)
{OFS=""}
{FS="\t"}
{print $1,$2,$3}

and the text file (a.txt)
1       a       cat
2       b       dog
3       c       horse

and processed with
  awk -f a.awk a.txt
to get
1acat
2bdog
3chorse

For more information, see the man page for awk...
  http://unixhelp.ed.ac.uk/CGI/man-cgi?awk
  http://campuscgi.princeton.edu/man?awk
or output of
  man awk
on your system.

  --Maniac
questioner1234-ga rated this answer:5 out of 5 stars
excellent answer and explanation

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy