calling external values inside awk command


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users calling external values inside awk command
# 1  
Old 04-16-2008
calling external values inside awk command

I have a code as follows

awk ' BEGIN{FS=OFS=","}
{
n=split($3,a1,"~")
split($4,a2,"~")
split($5,a3,"~")
for(i=1;i<=n;i++) {
print $1,$2,a1[i],a2[i],a3[i],date
}
}' file

In the above code I need to add current date(date). How is this possible to call an date or a exported value inside awk.
# 2  
Old 04-16-2008
Code:
awk -v date=$(date +%Y%m%d) 'BEGIN ...

Adjust the command to print the date in the format you want it. +%Y%m%d looks like 20080416 -- see the date manual page for the available options.
# 3  
Old 04-16-2008
awk -v RD=$(date +%Y%m%d) 'BEGIN{FS=OFS="|"}
{
n=split($7,B_ID,"~") split($8,B_VAL,"~") split($9,B_COS,"~")
for(i=1;i<=n;i++)
{
print $1,$2,$3,$4,$5,$6,B_ID[i],B_VAL[i],B_COS[i],$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20,$21,$22,$23,$24,$25,$26,$27,$
28,$29,$30,$31,$32,$35,$36,$37,$38,$RD
}
}' $Source_Path/EServe_0CCS_IN_${FD_WH}_$HR1$HR2.TXT > $Target_Path/R_GPC_$3_${HR1}_${HR2}.dat

It gives me an error

syntax error at line 9: `(' unexpected
# 4  
Old 04-16-2008
The syntax error appears to be unrelated. Please put that inside [CODE] tags so it's easier to see what's where. Looks to me like you have only 8 lines; did you cut out other stuff and if so which line is line 9?

If your shell doesn't understand $(...) try with backticks instead;

Code:
RD=`date +%Y%m%d`

By the way, in the awk script, you want RD, not $RD
# 5  
Old 04-16-2008
You can also use getline to retrieve the current date as in the following example
Code:
BEGIN {
    "date" | getline cur_date
    close("date")
}
{
     print cur_date
}

# 6  
Old 04-16-2008
Dear era

Here is the code

Code:
awk -v rd=`date` ' BEGIN{FS=OFS=","}
{
n=split($3,a1,"~")
split($4,a2,"~")
split($5,a3,"~")
for(i=1;i<=n;i++) {
print $1,$2,a1[i],a2[i],a3[i]
}
}' Myin.txt

This give me following error

awk: syntax error near line 1
awk: bailing out near line 1

If I remove the bolded code,its working properly?
# 7  
Old 04-16-2008
if on Soalris, use either /usr/bin/nawk or /usr/xpg4/bin/awk
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk appending values inside a for loop

Hi i have a 2 files say test1 and test2 with the following data. cat file test test1 i want to append the output from a awk one liner to both the files. for i in cat file;do awk '/ whats happening is its printing the output properly. but not appending the way it showing in print... (1 Reply)
Discussion started by: venkitesh
1 Replies

2. Shell Programming and Scripting

Command in inside awk statement

Hello can you please help me with below script which is meant to delete clients from multiple netbackup policies I want to run a command insdie awk statement apparelnlty this script is not working for me for i in $( cat clients_list) do bppllist -byclient $i | awk... (6 Replies)
Discussion started by: Sara_84
6 Replies

3. Shell Programming and Scripting

Multiple command execution inside awk command during xml parsing

below is the output xml string from some other command and i will be parsing it using awk cat /tmp/alerts.xml <Alert id="10102" name="APP-DS-ds_ha-140018-componentFailure-S" alertDefinitionId="13982" resourceId="11427" ctime="1359453507621" fixed="false" reason="If Event/Log Level(ANY) and... (2 Replies)
Discussion started by: vivek d r
2 Replies

4. Shell Programming and Scripting

Calling array inside awk

Hello I have the file df.tmp FS is actually the / FS but escape character\ and end of line $ is used in order to fetch exctly / and not other filesystems. awk '/\/$/ {print $(NF-1)+0}' df.tmp will work properly and return a value eg. 60 but when I am trying to issue the command with the array... (3 Replies)
Discussion started by: drbiloukos
3 Replies

5. Shell Programming and Scripting

Running external programs inside an if then else

Good morning, First time poster. be gentle! :) I do not know coding all that well but I can hack the hell out of example scripts! I just cant wrap my brain around this one. Im trying to run two grep statements in the first IF statement and doing an AND between them. Something is going... (6 Replies)
Discussion started by: mrplow2k69
6 Replies

6. Shell Programming and Scripting

calling a method inside awk

Hi All, How do we call a method existing in another file inside awk. After matching a pattern i want to call a method secureCopy that exists in another file, but method not getting called: ls -l | awk -v var2=$servername -v var1=$srcserverpath -v var3=$tgtpath '... (1 Reply)
Discussion started by: abhinav192
1 Replies

7. Shell Programming and Scripting

Calling external function in a shell

hi guys, how r u??? please I need you, help me please. I have a shell, in this shell i have this function and another code lines, this function is getting date one day back. the function is in the same shell (FILE 1) Now I need put this function in another file (FILE 2) and calling... (4 Replies)
Discussion started by: acevallo
4 Replies

8. Shell Programming and Scripting

How to use refrence values inside sed command

I am using script S_db=`grep SRC_DB= ${login_txt} | cut -d= -f2` T_db=`grep TGT_DB= ${login_txt} | cut -d= -f2` today=`date +%Y%m%d` sed 's/USER_WORK/RAR_WORK_D1/g' < ${my_path}/files/${T_target}_${today}_DDL.txt > ${my_path}/files/temp9 But when I am trying to remove hardcode values... (1 Reply)
Discussion started by: scorp_rahul23
1 Replies

9. Shell Programming and Scripting

calling function inside awk

Hi All, My 1.txt contains some functions fun1() .... .... fun2() .... .... I can call these fun from 2.txt inside awk as below value="`fun1 "argument1"`" awk 'BEGIN {printf ("%s", "'"$value"'")}' I need to modify the above code so that without using the variable to store... (2 Replies)
Discussion started by: jisha
2 Replies

10. Programming

Problems calling external C routines from PL/SQL

Hi everybody! I'm not familiar with C programming in Unix, but I'm trying to make work an example to execute external procedures (developed in C) from PL/SQL. The example includes .c and .pc source files, which I have compiled succesfully. After that, links the .o files into .so to declare... (0 Replies)
Discussion started by: lwnorowski
0 Replies
Login or Register to Ask a Question