Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How integrate AWK in KSH Shell

Status
Not open for further replies.

nymus

IS-IT--Management
Jul 6, 2005
16
CH
Hi all

Can we integrates a AWK script in a KSH script?
How do we make?

#! /bin/ksh
...
list_media()
{
for MediaNr in $(vmquery -a | grep "media" |awk 'BEGIN{FS=": "};{print $NF}')
do
# here is the problem !!!
awk 'BEGIN { FS=": " }
{
$1 ~ /mount/ {print $NF} >$FirstMount
}
END { echo $FirstMount }'$MediaNr
done
}
...

The structure is : awk 'BEGIN {parameters} {program}' but how to integrate this?
Thanks for your help
nmn
 
Hi

Sorry, that code is abit fuzzy for me. I think, you want to use shell variables inside the [tt]awk[/tt] script. If so, you can use one of the following :
Code:
MYVAR=2

[gray]# interrupt the single quotes around the variable[/gray]
seq 3 | awk '$1=='$MYVAR'{ print '$MYVAR',"found on line",NR }'

[gray]# use double quotes and escape characters from shell[/gray]
seq 3 | awk "\$1==$MYVAR'{ print $MYVAR,\"found on line\",NR }"

[gray]# set an awk variable from outside[/gray]
seq 3 | awk -v awkvar=$MYVAR '$1==awkvar{ print awkvar,"found on line",NR }'

Feherke.
 
Hi

Is it possible to use other variable als $1 or $<num>?
Could I use by ex.: $var='$MYVAR' and that before the BEGIN?
Because I still using the $1,$2,$3,etc...
Code:
seq 3 | awk '$var=='$MYVAR'BEGIN{<param} { print '$MYVAR',"found on line",NR }'
Thanks
nm
 
Any chance you could explain WHAT DO YOU WANT TO DO ?
 
Code:
    # here is the problem !!!
    awk 'BEGIN { FS=": " }
    {
      [b][COLOR=red]'[/color][/b]$1[b][COLOR=red]'[/color][/b] ~ /mount/ {print $NF} >$FirstMount
    }
    END { echo $FirstMount }'$MediaNr  
  done

Should do it for the code you posted. It may look like the single quotes are around the $1, but they are actually closing a single quoted string before it and opening a new one after it, which was the first of feherke's suggestions.

- Rod

IBM Certified Advanced Technical Expert pSeries and AIX 5L
CompTIA Linux+
CompTIA Security+

Wish you could view posts with a fixed font? Got Firefox & Greasemonkey? Give yourself the option.
 
Thanks for the tips, it is that I need to know!
Ok, I have many questions relating to awk, this is why I tried to put the most important questions!
I still have a question, of which I trying to be more precise: How can I start a program or another script in awk?

I've a command that I can start direct from shell :
Code:
bpmedialist -mlist -m $Media|awk '{print $2}'
and the result is a date, like dd/mm/yyyy.
Now I wish to integrate this command in my script awk.
My script read a file which contents a list of media ID.
For each Media ID I want to know his expiration date and his first mount. Then I want to use the result of expiration date for further if-test.
The problem is in red[/COLOR red] in the script.
Code:
cat $ListMediaID
while read MediaID
do
  [COLOR=red]expdate=$(bpmedialist -mlist -m $Media|awk '{print $2}')[/COLOR red]
  vmquery -m $MediaID | awk '
  BEGIN {FS=": "}
  $1 ~ /mount/ { 
    [COLOR=red]if (expdate = "") {[/COLOR red]
      print "no expiration date"
    }
    print "first mount :\t"$NF
 }'
done
Thanks
-nm
 
In the man awk page pay attention to getline

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
getline is perhaps that I need, I'll study it!
Thanks a lot
-nm
I'm using awk on AIX machine:)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top