sh var -> awk var -> sh var

sh var -> awk var -> sh var

Post by SIMON TNEOH CHEE BO » Mon, 20 Feb 1995 15:25:27



I want to do something like the following:
---------------------------------------------------------------------
echo "-----------end----------------"
line_num=`expr $line_num + 1`
awk '{  
       for( ; n%=xx ; n++ )  <---xx means a number.
        printf("\n")
     }' n=$page_line          <---this need input, else it stop....
line_num=$n    <-----I want $line_num = n in awk.
---------------------------------------------------------------------
I need certain length of the outfiles, so if "----end----" end at line number
which is not enough, space lines are added to enough length number.
Thanks.

--
  =====================================================================
    Simon Tneoh Chee Boon  ~{UEV>ND~}  i?

    Hsin Chu City       NATIONAL TSING HUA UNI.
    P.O.Box 2-037       Material Science Engineering Dept. Batch '96

    R.O.China.          http://140.114.63.14:6083/simon.html
    886-35-715131-7300
  =====================================================================

 
 
 

sh var -> awk var -> sh var

Post by Tim Maher/Consult » Tue, 21 Feb 1995 02:28:40



: I want to do something like the following:
: ---------------------------------------------------------------------
: echo "-----------end----------------"
: line_num=`expr $line_num + 1`
: awk '{  
:        for( ; n%=xx ; n++ )  <---xx means a number.
:         printf("\n")
:      }' n=$page_line          <---this need input, else it stop....
: line_num=$n    <-----I want $line_num = n in awk.
: ---------------------------------------------------------------------
: I need certain length of the outfiles, so if "----end----" end at line number
: which is not enough, space lines are added to enough length number.
: Thanks.

: --
:   =====================================================================
:     Simon Tneoh Chee Boon  ~{UEV>ND~}  i?

:     Hsin Chu City     NATIONAL TSING HUA UNI.
:     P.O.Box 2-037     Material Science Engineering Dept. Batch '96

:     R.O.China.                http://140.114.63.14:6083/simon.html
:     886-35-715131-7300
:   =====================================================================
One thing you're missing is that an action " {} " in the body of an AWK program
is only executed after the reading of input has begun.  Since your program
needs no input, you need to move that code into the  BEGIN section.
Because you need to set variables in the BEGIN section (page_line and
line_num), the most portable approach is through selective quoting, as
shown. Also, I recommend using nawk or gawk rather than awk if possible,
if only for the superior error messages!

echo "-----------end----------------"

# line_num=`expr $line_num + 1` # awk can do this more efficiently!
page_line=100  
nawk 'BEGIN {  SIZE='"$page_line"'    # size in lines to which all files will be padded
       for(n='"$line_num"'+ 1 ; n<=SIZE ; n++ )  <---xx means a number.
        printf("\n")
     }'
--

UPCOMING SEATTLE CLASSES:  AWK Programming, 3/6-9; B/K Shell: 3/13-15
Adv. B/K Shell: 3/27-31, LINUX Workshop: 4/24-25; UNIX Sys Adm, 4/26-28
*In Spring: UNIX for Programmers; Adv. C Shell; UNIX Security Workshop*

 
 
 

1. using /bin/sh vars in awk

ok, I know this came up awhile ago, and I dont want to bring it
up again.  However, not enuf disk to keep articles around, and
I forgot the answer:

Question: what syntax do I have to use to get awk to take /bin/sh
          variables in a shell script?

Example:  given a /bin/sh script, and an awk command in it somewheres,
          and i need to pass the var ${fred} to awk, what should
          the following line look like?

          fred="barney"
          wilma=`cat ${file1} | awk '{if($1== fred ){print("match")}}'
                                              ^^^^
             what I need is how to expand fred_|  to get awk to
          replace it with the string "barney"

Since this already came up, just email me the answer.  I've already
tried ${fred}, $fred, "${fred}" and a couple other combos.

thanks

--gaspo.

BTW, the example is just that, I don't need a different way to solve
the example.  just get the vars to awk.

/----------------------------------------------------------------------------\
| Scott "gaspo" Gasparian  --  System Administrator | _>________  _<________ |
| Dept. Informatik, Eidg. Techn. Hochschule, Zurich |/[][][][][]\/[][][][][]\|
| ETH-Zentrum,  CH-8092  Zurich.  T# 01-01-254-7205 |`oo------oo'`oo------oo'|

| ..!ethz-inf!gaspar | along the way.In this proud land,you can't forget your|

\----------------------------------------------------------------------------/

2. A Linux newbie gets his Winmodem going *first* time

3. sh vars in awk scripts??

4. SOLARIS 7 + KERNEL CONFIG

5. SUMMARY: using sh vars in awk calls.

6. Top 10 subjects comp.unix.questions

7. Question: passing $var and "$var" into AWK from script

8. How do I fix this error...

9. awk: -v Var=VAR Obviously not a syntax issue

10. sh->xqf->quake works, fvwm->xqf->quake hangs X?

11. <expr> | while -> var assignment not remembered in Bourne

12. How does the sh var SHACCT work ?

13. Error with 'env var-name=value sh'