Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
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 ...
  1. #1
    Just 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:
    <tmp>63</tmp>
    I can easily extract the actual temperature as follows:
    BEGIN {null=""}
    /<tmp>/ {sub ("<tmp>", null); sub ("</tmp>", null)}
    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".
    Therefore I add a print statement to the end of the line like this:
    /<tmp>/ {sub ("<tmp>", null); sub ("</tmp>", null); print $0 "F"}
    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".

    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

  2. #2
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    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

  3. #3
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    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

  4. #4
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    Try this...

    Code:
    cat myfile | awk 'BEGIN {null=""} /<tmp>/ {sub ("<tmp>", null); sub ("</tmp>", null);printf("%s%c%cF\n",$0,0xc2,0xb0);}'
    It will display - 63°F
    Make mine Arch Linux

  5. #5
    Just Joined!
    Join Date
    Jun 2009
    Posts
    6

    Question 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:
    Code:
    cat myfile | awk 'BEGIN {null=""} /<tmp>/ {sub ("<tmp>", null); sub ("</tmp>", null);printf("%sF\n",$0);}'
    ...produces this output: F3

    While the second one:
    Code:
    cat myfile | awk 'BEGIN {null=""} /<tmp>/ {sub ("<tmp>", null); sub ("</tmp>", null);printf("%s%c%cF\n",$0,0xc2,0xb0);}'
    ...produces this output: *F (that's the degree symbol)

    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

  6. #6
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    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

  7. #7

  8. #8
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    Quote Originally Posted by ghostdog74 View Post
    no need to use cat.
    Yeah I know....I'm still an (G)AWK rookie...Gerard4143
    Make mine Arch Linux

  9. #9
    Just 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

  10. #10
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    Quote Originally Posted by tgeer43 View Post
    Plus I'm getting that weird carriage return print problem no matter what text input I work with.
    tgeer
    Sounds like a problem with your terminal, or terminal settings...Try sending the AWK output to a file...Gerard4143
    Make mine Arch Linux

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...