Results 1 to 10 of 16
Hi folks - This is my first post to these forums so please go easy on the new guy.
Well, this question is about as basic as they get. I've ...
- 06-20-2009 #1Just Joined!
- Join Date
- Jun 2009
- Posts
- 6
Totally Perplexed With GAWK Print Function
Hi folks - This is my first post to these forums so please go easy on the new guy.

Well, this question is about as basic as they get. I've been programming for over 20 years now in a number of languages ranging from the highest level ones all the way down to assembler but never have I been as mystified as the last two days trying to learn gawk. I can't even get a handle on the PRINT function, even after hours of reading and rereading the Gawk Users Manual and trying innumerable ways to make it work.
Take the following example:
I am reading in an XML data file from weather.com It contains the following line of text:
I can easily extract the actual temperature as follows:<tmp>63</tmp>
This leaves the built-in variable $0 set to "63" and that will print just fine but I want to append an "F" to it so it reads "63F".BEGIN {null=""}
/<tmp>/ {sub ("<tmp>", null); sub ("</tmp>", null)}
Therefore I add a print statement to the end of the line like this:
But instead of printing "63F", I get "F3". So it appears that $0 consists of the string "63" plus a carriage return. This brings the insertion point back to the beginning of the current line after printing $0 but before printing "F", causing the first character of $0 to be overwritten with the "F". If instead of using concatenation to append the "F" to $0 I use a comma separated list, the output is this: " F". Using a comma separated list adds a space after each item in the list just as it's supposed to do but again $0 contains a trailing carriage return. Therefore "63" is completely overwritten by " F"./<tmp>/ {sub ("<tmp>", null); sub ("</tmp>", null); print $0 "F"}
I can't believe that I've spent the last 7 hours just trying to solve this one stupid print function issue and I'm sure I'm going to kick myself when I find out the solution.
Anyone care to enlighten a gawk newbie?
Thanks in advance,
tgeer
- 06-20-2009 #2
Use the printf function...Gerard4143
Like
printf("%s F\n", $0);
The built in variable $0 points to the current record...Make mine Arch Linux
- 06-20-2009 #3
I tried copying the data you provided and this seems to work...
Code:cat myfile | awk 'BEGIN {null=""} /<tmp>/ {sub ("<tmp>", null); sub ("</tmp>", null);printf("%sF\n",$0);}'Make mine Arch Linux
- 06-20-2009 #4
Try this...
It will display - 63°FCode:cat myfile | awk 'BEGIN {null=""} /<tmp>/ {sub ("<tmp>", null); sub ("</tmp>", null);printf("%s%c%cF\n",$0,0xc2,0xb0);}'Make mine Arch Linux
- 06-21-2009 #5Just Joined!
- Join Date
- Jun 2009
- Posts
- 6
More Perplexed Than Ever With GAWK Print Function
Gerard - thanks a bunch for taking the time to help. I especially like the code that prints the degree symbol - but what's wrong with my end of things? As far as I can tell we're executing the exact same code (I cut & pasted yours) on the exact same data yet I still have the same problem. As soon as $0 is printed the print position goes back to the beginning of the current line and then the rest is printed right over $0.
Your first example:
...produces this output: F3Code:cat myfile | awk 'BEGIN {null=""} /<tmp>/ {sub ("<tmp>", null); sub ("</tmp>", null);printf("%sF\n",$0);}'
While the second one:
...produces this output: *F (that's the degree symbol)Code:cat myfile | awk 'BEGIN {null=""} /<tmp>/ {sub ("<tmp>", null); sub ("</tmp>", null);printf("%s%c%cF\n",$0,0xc2,0xb0);}'
So maybe I'm not so thick after all. If the code you supplied works for you then several of my print methods would work also since I have the same carriage return problem with them as I have with your code.
Which brings us to the $1M question. Why the difference between our results? I noticed that you are using awk while I have been using gawk but I tried it using both with the same net result.
tgeer
- 06-21-2009 #6
On my system awk is just a link to gawk so we're using the same utility. The only thing that has to be different is the data. The data I used was just an ascii text file containing the characters <tmp>63</tmp> and it worked as expected..G4143
Make mine Arch Linux
- 06-21-2009 #7Linux User
- Join Date
- Aug 2006
- Posts
- 458
no need to use cat.
- 06-21-2009 #8
- 06-21-2009 #9Just Joined!
- Join Date
- Jun 2009
- Posts
- 6
More Perplexed Than Ever With GAWK Print Function
The difference shouldn't be with the data. My data is just a text file that's downloaded from weather.com and it contains a line of text exactly like I gave it to you Gerard. Plus I'm getting that weird carriage return print problem no matter what text input I work with.
I'm not actually using CAT. I feed the name of the input file right to my gawk program but when your code wouldn't work for me I used CAT myfile | <CODE> from the command line on the file with the same result.
So what - nobody's heard of this strange behavior before?
Bummer, I really need this script and haven't found an acceptable alternative to (g)awk. Maybe I'll try mawk or some other derivative to see if it exhibits the same bizareness.
tgeer
- 06-21-2009 #10


Reply With Quote
