Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 11
END{ print safe[1] "\t" safe[2] "\t" safe[3] "\t" safe[4] "\t" safe[5] "\t" safe[6] "\t" safe[7] } ' dump > dump2 a2ps --output=- dump2 | ps2pdf - > forecast.pdf I am ...
  1. #1
    Just Joined!
    Join Date
    Jan 2008
    Location
    Blacksburg
    Posts
    16

    Changing text color within a gawk command in a bash script

    END{
    print safe[1] "\t" safe[2] "\t" safe[3] "\t" safe[4] "\t" safe[5] "\t" safe[6] "\t" safe[7]
    }
    ' dump > dump2
    a2ps --output=- dump2 | ps2pdf - > forecast.pdf


    I am attempting to write a bash script, and what it does doesn't really matter. My problem is that the command above will always print black to a PDF file as shown. This is because my print function is within a gawk END phrase, and I cannot use the normal printf to change the color of the text.

    Is there a way I can output this text as red within the gawk command in a bash script?

  2. #2
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    Why can't you use printf in the END phrase?

  3. #3
    Just Joined!
    Join Date
    Jan 2008
    Location
    Blacksburg
    Posts
    16
    The problem is that I don't really know how printf() works within a gawk script. I can get printf to print colors if I am just doing bash commands, but if I do it the same way in gawk the ' marks mess up the entire command.

    Basically,I'm just trying to change the color of the aforementioned output to red.

  4. #4
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    Sounds like a fairly trivial problem with escaping the single-quotes - why not post the line you're stuck on, and let us have a go at showing you how you can quote it correctly?

  5. #5
    Just Joined!
    Join Date
    Jan 2008
    Location
    Blacksburg
    Posts
    16
    Code:
    printf '\x1b\x5b1;31;40msafe[1]\tsafe[2]\tsafe[3]\tsafe[4]\tsafe[5]\tsafe[6]\tsafe[7]\n'
    This is the command I try to use with in the END block. Unfortunately, using the escape character '\' doesn't seem to work. Is there another way?

  6. #6
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    If you use double quotes " instead of single quotes ', does that fix it?
    --
    Bill

    Old age and treachery will overcome youth and skill.

  7. #7
    Just Joined!
    Join Date
    Jan 2008
    Location
    Blacksburg
    Posts
    16
    No, using double quotes just makes it literally output the statement within the quotes without any formatting.

  8. #8
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Well, now you've gone and done it. I've actually had to drag out my awk book. I never use awk. (*grumble grumble*)
    Code:
    #!/bin/sh
    
    awk 'END { printf("%c[1;31;40mabc%c[0mdef\n",27,27) } '
    There ya go.

    Kids on the dEdit:*mn lawn. (*grumble grumble*)
    --
    Bill

    Old age and treachery will overcome youth and skill.

  9. #9
    Just Joined!
    Join Date
    Jan 2008
    Location
    Blacksburg
    Posts
    16
    Unfortunately, and I don't even know if this is a difference between awk and gawk or what, but the statement you gave me outputs literally:

    ^[[1;31;40mabc^[[0mdef

    in the same color as the rest of my output. :/

    Would that just be a difference between awk and gawk? Here's the entire END block I am using if that helps:

    Code:
    END{
    print "Day:    "day[1] "\t" day[2] "\t" day[3] "\t" day[4] "\t" day[5] "\t" day[6] "\t" day[7]
    print "Status: "safe[1] "\t" safe[2] "\t" safe[3] "\t" safe[4] "\t" safe[5] "\t" safe[6] "\t" safe[7]
    print "High:   "hi[1] "\t" hi[2] "\t" hi[3] "\t" hi[4] "\t" hi[5] "\t" hi[6] "\t" hi[7] "\t" 
    print "Low:    "lo[1] "\t" lo[2] "\t" lo[3] "\t" lo[4] "\t" lo[5] "\t" lo[6] "\t" lo[7] "\t" 
    printf("%c[1;31;40mabc%c[0mdef\n",27,27)
    }
    with the output appearing as:

    Code:
    Day:    Mon	Tue	Wed	Thu	Fri	Sat	Sun
    Status: +  	+	+	+	+	OK	OK
    High:   70  	73	76	75	64	72	73	
    Low:    46  	51	59	56	45	52	53	
    ^[[1;31;40mabc^[[0mdef
    I have officially scoured the internet and can't find any good reason why printf can't change text color. is there another possible solution?

    Another quick note: I am using #!/bin/bash, not /bin/sh.

  10. #10
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    You may have problems with [g]awk, but you have problems beyond that as well.

    Please make an empty directory, copy and paste this script into a file in that directory, change the protection on that file so that it's executable, make that directory your current directory, and run this script:
    Code:
    #!/bin/bash
    
    which perl > wje.2.txt
    sed -e 's/^/#!/' wje.2.txt > wje.2.pl
    
    cat >> wje.2.pl <<EOD
    print sprintf("%c[1;31;40mabc%c[0mdef\n",27,27);
    EOD
    
    chmod 700 wje.2.pl
    wje.2.pl > wje.2.dat
    a2ps --output=wje.3.dat wje.2.dat
    od -t x1 wje.2.dat
    echo =====
    grep 'abc.*def' wje.3.dat
    echo =====
    echo $TERM
    echo =====
    cat wje.2.dat
    Don't worry about the Perl; I'm just using it because I'm more familiar with it.

    Then copy and paste the output from running that script into a response to this post.

    I'm guessing you'll get output like this:
    Code:
    [wje.2.dat (plain): 1 page on 1 sheet]
    [Total: 1 page on 1 sheet] saved into the file `wje.3.dat'
    0000000 1b 5b 31 3b 33 31 3b 34 30 6d 61 62 63 1b 5b 30
    0000020 6d 64 65 66 0a
    0000025
    =====
    (^[[1;31;40mabc^[[0mdef) p n
    =====
    xterm
    =====
    abcdef
    Also, when you post the output, please tell me whether the final line is the same color as all the rest, or whether the abc is red.

    If you get output like the above, I would conclude that a2ps doesn't interpret the escape codes normally used for printing to a window. PostScript has its own way of specifying colors, and at least in my run that wasn't used in the PostScript output. That would be a shortcoming of a2ps (from your perspective).

    So even if you get the [g]awk script to work perfectly, a2ps won't be your tool of choice.

    How do you export text to a pdf document so that some of it is in color? That's your underlying question, and I would pose that in a new thread (rather than continuing this one), so that you'll attract the right people. I'd give it a subject line like: How do I export text to pdf, partly in red?

    Hope this helps a little.
    --
    Bill

    Old age and treachery will overcome youth and skill.

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
  •  
...