Find the answer to your Linux question:
Results 1 to 7 of 7
could anyone help out with this? The following command line works in korn shell awk '/SCC_DAILY_UPDATE/ {print $2}' service_stream_mapping.dat but this doesn't export JOB_STREAM="SCC_DAILY_UPDATE" awk '/$JOB_STREAM/ {print $2}' service_stream_mapping.dat I ...
  1. #1
    Just Joined!
    Join Date
    Jan 2010
    Posts
    6

    new to awk!

    could anyone help out with this?

    The following command line works in korn shell
    awk '/SCC_DAILY_UPDATE/ {print $2}' service_stream_mapping.dat

    but this doesn't

    export JOB_STREAM="SCC_DAILY_UPDATE"
    awk '/$JOB_STREAM/ {print $2}' service_stream_mapping.dat


    I need to be able to pass the string to search as a parameter.
    Any ideas ?

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

    Code:
    export JOB_STREAM="SCC_DAILY_UPDATE"
    awk '{printf("%s\n", ENVIRON["JOB_STREAM"])}' testfile.dat
    It works with bash...
    Make mine Arch Linux

  3. #3
    Just Joined!
    Join Date
    Jan 2010
    Posts
    6
    That doesn't seem to work in ksh,

    but I figured it out eventually using this:

    $ awk '/'^${JOB_STREAM}'/ {print$2}' service_stream_mapping.dat
    DOLNSGC3


    I have now encountered a different problem, I cannot assign the result to a variable:

    e.g.
    export GET_RESULT
    GET_RESULT=`awk '/'^${JOB_STREAM}'/ {print$2}' service_stream_mapping.dat`
    echo $GET_RESULT


    doesn't print anything!
    Any further help would be much appreciated!

  4. #4
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    Getting shell variables into awk has been improved with modern versions of awk. The method that appeals to me is:
    Code:
           -v var=val
           --assign var=val
                  Assign  the  value  val to the variable var, before execution of
                  the program begins.  Such variable values are available  to  the
                  BEGIN block of an AWK program.
    
    -- excerpt from man awk
    For example:
    Code:
    #!/usr/bin/env ksh
    
    # @(#) s1	Demonstrate method of getting shell variables into awk.
    
    echo
    export LC_ALL=C
    echo "Environment: LC_ALL = $LC_ALL"
    echo "(Versions displayed with local utility \"version\")"
    version >/dev/null 2>&1 && version "=o" $(_eat $0 $1) awk
    echo
    
    echo "Results:"
    greeting=" Hello, world."
    awk -v hi="$greeting" '
    BEGIN	{ print hi ; exit }
    '
    
    echo
    echo "Results, assignment:"
    GET_RESULT=`awk -v hi="$greeting" '
    BEGIN	{ print hi ; exit }
    '`
    echo " GET_RESULT is \"$GET_RESULT\""
    
    echo
    echo "Results, assignment, modern syntax:"
    GET_RESULT=$( awk -v hi="$greeting" '
    BEGIN	{ print hi ; exit }
    ' )
    echo " GET_RESULT is \"$GET_RESULT\""
    
    exit 0
    producing:
    Code:
    % ./s1
    
    Environment: LC_ALL = C
    (Versions displayed with local utility "version")
    OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
    Distribution        : Debian GNU/Linux 5.0 
    ksh 93s+
    GNU Awk 3.1.5
    
    Results:
     Hello, world.
    
    Results, assignment:
     GET_RESULT is " Hello, world."
    
    Results, assignment, modern syntax:
     GET_RESULT is " Hello, world."
    I had no trouble with the backquote-execution. However, I prefer the syntax that uses
    Code:
    $( ... )
    for a number of reasons.

    Best wishes ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  5. #5
    Just Joined!
    Join Date
    Jan 2010
    Posts
    6
    thx drl

    I have tried the following:

    export $JOB_STREAM="DAILY_UPDATE"
    $SERVICE_NAME="$(awk -v VAR=$JOB_STREAM' '/VAR/ {print $2}' service_stream_mapping.dat)"

    and the following syntax error appears:

    awk: syntax error near line 1
    awk: bailing out near line 1
    ksh[6]: =: not found


    An error does not appear when I try the following;
    $SERVICE_NAME=$(awk '/$x/ {print $2}' x='${JOB_STREAM}' service_stream_mapping.dat)

    but I get the following run time message:
    ksh[6]: =: not found

  6. #6
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    Do not use:
    Code:
    export $JOB_STREAM="DAILY_UPDATE"
    but rather:
    Code:
    export JOB_STREAM="DAILY_UPDATE"
    that is, remove the "$" on the assignments; and, if you use the "-v" syntax, you will not need the export, just the assignment ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  7. #7
    Just Joined!
    Join Date
    Jan 2010
    Posts
    6
    thx dlr,

    yes you right, I got the variables all mixed up!!!

    I finally got it working using the following script:

    export JOB_STREAM="SCC_DAILY_UPDATE"

    export SERVICE_NAME=""

    SERVICE_NAME=$(awk '$1==x {print $2}' x="${JOB_STREAM}" service_stream_mapping.dat)

    echo $SERVICE_NAME


    thx again!

Posting Permissions

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