calling external values inside awk command


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users calling external values inside awk command
# 8  
Old 04-16-2008
You need to put double quotes around the value if it contains spaces (which just plain date does; that's why I suggested using the +%Y%m%d format).

Code:
awk -v rd="`date`" '...'


Last edited by era; 04-16-2008 at 10:12 AM.. Reason: Note that just date prints a string with spaces
# 9  
Old 04-16-2008
Hai

I got the solution. But there is small problem

Code:
nawk -v rd=`date +%Y"-"%m"-"%d" "%T` ' 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],rd
}
}' Myin.txt

When I try to seperate date and Time stamp using Quotes, its giving an error
nawk: syntax error at source line 1
context is
>>> 19: <<<
nawk: bailing out at source line 1

My final out put of rd is YYYY-MM-DD HH:MI:SS
# 10  
Old 04-16-2008
Use a unique delimiter to separate the date from the time and then remove that delimiter with a space inside the action part of the nawk script.

Code:
nawk -v rd=`date +%Y-%m-%d::%T` 'BEGIN{FS=OFS=","}
{
sub("::"," ",rd)
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],rd
}
}' Myin.txt

# 11  
Old 04-16-2008
Repeat: if the value contains spaces, you need to double-quote the whole of it.

Code:
nawk -v rd="`date +%Y"-"%m"-"%d" "%T`" 'BEGIN ...'

# 12  
Old 04-16-2008
Quote:
Originally Posted by shamrock
Use a unique delimiter to separate the date from the time and then remove that delimiter with a space inside the action part of the nawk script.

Code:
nawk -v rd=`date +%Y-%m-%d::%T` 'BEGIN{FS=OFS=","}
{
sub("::"," ",rd)
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],rd
}
}' Myin.txt

you don't need to go through this pain.....
as era's noted previously....
Code:
nawk -v rd="`date '+%Y-%m-%d %T'`" 'BEGIN...'

# 13  
Old 04-16-2008
Quote:
Originally Posted by vgersh99
you don't need to go through this pain.....
as era's noted previously....
Code:
nawk -v rd="`date '+%Y-%m-%d %T'`" 'BEGIN...'

Unfortunately era's code has awk on my machine still complaining about the space that's why I came up with that alternative Smilie
# 14  
Old 04-22-2008
rd = `date +%Y-%m-%d" "%H:%M:%S`
print $rd
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