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 ...
- 01-13-2010 #1Just 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 ?
- 01-13-2010 #2
Try this
It works with bash...Code:export JOB_STREAM="SCC_DAILY_UPDATE" awk '{printf("%s\n", ENVIRON["JOB_STREAM"])}' testfile.datMake mine Arch Linux
- 01-13-2010 #3Just 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!
- 01-14-2010 #4Linux Engineer
- 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:
For example: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
producing: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
I had no trouble with the backquote-execution. However, I prefer the syntax that usesCode:% ./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."
for a number of reasons.Code:$( ... )
Best wishes ... cheers, drlWelcome - 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 )
- 01-14-2010 #5Just 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
- 01-14-2010 #6Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,117
Hi.
Do not use:
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, drlCode:export JOB_STREAM="DAILY_UPDATE"
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 )
- 01-14-2010 #7Just 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!


Reply With Quote