Passing awk variable argument to a script which is being called inside awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Passing awk variable argument to a script which is being called inside awk
# 1  
Old 02-11-2013
Passing awk variable argument to a script which is being called inside awk

consider the script below

Code:
sh /opt/hqe/hqapi1-client-5.0.0/bin/hqapi.sh  alert list --host=localhost --port=7443 --user=hqadmin --password=hqadmin --secure=true >/tmp/alerts.xml
 
awk -F'[=|"|<|>|,]' '{for(i=1;i<=NF;i++){
 if($i=="Alert id") {
  if(id!="")
        if(dt!=""){
cmd="sh someScript.sh "printf "%s",alDFid;"
cmd | getline prio
print "priority=" prio
close(cmd)
 printf "ID=%d\nNAME=%s\nFIXED=%s\nDATE=%s\nAlDefID=%d\n\n", id,nm,fx,dt,alDFid; }
  id=($i=="Alert id")?$(i+2):id; }
  nm=($i==" name")?$(i+2):nm;
  fx=($i==" fixed")?$(i+2):fx;
  dt=($i~/^ [0-9]+-/)?$i" "$(i+1):dt;
  alDFid=($i==" alertDefinitionId")?$(i+2):alDFid;
 }
}END{
 if(dt!=""){
 printf "ID=%d\nNAME=%s\nFIXED=%s\nDATE=%s\nAlDefID=%d\n\n", id,nm,fx,dt,alDFid;
}
}' /tmp/alerts.xml


here the script that i am calling is someScript.sh and to that i want to pass the contents of alDFid , i itred giving it in quotes , $ but none of it worked. even tried printf "%s",alDFid even this is not working.


Does anyone know how to pass argument to script from Awk with awk variable??



ALSO
why does
Code:
awk -F'priority=' '{print $2}' | cut -d'"' -f2

inside cmd="" wont work.. it wont consider awk inside this command. it keeps throwing error as

Code:
| grep priority | awk -Fpriority= {print
awk: cmd. line:6:     ^ unterminated string
./alertCheck_vivek.sh_date: line 26: cmd: command not found
./alertCheck_vivek.sh_date: line 26: getline: command not found
./alertCheck_vivek.sh_date: line 27: print: command not found
./alertCheck_vivek.sh_date: line 28: syntax error near unexpected token `cmd'
./alertCheck_vivek.sh_date: line 28: `close(cmd)'

# 2  
Old 02-11-2013
Your idea is correct, but as your code is absolutely unreadable, I only can guess that the variable alDFid is undefined when used to build the external command.
And, of course, you have an unterminated string, i.e. one unmatched double quote (I guess in the printf statement)

Last edited by RudiC; 02-11-2013 at 07:10 AM.. Reason: typo / addendum
# 3  
Old 02-11-2013
the error which was coming was due to below line

Code:
awk: cmd. line:6: cmd="sh /opt/hqe/hqapi1-client-5.0.0/bin/hqapi.sh  alertdefinition list --id=$myAlertDefID --host=localhost --port=7443 --user=hqadmin --password=hqadmin --secure=true | grep priority | awk -Fpriority= {print
awk: cmd. line:6:     ^ unterminated string
./alertCheck_vivek.sh_date: line 26: cmd: command not found
./alertCheck_vivek.sh_date: line 26: getline: command not found
./alertCheck_vivek.sh_date: line 27: print: command not found
./alertCheck_vivek.sh_date: line 28: syntax error near unexpected token `cmd'
./alertCheck_vivek.sh_date: line 28: `close(cmd)'

lets forget about that... the alDFid is defined below is the output

Code:
13214
priority=
ID=10101
NAME=APP-MS-lib_license_common-150040-licenseSchemaTampered-S_R
FIXED=true
DATE= 2013-01-22 07:54:51.3
AlDefID=13214

where the line above priority is alDFid
the snippet which i used was

Code:
awk -F'[=|"|<|>|,]' '{for(i=1;i<=NF;i++){
 if($i=="Alert id") {
  if(id!="")
        if(dt!=""){
printf "%s\n",alDFid;
cmd="sh someScript.sh alDFid"
cmd | getline prio
print "priority=" prio
close(cmd)
 printf "ID=%d\nNAME=%s\nFIXED=%s\nDATE=%s\nAlDefID=%d\n\n", id,nm,fx,dt,alDFid; }
  id=($i=="Alert id")?$(i+2):id; }
  nm=($i==" name")?$(i+2):nm;
  fx=($i==" fixed")?$(i+2):fx;
  dt=($i~/^ [0-9]+-/)?$i" "$(i+1):dt;
  alDFid=($i==" alertDefinitionId")?$(i+2):alDFid;
 }
}END{
 if(dt!=""){
 printf "ID=%d\nNAME=%s\nFIXED=%s\nDATE=%s\nAlDefID=%d\n\n", id,nm,fx,dt,alDFid;
}
}' /tmp/alerts.xml

in the above i want to pass the content of alDFid to someScript.sh script. But current implementation is passing string "alDFid " to the script.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing the value of variable which is read from command line in called script

Hi, I am calling a Perl script in my shell script. When Perl script is executed it asks for a answer to be entered by user from terminal. How can i pass that value from my shell script ?? I know I can change perl script to default the answer but i dont have access to do that so only option i... (5 Replies)
Discussion started by: varun22486
5 Replies

2. Shell Programming and Scripting

Problem passing a search pattern to AWK inside a script loop

Learning, stumbling! My progress in shell scripting is slow. Now I have this doubt: I have the following file (users.txt): AU0909,on AU0309,off AU0209,on AU0109,off And this file (userson.txt) AU0909 AU0209 AU0109 AU0309 I just want to set those users on userson.txt to "off" in... (14 Replies)
Discussion started by: quinestor
14 Replies

3. Shell Programming and Scripting

Passing --usage as argument to awk script

I have the awk script below and things go wrong when I do awk -v dsrmx=25 -f ./checkSRDry.awk --usage I basically want to override the usual --usage and --help that awk gives. How do people usually handle this situation when you also want to supply your own usage and help concerning the... (2 Replies)
Discussion started by: kristinu
2 Replies

4. Shell Programming and Scripting

Passing value as a command line argument in awk script.

I have one working awk command line. Which taking data from the “J1202523.TXT” file and generating the “brazil.dat” file. PFB code. awk '{ DUNS = substr($0,0,9);if ( substr($0,14,3) == "089" ) print DUNS }' J1202523.TXT > Brazil.dat But now I want to pass two parameter as a command line argument... (4 Replies)
Discussion started by: humaemo
4 Replies

5. Shell Programming and Scripting

Passing argument to system call in awk script

So, I have this script. It reads a CSV file that has a mixture of object names with IP addresses (parsing out that part I have working), and object names which have a DNS name. I want to be able to run a "dig +short" based off of the name given to me in the line of the awk script, and then deal... (6 Replies)
Discussion started by: mikesimone
6 Replies

6. Shell Programming and Scripting

Passing non Awk Argument in Awk

Dear Conerned, I am facing a situation where i need to pass an argument which is non-awk variable like day=090319 awk '/TID:R/ && /TTIN:/' transaction.log I want to add this day variable like below awk '/TID:R$day/ && /TTIN:/' transaction.log But it is not working. :confused: (1 Reply)
Discussion started by: saifurshaon
1 Replies

7. Shell Programming and Scripting

passing argument into awk

i'm trying to pass a numerical argument with function xyz to print specfic lines of filename, but my 'awk' syntax is incorrect. ie xyx 3 (prints the 3rd line, separated by ':' of filename) function xyz() { arg1=$1 cat filename | awk -F: -v x=$arg1 '{print $x}' } any ideas? (4 Replies)
Discussion started by: prkfriryce
4 Replies

8. UNIX for Advanced & Expert Users

Passing a variable into an awk script

Hello all, I'm trying to run a script of this format - for i in $(cat <file>); do grep $i <file1>|awk '{print $i, $1, $2}' It's not working - does anyone know how this can be done? Khoom (5 Replies)
Discussion started by: Khoomfire
5 Replies

9. Shell Programming and Scripting

passing awk variable to the shell script

hi; i have a file containing lines like: 1|1069108123|96393669788|00963215755711|2|0|941||;serv:Pps6aSyria;first:0;bear i want to extract the second, third and fourth record of each line and store it in a file ";" seperated this is what i wrote while read line do ... (3 Replies)
Discussion started by: bcheaib
3 Replies

10. UNIX for Dummies Questions & Answers

Passing argument to awk script

I am writing a shell script. Now i need to read in a string and send it to an awk file to compare and search for compatible record. I wrote it like tat: read serial | awk -f generate.awk data.dat p/s: the data file got 6 field. According to an expert, we can write it like tat: read... (1 Reply)
Discussion started by: AkumaTay
1 Replies
Login or Register to Ask a Question