AWK sub function curious problem under bash


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users AWK sub function curious problem under bash
# 1  
Old 11-11-2009
AWK sub function curious problem under bash

I need to detect the number of pages in a print job when it is available so I can warn users when they try to print a report much larger than they expected. Sometimes they are trying to print 1000 page reports when they thought they were getting a 10 page report.

Under linux I am scanning the print job before it goes to the printer and want the user to be able to cancel the print job if it is not what they expected.

This current function works, but is not exactly what I wanted:

Code:
 
function get-last-page() 
{ 
awk '/Page/{ f=$NF }; END{ print f }' $1 | awk '{ sub(/Page/,""); print }'; 
}

In my test case the page numbers can be found on the top of each page after the word "Page". -but- sometimes the page count abuts the "Page" label so the above function could return "Page2308" when there is no white space between the regex "Page" and the desired number. There is no problem when there is white space.

Simple answer, just sub out the /Page/ unconditionally and I always get the last page number of the report. -but- I originally tried it like this (using only one awk call) and I cannot for the life of me make it work properly:

Code:
 
function get-last-page() 
{
awk '/Page/{ f=$NF }; END{ print sub(/Page/,"",f); }' $1; 
}

The above function does not return the desired 2308 (or whatever). I cannot make out what is wrong. If I change it to the following I get what I expect, including the "Page2308":

Code:
 
function get-last-page() 
{
awk '/Page/{ f=$NF }; END{ print f }' $1; 
}

The above behaves as I expect, but has the error I am trying to fix with the second example. I don't understand why I can't do the substitution in the END{} clause but can do it after the pipe (as in the first example).

I really was hoping to get this to work as a sort-of one-liner.

Can anyone help me either fix this or at least understand why it does not work?

I am running under a bash shell on openSuSE 10.3 linux.

TIA
# 2  
Old 11-11-2009
Regarding function 2, sub is not a function that produces a string that you can print. This will perhaps work better:

Code:
function get-last-page() 
{
awk '/Page/{ f=$NF }; END{ sub(/Page/,"",f); print f}' $1; 
}

# 3  
Old 11-12-2009
Scutinizer! You da man!

OK I see it now. I had a version that was close but instead of "print f" I just did a print.

I love awk but only brush up against it when I need it and learn as I go. Your comment on how to view the sub function return value was exactly the point I was missing.

Thanks I am a happy camper with my new, working one-line solution! Smilie

(Are you familiar with Frank Zappa's "Central Scrutinizer" from "Joe's Garage I, II, and III"? Just wondering.)
# 4  
Old 11-12-2009
Quote:
Originally Posted by Max Rebo
Scutinizer! You da man!

OK I see it now. I had a version that was close but instead of "print f" I just did a print.

I love awk but only brush up against it when I need it and learn as I go. Your comment on how to view the sub function return value was exactly the point I was missing.

Thanks I am a happy camper with my new, working one-line solution! Smilie
Great that it works and good to hear some enthusiastic feedback...
Quote:

(Are you familiar with Frank Zappa's "Central Scrutinizer" from "Joe's Garage I, II, and III"? Just wondering.)
Very! What gave it away? SmilieSmilieSmilie . Big fan of one the most intelligent persons ever on planet Earth and his music of course (though obviously not as famous as Max Rebo, cause of the smaller audience Smilie.
# 5  
Old 11-12-2009
Ahh, so you have heard about the little blue elephant (who is not an elephant)? Exxxxcellent.

-OK- Next problem that is perplexing me (in the same large-print-file problem) is another corker... grrr.
Code:
#!/bin/bash
##### Detect potential too-large reports
function get-last-page()
{
   if [ $# == 1 ] && [ -s $1 ] && grep -qs "Page " $1 ; then # File is valid
      PAGES=`awk '/Page/{ f=$NF }; END{ sub(/Page/,"",f); print f }' $1;`
#     echo $PAGES
      if [ "$PAGES" -gt "100" ]; then
         get-operator-option $1 $PAGES "Pages"
         return $?
      fi
   fi
}
##### Ask operator how to handle too-large report
function get-operator-option()
{
   echo -e "The system has detected that your report may be larger than you"
   echo -e "expected.  You need to decide what to do with this report.\n"
   echo -e "Your $1 report is $2 $3.\n"
   echo -e "No matter what you decide your report is saved to disk now.\n"
   echo -e "Please select from the following options (Enter 1, 2, 3, or 4):\n"
   select OPTION in \
                    "Print large report anyway."                     \
                    "Don't print large report (cancel print)."       \
                    "E-mail me information on this report (cancel)." \
                    "E-mail IT department on this report (cancel)."
   do
      case \($REPLY\) in
        "1") return 0;;  # Print  report
        "2") return 1;;  # Cancel report
        "3") return 2;;  # Cancel report and email info to user and IT
#           send-too-large-email ${USER} ${PROGRAM} ${SPOOLFILE};&
        "4") return 3;;  # Cancel report and email info to IT
#           send-too-large-email "IT" ${PROGRAM} ${SPOOLFILE};;
      esac
   done
}
 
#### Test for too-large print job
if [ $(get-last-page $1) -gt 0 ]; then
   echo "exit TESTING: $?"
   read x;
fi
echo "normal TESTING: $?"
read x;

I don't know how I get myself in these fixes, but when I test my script using this function I -NEVER- see the message echo'ed at the top of my get-operator-option function. It goes straight to the select OPTION clause.

(-btw- when I type the function into my current shell and run it as an immediate function, not from a script file, I see the echo'ed text! ai!)

Why is echo not working at the top of the get-operator-option function? What silly little thing am I missing?

TIAA
# 6  
Old 11-12-2009
Sometimes "echo -e" does not work correctly. Better use printf instead. Also the \($REPLY\) seems odd. I would just use $REPLY
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk call in bash function called with arugments not working, something lost in translation?

Hello, I have this awk code in a bash script to perform a find and replace task. This finds one unique line in a file and substitutes the found line with a replacement. #! /bin/bash # value determined elsewhere total_outputs_p1=100 # file being modified... (5 Replies)
Discussion started by: LMHmedchem
5 Replies

2. Shell Programming and Scripting

Bash script: problem with a function which use colors

Hello guys :) I've a some issue with a function which use the bash colors in my script. An example : #!/bin/bash set -x log_in(){ host="srv1" remote_files="log/" LOG_FILE="logfile" green='\033]; then color_in_red=("${red}"$2"${none}") echo -e... (2 Replies)
Discussion started by: Arnaudh78
2 Replies

3. Shell Programming and Scripting

Bash function problem

I am trying to figure out why I am having a "problem" with some functions in a bash script I am running. The reason for air quoting is that the functions are working, they are just not displaying anything to screen when called from another function. Here's an example: function Create_Input {... (6 Replies)
Discussion started by: dagamier
6 Replies

4. UNIX for Dummies Questions & Answers

New problem with awk using bash

Hi! I have a new problem with awk, this time I think is because I'm using it in bash and I don't know how to put the valor of the variable in awk. Here is the code: #!/bin/bash for i in 1 2 3 4 5 do a=$i b=$ awk '$1>=a&&$1<=b {print $1,$2,$3}'>asdf test... (3 Replies)
Discussion started by: florpi
3 Replies

5. Solaris

Curious MPxIO problem

Hello folks, I have a newly installed Solaris 10 system running on a T6320 blade. I have set up LDM with the intent to move an ldom from another blade to this one. So far, so good. I had the SAN folks make the LUNs belonging to the ldom visible to my new blade and I can see them, all 4 paths.... (4 Replies)
Discussion started by: Ranck
4 Replies

6. Shell Programming and Scripting

Problem using function in awk

I created two functions that output two random variables. I want to output them in the output file. But it does not seem to work. # Function rgaussian1(r1, r2) # Gaussian random number generator function rgaussian1(r1, r2) { pi = 3.142 v1 = sqrt( -2 * log(rand()) ) v2... (18 Replies)
Discussion started by: kristinu
18 Replies

7. Shell Programming and Scripting

AWK Problem in recursive function

Hi, I have a file like this SPF_HC00001|iCalcular_Monto_Minimo|--->|SPF_HC00028|pstcObtener_Monto_Minimo SPF_HC00004|iCalcular_Incrementos|--->|SPF_HC00032|pstcObtener_Num_Incrementos SPF_HC00005|iCalcular_Articulo_167_Reformado|--->|SPF_HC00031|pstcObtener_Por_CB_Inc... (2 Replies)
Discussion started by: kcoder24
2 Replies

8. Shell Programming and Scripting

Awk problem: How to express the single quote(') by using awk print function

Actually I got a list of file end with *.txt I want to use the same command apply to all the *.txt Thus I try to find out the fastest way to write those same command in a script and then want to let them run automatics. For example: I got the file below: file1.txt file2.txt file3.txt... (4 Replies)
Discussion started by: patrick87
4 Replies

9. Shell Programming and Scripting

awk , function call problem

#!/bin/bash awk ' function ad(t,r){ return (t+r); } BEGIN{ print ad(5,3); } { print ad(5,3); } ' Doesn't print anything for the last print ad(5,3); (6 Replies)
Discussion started by: cola
6 Replies

10. Shell Programming and Scripting

problem in awk int() function

awk -vwgt=$vWeight -vfac=$vFactor ' BEGIN { printf("wgt:" wgt "\n"); printf("factor:" fac "\n"); total = sprintf("%.0f", wgt * fac); total2 = sprintf("%.0f", int(wgt * fac)); printf("total:" total "\n"); printf("total2:" total2 "\n"); } ' if vWeight=326.4 vFactor=100 the result... (2 Replies)
Discussion started by: qa.bingo
2 Replies
Login or Register to Ask a Question